[PATCH 3 of 5 V2] demandimport: refactor logic and add documentation

Gregory Szorc gregory.szorc at gmail.com
Sat Aug 8 20:17:05 CDT 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1439076297 25200
#      Sat Aug 08 16:24:57 2015 -0700
# Node ID 4ab009c00deee53a6125219dbba66638634a7ca5
# Parent  4bb6021564277224c09f2a9ea63249af7b33876d
demandimport: refactor logic and add documentation

demandimport doesn't currently support absolute imports (level >= 0).
In preparation for this, we add some documentation and a code branch
to handle the absolute_import case.

diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py
--- a/mercurial/demandimport.py
+++ b/mercurial/demandimport.py
@@ -127,17 +127,29 @@ def _demandimport(name, globals=None, lo
                     locals[base]._extend(rest)
                 return locals[base]
         return _demandmod(name, globals, locals, level)
     else:
-        if level != -1:
-            # from . import b,c,d or from .a import b,c,d
+        # There is a fromlist.
+        # from a import b,c,d
+        # from . import b,c,d
+        # from .a import b,c,d
+
+        # level == -1: relative and absolute attempted (Python 2 only).
+        # 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.
+
+        if level >= 0:
             return _origimport(name, globals, locals, fromlist, level)
-        # from a import b,c,d
+
+        # But, we still need to support lazy loading of standard library and 3rd
+        # party modules. So handle level == -1.
         mod = _hgextimport(_origimport, name, globals, locals)
         # recurse down the module chain
         for comp in name.split('.')[1:]:
             if getattr(mod, comp, nothing) is nothing:
-                setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
+                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:


More information about the Mercurial-devel mailing list