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

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Mar 11 11:47:03 UTC 2016


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at 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.

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.


More information about the Mercurial-devel mailing list