[PATCH 2 of 3 RFC] extensions: move the "import" logic out from "load"

Jun Wu quark at fb.com
Mon Oct 3 02:11:19 EDT 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1475462230 -3600
#      Mon Oct 03 03:37:10 2016 +0100
# Node ID 7d106c3386e17746c9b49ab9c788535634b3e8f6
# Parent  7b19924c9fd57fbb7fa39c103902d51d7ea9beb4
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 7d106c3386e1
extensions: move the "import" logic out from "load"

The "load" method does too many things: on-demand import and check version.
This patch moves the import logic out from "load" so it could be wrapped to
change the import behavior, for example, chg will use it to pre-import
extensions.

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -81,4 +81,24 @@ def _importh(name):
     return mod
 
+def _importext(name, path=None, reportfunc=None):
+    if 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:
+        try:
+            mod = _importh("hgext.%s" % name)
+        except ImportError as err:
+            if reportfunc:
+                reportfunc(err, "hgext.%s" % name, "hgext3rd.%s" % name)
+            try:
+                mod = _importh("hgext3rd.%s" % name)
+            except ImportError as err:
+                if reportfunc:
+                    reportfunc(err, "hgext3rd.%s" % name, name)
+                mod = _importh(name)
+    return mod
+
 def _reportimporterror(ui, err, failed, next):
     # note: this ui.debug happens before --debug is processed,
@@ -99,19 +119,5 @@ def load(ui, name, path):
         return _extensions[shortname]
     _extensions[shortname] = None
-    if 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:
-        try:
-            mod = _importh("hgext.%s" % name)
-        except ImportError as err:
-            _reportimporterror(ui, err, "hgext.%s" % name, "hgext3rd.%s" % name)
-            try:
-                mod = _importh("hgext3rd.%s" % name)
-            except ImportError as err:
-                _reportimporterror(ui, err, "hgext3rd.%s" % name, name)
-                mod = _importh(name)
+    mod = _importext(name, path, bind(_reportimporterror, ui))
 
     # Before we do anything with the extension, check against minimum stated


More information about the Mercurial-devel mailing list