comparison mercurial/demandimport.py @ 3903:f9136599700f

Make demandimport pass all tests on python2.5.
author Brendan Cully <brendan@kublai.com>
date Fri, 15 Dec 2006 20:16:20 -0800
parents 316cd5b0c940
children 6d0d025e125a
comparison
equal deleted inserted replaced
3902:0d27502a804c 3903:f9136599700f
57 object.__setattr__(self, "_module", mod) 57 object.__setattr__(self, "_module", mod)
58 def __repr__(self): 58 def __repr__(self):
59 return "<unloaded module '%s'>" % self._data[0] 59 return "<unloaded module '%s'>" % self._data[0]
60 def __call__(self, *args, **kwargs): 60 def __call__(self, *args, **kwargs):
61 raise TypeError("'unloaded module' object is not callable") 61 raise TypeError("'unloaded module' object is not callable")
62 def __getattr__(self, attr): 62 def __getattribute__(self, attr):
63 if attr in ('_data', '_extend', '_load', '_module'):
64 return object.__getattribute__(self, attr)
63 self._load() 65 self._load()
64 return getattr(self._module, attr) 66 return getattr(self._module, attr)
65 def __setattr__(self, attr, val): 67 def __setattr__(self, attr, val):
66 self._load() 68 self._load()
67 setattr(self._module, attr, val) 69 setattr(self._module, attr, val)
72 return _origimport(name, globals, locals, fromlist) 74 return _origimport(name, globals, locals, fromlist)
73 elif not fromlist: 75 elif not fromlist:
74 # import a [as b] 76 # import a [as b]
75 if '.' in name: # a.b 77 if '.' in name: # a.b
76 base, rest = name.split('.', 1) 78 base, rest = name.split('.', 1)
79 # email.__init__ loading email.mime
80 if globals and globals.get('__name__', None) == base:
81 return _origimport(name, globals, locals, fromlist)
77 # if a is already demand-loaded, add b to its submodule list 82 # if a is already demand-loaded, add b to its submodule list
78 if base in locals: 83 if base in locals:
79 if isinstance(locals[base], _demandmod): 84 if isinstance(locals[base], _demandmod):
80 locals[base]._extend(rest) 85 locals[base]._extend(rest)
81 return locals[base] 86 return locals[base]
90 # set requested submodules for demand load 95 # set requested submodules for demand load
91 if not(hasattr(mod, x)): 96 if not(hasattr(mod, x)):
92 setattr(mod, x, _demandmod(x, mod.__dict__, mod.__dict__)) 97 setattr(mod, x, _demandmod(x, mod.__dict__, mod.__dict__))
93 return mod 98 return mod
94 99
95 ignore = ['_hashlib', 'email.mime'] 100 ignore = []
96 101
97 def enable(): 102 def enable():
98 "enable global demand-loading of modules" 103 "enable global demand-loading of modules"
99 __builtins__["__import__"] = _demandimport 104 __builtins__["__import__"] = _demandimport
100 105