##// END OF EJS Templates
demandimport: fix an obscure corner-case....
Dan Villiom Podlaski Christiansen -
r12686:fe31f834 default
parent child Browse files
Show More
@@ -1,160 +1,160 b''
1 # demandimport.py - global demand-loading of modules for Mercurial
1 # demandimport.py - global demand-loading of modules for Mercurial
2 #
2 #
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 '''
8 '''
9 demandimport - automatic demandloading of modules
9 demandimport - automatic demandloading of modules
10
10
11 To enable this module, do:
11 To enable this module, do:
12
12
13 import demandimport; demandimport.enable()
13 import demandimport; demandimport.enable()
14
14
15 Imports of the following forms will be demand-loaded:
15 Imports of the following forms will be demand-loaded:
16
16
17 import a, b.c
17 import a, b.c
18 import a.b as c
18 import a.b as c
19 from a import b,c # a will be loaded immediately
19 from a import b,c # a will be loaded immediately
20
20
21 These imports will not be delayed:
21 These imports will not be delayed:
22
22
23 from a import *
23 from a import *
24 b = __import__(a)
24 b = __import__(a)
25 '''
25 '''
26
26
27 import __builtin__
27 import __builtin__
28 _origimport = __import__
28 _origimport = __import__
29
29
30 class _demandmod(object):
30 class _demandmod(object):
31 """module demand-loader and proxy"""
31 """module demand-loader and proxy"""
32 def __init__(self, name, globals, locals, level):
32 def __init__(self, name, globals, locals, level):
33 if '.' in name:
33 if '.' in name:
34 head, rest = name.split('.', 1)
34 head, rest = name.split('.', 1)
35 after = [rest]
35 after = [rest]
36 else:
36 else:
37 head = name
37 head = name
38 after = []
38 after = []
39 object.__setattr__(self, "_data", (head, globals, locals, after, level))
39 object.__setattr__(self, "_data", (head, globals, locals, after, level))
40 object.__setattr__(self, "_module", None)
40 object.__setattr__(self, "_module", None)
41 def _extend(self, name):
41 def _extend(self, name):
42 """add to the list of submodules to load"""
42 """add to the list of submodules to load"""
43 self._data[3].append(name)
43 self._data[3].append(name)
44 def _load(self):
44 def _load(self):
45 if not self._module:
45 if not self._module:
46 head, globals, locals, after, level = self._data
46 head, globals, locals, after, level = self._data
47 if level is not None:
47 if level is not None:
48 mod = _origimport(head, globals, locals, level)
48 mod = _origimport(head, globals, locals, level=level)
49 else:
49 else:
50 mod = _origimport(head, globals, locals)
50 mod = _origimport(head, globals, locals)
51 # load submodules
51 # load submodules
52 def subload(mod, p):
52 def subload(mod, p):
53 h, t = p, None
53 h, t = p, None
54 if '.' in p:
54 if '.' in p:
55 h, t = p.split('.', 1)
55 h, t = p.split('.', 1)
56 if not hasattr(mod, h):
56 if not hasattr(mod, h):
57 # TODO: should we adjust the level here?
57 # TODO: should we adjust the level here?
58 submod = _demandmod(p, mod.__dict__, mod.__dict__,
58 submod = _demandmod(p, mod.__dict__, mod.__dict__,
59 level=level)
59 level=level)
60 setattr(mod, h, submod)
60 setattr(mod, h, submod)
61 elif t:
61 elif t:
62 subload(getattr(mod, h), t)
62 subload(getattr(mod, h), t)
63
63
64 for x in after:
64 for x in after:
65 subload(mod, x)
65 subload(mod, x)
66
66
67 # are we in the locals dictionary still?
67 # are we in the locals dictionary still?
68 if locals and locals.get(head) == self:
68 if locals and locals.get(head) == self:
69 locals[head] = mod
69 locals[head] = mod
70 object.__setattr__(self, "_module", mod)
70 object.__setattr__(self, "_module", mod)
71
71
72 def __repr__(self):
72 def __repr__(self):
73 if self._module:
73 if self._module:
74 return "<proxied module '%s'>" % self._data[0]
74 return "<proxied module '%s'>" % self._data[0]
75 return "<unloaded module '%s'>" % self._data[0]
75 return "<unloaded module '%s'>" % self._data[0]
76 def __call__(self, *args, **kwargs):
76 def __call__(self, *args, **kwargs):
77 raise TypeError("%s object is not callable" % repr(self))
77 raise TypeError("%s object is not callable" % repr(self))
78 def __getattribute__(self, attr):
78 def __getattribute__(self, attr):
79 if attr in ('_data', '_extend', '_load', '_module'):
79 if attr in ('_data', '_extend', '_load', '_module'):
80 return object.__getattribute__(self, attr)
80 return object.__getattribute__(self, attr)
81 self._load()
81 self._load()
82 return getattr(self._module, attr)
82 return getattr(self._module, attr)
83 def __setattr__(self, attr, val):
83 def __setattr__(self, attr, val):
84 self._load()
84 self._load()
85 setattr(self._module, attr, val)
85 setattr(self._module, attr, val)
86
86
87 def _demandimport(name, globals=None, locals=None, fromlist=None, level=None):
87 def _demandimport(name, globals=None, locals=None, fromlist=None, level=None):
88 if not locals or name in ignore or fromlist == ('*',):
88 if not locals or name in ignore or fromlist == ('*',):
89 # these cases we can't really delay
89 # these cases we can't really delay
90 if level is None:
90 if level is None:
91 return _origimport(name, globals, locals, fromlist)
91 return _origimport(name, globals, locals, fromlist)
92 else:
92 else:
93 return _origimport(name, globals, locals, fromlist, level)
93 return _origimport(name, globals, locals, fromlist, level)
94 elif not fromlist:
94 elif not fromlist:
95 # import a [as b]
95 # import a [as b]
96 if '.' in name: # a.b
96 if '.' in name: # a.b
97 base, rest = name.split('.', 1)
97 base, rest = name.split('.', 1)
98 # email.__init__ loading email.mime
98 # email.__init__ loading email.mime
99 if globals and globals.get('__name__', None) == base:
99 if globals and globals.get('__name__', None) == base:
100 if level is not None:
100 if level is not None:
101 return _origimport(name, globals, locals, fromlist, level)
101 return _origimport(name, globals, locals, fromlist, level)
102 else:
102 else:
103 return _origimport(name, globals, locals, fromlist)
103 return _origimport(name, globals, locals, fromlist)
104 # if a is already demand-loaded, add b to its submodule list
104 # if a is already demand-loaded, add b to its submodule list
105 if base in locals:
105 if base in locals:
106 if isinstance(locals[base], _demandmod):
106 if isinstance(locals[base], _demandmod):
107 locals[base]._extend(rest)
107 locals[base]._extend(rest)
108 return locals[base]
108 return locals[base]
109 return _demandmod(name, globals, locals, level=level)
109 return _demandmod(name, globals, locals, level=level)
110 else:
110 else:
111 # from a import b,c,d
111 # from a import b,c,d
112 if level is not None:
112 if level is not None:
113 mod = _origimport(name, globals, locals, level=level)
113 mod = _origimport(name, globals, locals, level=level)
114 else:
114 else:
115 mod = _origimport(name, globals, locals)
115 mod = _origimport(name, globals, locals)
116 # recurse down the module chain
116 # recurse down the module chain
117 for comp in name.split('.')[1:]:
117 for comp in name.split('.')[1:]:
118 if not hasattr(mod, comp):
118 if not hasattr(mod, comp):
119 # TODO: should we adjust the level here?
119 # TODO: should we adjust the level here?
120 submod = _demandmod(comp, mod.__dict__, mod.__dict__,
120 submod = _demandmod(comp, mod.__dict__, mod.__dict__,
121 level=level)
121 level=level)
122 setattr(mod, comp, submod)
122 setattr(mod, comp, submod)
123 mod = getattr(mod, comp)
123 mod = getattr(mod, comp)
124 for x in fromlist:
124 for x in fromlist:
125 # set requested submodules for demand load
125 # set requested submodules for demand load
126 if not(hasattr(mod, x)):
126 if not(hasattr(mod, x)):
127 # TODO: should we adjust the level here?
127 # TODO: should we adjust the level here?
128 submod = _demandmod(x, mod.__dict__, locals, level=level)
128 submod = _demandmod(x, mod.__dict__, locals, level=level)
129 setattr(mod, x, submod)
129 setattr(mod, x, submod)
130 return mod
130 return mod
131
131
132 ignore = [
132 ignore = [
133 '_hashlib',
133 '_hashlib',
134 '_xmlplus',
134 '_xmlplus',
135 'fcntl',
135 'fcntl',
136 'win32com.gen_py',
136 'win32com.gen_py',
137 '_winreg', # 2.7 mimetypes needs immediate ImportError
137 '_winreg', # 2.7 mimetypes needs immediate ImportError
138 'pythoncom',
138 'pythoncom',
139 # imported by tarfile, not available under Windows
139 # imported by tarfile, not available under Windows
140 'pwd',
140 'pwd',
141 'grp',
141 'grp',
142 # imported by profile, itself imported by hotshot.stats,
142 # imported by profile, itself imported by hotshot.stats,
143 # not available under Windows
143 # not available under Windows
144 'resource',
144 'resource',
145 # this trips up many extension authors
145 # this trips up many extension authors
146 'gtk',
146 'gtk',
147 # setuptools' pkg_resources.py expects "from __main__ import x" to
147 # setuptools' pkg_resources.py expects "from __main__ import x" to
148 # raise ImportError if x not defined
148 # raise ImportError if x not defined
149 '__main__',
149 '__main__',
150 '_ssl', # conditional imports in the stdlib, issue1964
150 '_ssl', # conditional imports in the stdlib, issue1964
151 ]
151 ]
152
152
153 def enable():
153 def enable():
154 "enable global demand-loading of modules"
154 "enable global demand-loading of modules"
155 __builtin__.__import__ = _demandimport
155 __builtin__.__import__ = _demandimport
156
156
157 def disable():
157 def disable():
158 "disable global demand-loading of modules"
158 "disable global demand-loading of modules"
159 __builtin__.__import__ = _origimport
159 __builtin__.__import__ = _origimport
160
160
General Comments 0
You need to be logged in to leave comments. Login now