extensions: move the "import" logic out from "load"
authorJun Wu <quark@fb.com>
Mon, 03 Oct 2016 03:37:10 +0100
changeset 30058 8f54f9b8010d
parent 30057 a8ba9a23c893
child 30059 6ffb7e0249f4
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.
mercurial/extensions.py
--- a/mercurial/extensions.py	Fri Oct 07 07:43:04 2016 -0400
+++ b/mercurial/extensions.py	Mon Oct 03 03:37:10 2016 +0100
@@ -80,6 +80,26 @@
         mod = getattr(mod, comp)
     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,
     #       Use --config ui.debug=1 to see them.
@@ -98,21 +118,7 @@
     if shortname in _extensions:
         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
     # compatibility. This gives extension authors a mechanism to have their