Patchwork [1,of,3] extensions: extra the 'importh' closure as normal function

login
register
mail settings
Submitter Pierre-Yves David
Date March 11, 2016, 11:47 a.m.
Message ID <9979de04196de6f0a760.1457696823@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/13760/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Pierre-Yves David - March 11, 2016, 11:47 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1457691894 0
#      Fri Mar 11 10:24:54 2016 +0000
# Node ID 9979de04196de6f0a7609d49ba447f88228a2507
# Parent  5021398417ed44f7d589879ff74ce9e9eaf20b5b
# EXP-Topic hgext3rd
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r 9979de04196d
extensions: extra the 'importh' closure as normal function

There is no reason for this to be a closure so we extract it for clarity.
timeless - March 11, 2016, 2:34 p.m.
> extensions: extra the 'importh' closure as normal function

I think you mean extract
Yuya Nishihara - March 12, 2016, 2:31 p.m.
On Fri, 11 Mar 2016 11:47:03 +0000, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@fb.com>
> # Date 1457691894 0
> #      Fri Mar 11 10:24:54 2016 +0000
> # Node ID 9979de04196de6f0a7609d49ba447f88228a2507
> # Parent  5021398417ed44f7d589879ff74ce9e9eaf20b5b
> # EXP-Topic hgext3rd
> # Available At http://hg.netv6.net/marmoute-wip/mercurial/
> #              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r 9979de04196d
> extensions: extra the 'importh' closure as normal function
> 
> There is no reason for this to be a closure so we extract it for clarity.

Sure, pushed the first two to the clowncopter, thanks.

Patch

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -69,10 +69,18 @@  def loadpath(path, module_name):
         except IOError as exc:
             if not exc.filename:
                 exc.filename = path # python does not fill this
             raise
 
+def _importh(name):
+    """import and return the <name> module"""
+    mod = __import__(name)
+    components = name.split('.')
+    for comp in components[1:]:
+        mod = getattr(mod, comp)
+    return mod
+
 def load(ui, name, path):
     if name.startswith('hgext.') or name.startswith('hgext/'):
         shortname = name[6:]
     else:
         shortname = name
@@ -85,24 +93,18 @@  def load(ui, name, path):
         # the module will be loaded in sys.modules
         # choose an unique name so that it doesn't
         # conflicts with other modules
         mod = loadpath(path, 'hgext.%s' % name)
     else:
-        def importh(name):
-            mod = __import__(name)
-            components = name.split('.')
-            for comp in components[1:]:
-                mod = getattr(mod, comp)
-            return mod
         try:
-            mod = importh("hgext.%s" % name)
+            mod = _importh("hgext.%s" % name)
         except ImportError as err:
             ui.debug('could not import hgext.%s (%s): trying %s\n'
                      % (name, err, name))
             if ui.debugflag:
                 ui.traceback()
-            mod = importh(name)
+            mod = _importh(name)
 
     # Before we do anything with the extension, check against minimum stated
     # compatibility. This gives extension authors a mechanism to have their
     # extensions short circuit when loaded with a known incompatible version
     # of Mercurial.