update demandimport module from mercurial
This commit is contained in:
parent
b4bea04407
commit
e073718907
1 changed files with 20 additions and 10 deletions
|
@ -27,6 +27,17 @@ These imports will not be delayed:
|
||||||
import __builtin__
|
import __builtin__
|
||||||
_origimport = __import__
|
_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):
|
class _demandmod(object):
|
||||||
"""module demand-loader and proxy"""
|
"""module demand-loader and proxy"""
|
||||||
def __init__(self, name, globals, locals):
|
def __init__(self, name, globals, locals):
|
||||||
|
@ -50,7 +61,7 @@ class _demandmod(object):
|
||||||
h, t = p, None
|
h, t = p, None
|
||||||
if '.' in p:
|
if '.' in p:
|
||||||
h, t = p.split('.', 1)
|
h, t = p.split('.', 1)
|
||||||
if not hasattr(mod, h):
|
if getattr(mod, h, nothing) is nothing:
|
||||||
setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
|
setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
|
||||||
elif t:
|
elif t:
|
||||||
subload(getattr(mod, h), t)
|
subload(getattr(mod, h), t)
|
||||||
|
@ -78,20 +89,17 @@ class _demandmod(object):
|
||||||
self._load()
|
self._load()
|
||||||
setattr(self._module, attr, val)
|
setattr(self._module, attr, val)
|
||||||
|
|
||||||
def _demandimport(name, globals=None, locals=None, fromlist=None, level=None):
|
def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
|
||||||
if not locals or name in ignore or fromlist == ('*',):
|
if not locals or name in ignore or fromlist == ('*',):
|
||||||
# these cases we can't really delay
|
# these cases we can't really delay
|
||||||
if level is None:
|
return _import(name, globals, locals, fromlist, level)
|
||||||
return _origimport(name, globals, locals, fromlist)
|
|
||||||
else:
|
|
||||||
return _origimport(name, globals, locals, fromlist, level)
|
|
||||||
elif not fromlist:
|
elif not fromlist:
|
||||||
# import a [as b]
|
# import a [as b]
|
||||||
if '.' in name: # a.b
|
if '.' in name: # a.b
|
||||||
base, rest = name.split('.', 1)
|
base, rest = name.split('.', 1)
|
||||||
# email.__init__ loading email.mime
|
# email.__init__ loading email.mime
|
||||||
if globals and globals.get('__name__', None) == base:
|
if globals and globals.get('__name__', None) == base:
|
||||||
return _origimport(name, globals, locals, fromlist)
|
return _import(name, globals, locals, fromlist, level)
|
||||||
# if a is already demand-loaded, add b to its submodule list
|
# if a is already demand-loaded, add b to its submodule list
|
||||||
if base in locals:
|
if base in locals:
|
||||||
if isinstance(locals[base], _demandmod):
|
if isinstance(locals[base], _demandmod):
|
||||||
|
@ -99,19 +107,19 @@ def _demandimport(name, globals=None, locals=None, fromlist=None, level=None):
|
||||||
return locals[base]
|
return locals[base]
|
||||||
return _demandmod(name, globals, locals)
|
return _demandmod(name, globals, locals)
|
||||||
else:
|
else:
|
||||||
if level is not None:
|
if level != -1:
|
||||||
# from . import b,c,d or from .a import b,c,d
|
# from . import b,c,d or from .a import b,c,d
|
||||||
return _origimport(name, globals, locals, fromlist, level)
|
return _origimport(name, globals, locals, fromlist, level)
|
||||||
# from a import b,c,d
|
# from a import b,c,d
|
||||||
mod = _origimport(name, globals, locals)
|
mod = _origimport(name, globals, locals)
|
||||||
# recurse down the module chain
|
# recurse down the module chain
|
||||||
for comp in name.split('.')[1:]:
|
for comp in name.split('.')[1:]:
|
||||||
if not hasattr(mod, comp):
|
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)
|
mod = getattr(mod, comp)
|
||||||
for x in fromlist:
|
for x in fromlist:
|
||||||
# set requested submodules for demand load
|
# set requested submodules for demand load
|
||||||
if not hasattr(mod, x):
|
if getattr(mod, x, nothing) is nothing:
|
||||||
setattr(mod, x, _demandmod(x, mod.__dict__, locals))
|
setattr(mod, x, _demandmod(x, mod.__dict__, locals))
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
@ -134,6 +142,8 @@ ignore = [
|
||||||
# raise ImportError if x not defined
|
# raise ImportError if x not defined
|
||||||
'__main__',
|
'__main__',
|
||||||
'_ssl', # conditional imports in the stdlib, issue1964
|
'_ssl', # conditional imports in the stdlib, issue1964
|
||||||
|
'rfc822',
|
||||||
|
'mimetools',
|
||||||
]
|
]
|
||||||
|
|
||||||
def enable():
|
def enable():
|
||||||
|
|
Loading…
Add table
Reference in a new issue