Patchwork [3,of,4,V3] demandimport: consolidate code for processing items in fromlist

login
register
mail settings
Submitter Gregory Szorc
Date Oct. 3, 2015, 10:56 p.m.
Message ID <9c85b9664b324a18b78e.1443912972@126.1.168.192.in-addr.arpa>
Download mbox | patch
Permalink /patch/10765/
State Superseded
Commit f2bf76d3d567816649795cf83dc11696f641ac29
Headers show

Comments

Gregory Szorc - Oct. 3, 2015, 10:56 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1443911417 25200
#      Sat Oct 03 15:30:17 2015 -0700
# Node ID 9c85b9664b324a18b78e252526285dfb0a39c161
# Parent  751a5e46ab943ee93eb43c6248b878edd237e92d
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.

Patch

diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
--- a/mercurial/demandimport.py
+++ b/mercurial/demandimport.py
@@ -146,8 +146,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
@@ -157,14 +167,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
@@ -175,12 +182,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__',