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

login
register
mail settings
Submitter Jun Wu
Date Oct. 3, 2016, 6:11 a.m.
Message ID <7d106c3386e17746c9b4.1475475079@x1c>
Download mbox | patch
Permalink /patch/16826/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - Oct. 3, 2016, 6:11 a.m.
# HG changeset patch
# User Jun Wu <quark@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.
Yuya Nishihara - Oct. 8, 2016, 7:03 a.m.
On Mon, 3 Oct 2016 07:11:19 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@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"

This change looks good. Queued this, thanks.

Patch

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