diff options
Diffstat (limited to 'mercurial/demandimport.py')
-rw-r--r-- | mercurial/demandimport.py | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/mercurial/demandimport.py b/mercurial/demandimport.py index e439487..4f5d71e 100644 --- a/mercurial/demandimport.py +++ b/mercurial/demandimport.py @@ -27,17 +27,6 @@ These imports will not be delayed: import __builtin__ _origimport = __import__ -nothing = object() - -try: - _origimport(__builtin__.__name__, {}, {}, None, -1) -except TypeError: # no level argument - def _import(name, globals, locals, fromlist, level): - "call _origimport with no level argument" - return _origimport(name, globals, locals, fromlist) -else: - _import = _origimport - class _demandmod(object): """module demand-loader and proxy""" def __init__(self, name, globals, locals): @@ -61,7 +50,7 @@ class _demandmod(object): h, t = p, None if '.' in p: h, t = p.split('.', 1) - if getattr(mod, h, nothing) is nothing: + if not hasattr(mod, h): setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) elif t: subload(getattr(mod, h), t) @@ -92,14 +81,20 @@ class _demandmod(object): def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1): if not locals or name in ignore or fromlist == ('*',): # these cases we can't really delay - return _import(name, globals, locals, fromlist, level) + if level == -1: + return _origimport(name, globals, locals, fromlist) + else: + return _origimport(name, globals, locals, fromlist, level) elif not fromlist: # import a [as b] if '.' in name: # a.b base, rest = name.split('.', 1) # email.__init__ loading email.mime if globals and globals.get('__name__', None) == base: - return _import(name, globals, locals, fromlist, level) + if level != -1: + 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): @@ -114,12 +109,12 @@ def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1): mod = _origimport(name, globals, locals) # recurse down the module chain for comp in name.split('.')[1:]: - if getattr(mod, comp, nothing) is nothing: + if not hasattr(mod, comp): 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: + if not hasattr(mod, x): setattr(mod, x, _demandmod(x, mod.__dict__, locals)) return mod @@ -142,8 +137,6 @@ ignore = [ # raise ImportError if x not defined '__main__', '_ssl', # conditional imports in the stdlib, issue1964 - 'rfc822', - 'mimetools', ] def enable(): @@ -153,3 +146,4 @@ def enable(): def disable(): "disable global demand-loading of modules" __builtin__.__import__ = _origimport + |