changeset 26455:f2bf76d3d567

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.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 03 Oct 2015 15:30:17 -0700
parents 62c5e937f477
children 86fc4a2863ff
files mercurial/demandimport.py
diffstat 1 files changed, 15 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/demandimport.py	Sat Oct 03 15:16:33 2015 +0900
+++ b/mercurial/demandimport.py	Sat Oct 03 15:30:17 2015 -0700
@@ -143,6 +143,16 @@
         # 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
@@ -154,12 +164,9 @@
                                     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
 
@@ -172,10 +179,10 @@
                 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 = [