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.
--- a/mercurial/demandimport.py Sat Aug 08 17:12:37 2015 -0700
+++ b/mercurial/demandimport.py Sat Aug 08 16:24:57 2015 -0700
@@ -128,15 +128,27 @@
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