##// END OF EJS Templates
demandimport: determine at load time if __import__ has level argument
Simon Heimberg -
r15096:868282fa default
parent child Browse files
Show More
@@ -29,6 +29,15 b' import __builtin__'
29 29
30 30 nothing = object()
31 31
32 try:
33 _origimport(__builtin__.__name__, {}, {}, None, -1)
34 except TypeError: # no level argument
35 def _import(name, globals, locals, fromlist, level):
36 "call _origimport with no level argument"
37 return _origimport(name, globals, locals, fromlist)
38 else:
39 _import = _origimport
40
32 41 class _demandmod(object):
33 42 """module demand-loader and proxy"""
34 43 def __init__(self, name, globals, locals):
@@ -83,20 +92,14 b' class _demandmod(object):'
83 92 def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
84 93 if not locals or name in ignore or fromlist == ('*',):
85 94 # these cases we can't really delay
86 if level == -1:
87 return _origimport(name, globals, locals, fromlist)
88 else:
89 return _origimport(name, globals, locals, fromlist, level)
95 return _import(name, globals, locals, fromlist, level)
90 96 elif not fromlist:
91 97 # import a [as b]
92 98 if '.' in name: # a.b
93 99 base, rest = name.split('.', 1)
94 100 # email.__init__ loading email.mime
95 101 if globals and globals.get('__name__', None) == base:
96 if level != -1:
97 return _origimport(name, globals, locals, fromlist, level)
98 else:
99 return _origimport(name, globals, locals, fromlist)
102 return _import(name, globals, locals, fromlist, level)
100 103 # if a is already demand-loaded, add b to its submodule list
101 104 if base in locals:
102 105 if isinstance(locals[base], _demandmod):
General Comments 0
You need to be logged in to leave comments. Login now