[PATCH 3 of 4 V3] demandimport: consolidate code for processing items in fromlist
Gregory Szorc
gregory.szorc at gmail.com
Sat Oct 3 17:56:12 CDT 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 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.
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__',
More information about the Mercurial-devel
mailing list