[PATCH 1 of 3 V4] demandimport: consolidate code for processing items in fromlist

Gregory Szorc gregory.szorc at gmail.com
Sun Oct 4 18:18:42 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1443911417 25200
#      Sat Oct 03 15:30:17 2015 -0700
# Node ID 7d27540d059ce9651aa19411d089c9c84be3344a
# Parent  97dc6ab42aad232c73180dee648685c26662230b
demandimport: consolidate code for processing items in fromlist

This code was mostly duplicated. An upcoming patch will add more
complexity, making the duplication harder to justify. Consolidate the
code.

diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
--- a/mercurial/demandimport.py
+++ b/mercurial/demandimport.py
@@ -142,8 +142,18 @@ def _demandimport(name, globals=None, lo
         # level >= 0: absolute only (Python 2 w/ absolute_import and Python 3).
         # The modern Mercurial convention is to use absolute_import everywhere,
         # so modern Mercurial code will have level >= 0.
 
+        def processfromitem(mod, attr, **kwargs):
+            """Process an imported symbol in the import statement.
+
+            If the symbol doesn't exist in the parent module, it must be a
+            module. We set missing modules up as _demandmod instances.
+            """
+            if getattr(mod, attr, nothing) is nothing:
+                setattr(mod, attr,
+                        _demandmod(attr, mod.__dict__, locals, **kwargs))
+
         if level >= 0:
             # Mercurial's enforced import style does not use
             # "from a import b,c,d" or "from .a import b,c,d" syntax. In
             # addition, this appears to be giving errors with some modules
@@ -153,14 +163,11 @@ def _demandimport(name, globals=None, lo
                 return _hgextimport(_origimport, name, globals, locals,
                                     fromlist, level)
 
             mod = _hgextimport(_origimport, name, globals, locals, level=level)
+
             for x in fromlist:
-                # Missing symbols mean they weren't defined in the module
-                # itself which means they are sub-modules.
-                if getattr(mod, x, nothing) is nothing:
-                    setattr(mod, x,
-                            _demandmod(x, mod.__dict__, locals, level=level))
+                processfromitem(mod, x, level=level)
 
             return mod
 
         # But, we still need to support lazy loading of standard library and 3rd
@@ -171,12 +178,12 @@ def _demandimport(name, globals=None, lo
             if getattr(mod, comp, nothing) is nothing:
                 setattr(mod, comp,
                         _demandmod(comp, mod.__dict__, mod.__dict__))
             mod = getattr(mod, comp)
+
         for x in fromlist:
-            # set requested submodules for demand load
-            if getattr(mod, x, nothing) is nothing:
-                setattr(mod, x, _demandmod(x, mod.__dict__, locals))
+            processfromitem(mod, x)
+
         return mod
 
 ignore = [
     '__future__',


More information about the Mercurial-devel mailing list