diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py index 4532c44bb62da095c260724ca4165559626efa65_bWVyY3VyaWFsL2RlbWFuZGltcG9ydC5weQ==..50a4e55aa2781e2c508a668d1fc45660a21b2b64_bWVyY3VyaWFsL2RlbWFuZGltcG9ydC5weQ== 100644 --- a/mercurial/demandimport.py +++ b/mercurial/demandimport.py @@ -29,10 +29,10 @@ class _demandmod(object): """module demand-loader and proxy""" - def __init__(self, name, globals, locals): + def __init__(self, name, globals, locals, level): if '.' in name: head, rest = name.split('.', 1) after = [rest] else: head = name after = [] @@ -33,13 +33,13 @@ if '.' in name: head, rest = name.split('.', 1) after = [rest] else: head = name after = [] - object.__setattr__(self, "_data", (head, globals, locals, after)) + object.__setattr__(self, "_data", (head, globals, locals, after, level)) object.__setattr__(self, "_module", None) def _extend(self, name): """add to the list of submodules to load""" self._data[3].append(name) def _load(self): if not self._module: @@ -40,14 +40,17 @@ object.__setattr__(self, "_module", None) def _extend(self, name): """add to the list of submodules to load""" self._data[3].append(name) def _load(self): if not self._module: - head, globals, locals, after = self._data - mod = _origimport(head, globals, locals) + head, globals, locals, after, level = self._data + if level is not None: + mod = _origimport(head, globals, locals, level) + else: + mod = _origimport(head, globals, locals) # load submodules def subload(mod, p): h, t = p, None if '.' in p: h, t = p.split('.', 1) if not hasattr(mod, h): @@ -48,10 +51,13 @@ # load submodules def subload(mod, p): h, t = p, None if '.' in p: h, t = p.split('.', 1) if not hasattr(mod, h): - setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) + # TODO: should we adjust the level here? + submod = _demandmod(p, mod.__dict__, mod.__dict__, + level=level) + setattr(mod, h, submod) elif t: subload(getattr(mod, h), t) @@ -91,9 +97,12 @@ base, rest = name.split('.', 1) # email.__init__ loading email.mime if globals and globals.get('__name__', None) == base: - return _origimport(name, globals, locals, fromlist) + if level is not None: + return _origimport(name, globals, locals, fromlist, level) + else: + return _origimport(name, globals, locals, fromlist) # if a is already demand-loaded, add b to its submodule list if base in locals: if isinstance(locals[base], _demandmod): locals[base]._extend(rest) return locals[base] @@ -95,7 +104,7 @@ # if a is already demand-loaded, add b to its submodule list if base in locals: if isinstance(locals[base], _demandmod): locals[base]._extend(rest) return locals[base] - return _demandmod(name, globals, locals) + return _demandmod(name, globals, locals, level=level) else: @@ -101,2 +110,3 @@ else: + # from a import b,c,d if level is not None: @@ -102,8 +112,7 @@ if level is not None: - # from . import b,c,d or from .a import b,c,d - return _origimport(name, globals, locals, fromlist, level) - # from a import b,c,d - mod = _origimport(name, globals, locals) + mod = _origimport(name, globals, locals, level=level) + else: + mod = _origimport(name, globals, locals) # recurse down the module chain for comp in name.split('.')[1:]: if not hasattr(mod, comp): @@ -107,8 +116,11 @@ # recurse down the module chain for comp in name.split('.')[1:]: if not hasattr(mod, comp): - setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__)) + # TODO: should we adjust the level here? + submod = _demandmod(comp, mod.__dict__, mod.__dict__, + level=level) + setattr(mod, comp, submod) mod = getattr(mod, comp) for x in fromlist: # set requested submodules for demand load if not(hasattr(mod, x)): @@ -111,8 +123,10 @@ mod = getattr(mod, comp) for x in fromlist: # set requested submodules for demand load if not(hasattr(mod, x)): - setattr(mod, x, _demandmod(x, mod.__dict__, locals)) + # TODO: should we adjust the level here? + submod = _demandmod(x, mod.__dict__, locals, level=level) + setattr(mod, x, submod) return mod ignore = [