##// END OF EJS Templates
demandimport: use getattr instead of hasattr...
Augie Fackler -
r14977:1dbd42a0 default
parent child Browse files
Show More
@@ -27,6 +27,8 b' These imports will not be delayed:'
27 import __builtin__
27 import __builtin__
28 _origimport = __import__
28 _origimport = __import__
29
29
30 nothing = object()
31
30 class _demandmod(object):
32 class _demandmod(object):
31 """module demand-loader and proxy"""
33 """module demand-loader and proxy"""
32 def __init__(self, name, globals, locals):
34 def __init__(self, name, globals, locals):
@@ -50,7 +52,7 b' class _demandmod(object):'
50 h, t = p, None
52 h, t = p, None
51 if '.' in p:
53 if '.' in p:
52 h, t = p.split('.', 1)
54 h, t = p.split('.', 1)
53 if not hasattr(mod, h):
55 if getattr(mod, h, nothing) is nothing:
54 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
56 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
55 elif t:
57 elif t:
56 subload(getattr(mod, h), t)
58 subload(getattr(mod, h), t)
@@ -109,12 +111,12 b' def _demandimport(name, globals=None, lo'
109 mod = _origimport(name, globals, locals)
111 mod = _origimport(name, globals, locals)
110 # recurse down the module chain
112 # recurse down the module chain
111 for comp in name.split('.')[1:]:
113 for comp in name.split('.')[1:]:
112 if not hasattr(mod, comp):
114 if getattr(mod, comp, nothing) is nothing:
113 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
115 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
114 mod = getattr(mod, comp)
116 mod = getattr(mod, comp)
115 for x in fromlist:
117 for x in fromlist:
116 # set requested submodules for demand load
118 # set requested submodules for demand load
117 if not hasattr(mod, x):
119 if getattr(mod, x, nothing) is nothing:
118 setattr(mod, x, _demandmod(x, mod.__dict__, locals))
120 setattr(mod, x, _demandmod(x, mod.__dict__, locals))
119 return mod
121 return mod
120
122
@@ -148,4 +150,3 b' def enable():'
148 def disable():
150 def disable():
149 "disable global demand-loading of modules"
151 "disable global demand-loading of modules"
150 __builtin__.__import__ = _origimport
152 __builtin__.__import__ = _origimport
151
General Comments 0
You need to be logged in to leave comments. Login now