Show More
@@ -1,208 +1,225 b'' | |||
|
1 | 1 | # demandimport.py - global demand-loading of modules for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | ''' |
|
9 | 9 | demandimport - automatic demandloading of modules |
|
10 | 10 | |
|
11 | 11 | To enable this module, do: |
|
12 | 12 | |
|
13 | 13 | import demandimport; demandimport.enable() |
|
14 | 14 | |
|
15 | 15 | Imports of the following forms will be demand-loaded: |
|
16 | 16 | |
|
17 | 17 | import a, b.c |
|
18 | 18 | import a.b as c |
|
19 | 19 | from a import b,c # a will be loaded immediately |
|
20 | 20 | |
|
21 | 21 | These imports will not be delayed: |
|
22 | 22 | |
|
23 | 23 | from a import * |
|
24 | 24 | b = __import__(a) |
|
25 | 25 | ''' |
|
26 | 26 | |
|
27 | 27 | import os, sys |
|
28 | 28 | from contextlib import contextmanager |
|
29 | 29 | |
|
30 | 30 | # __builtin__ in Python 2, builtins in Python 3. |
|
31 | 31 | try: |
|
32 | 32 | import __builtin__ as builtins |
|
33 | 33 | except ImportError: |
|
34 | 34 | import builtins |
|
35 | 35 | |
|
36 | 36 | _origimport = __import__ |
|
37 | 37 | |
|
38 | 38 | nothing = object() |
|
39 | 39 | |
|
40 | 40 | # Python 3 doesn't have relative imports nor level -1. |
|
41 | 41 | level = -1 |
|
42 | 42 | if sys.version_info[0] >= 3: |
|
43 | 43 | level = 0 |
|
44 | 44 | _import = _origimport |
|
45 | 45 | |
|
46 | 46 | def _hgextimport(importfunc, name, globals, *args, **kwargs): |
|
47 | 47 | try: |
|
48 | 48 | return importfunc(name, globals, *args, **kwargs) |
|
49 | 49 | except ImportError: |
|
50 | 50 | if not globals: |
|
51 | 51 | raise |
|
52 | 52 | # extensions are loaded with "hgext_" prefix |
|
53 | 53 | hgextname = 'hgext_%s' % name |
|
54 | 54 | nameroot = hgextname.split('.', 1)[0] |
|
55 | 55 | contextroot = globals.get('__name__', '').split('.', 1)[0] |
|
56 | 56 | if nameroot != contextroot: |
|
57 | 57 | raise |
|
58 | 58 | # retry to import with "hgext_" prefix |
|
59 | 59 | return importfunc(hgextname, globals, *args, **kwargs) |
|
60 | 60 | |
|
61 | 61 | class _demandmod(object): |
|
62 | 62 | """module demand-loader and proxy""" |
|
63 | 63 | def __init__(self, name, globals, locals, level=level): |
|
64 | 64 | if '.' in name: |
|
65 | 65 | head, rest = name.split('.', 1) |
|
66 | 66 | after = [rest] |
|
67 | 67 | else: |
|
68 | 68 | head = name |
|
69 | 69 | after = [] |
|
70 | 70 | object.__setattr__(self, "_data", |
|
71 | 71 | (head, globals, locals, after, level)) |
|
72 | 72 | object.__setattr__(self, "_module", None) |
|
73 | 73 | def _extend(self, name): |
|
74 | 74 | """add to the list of submodules to load""" |
|
75 | 75 | self._data[3].append(name) |
|
76 | 76 | def _load(self): |
|
77 | 77 | if not self._module: |
|
78 | 78 | head, globals, locals, after, level = self._data |
|
79 | 79 | mod = _hgextimport(_import, head, globals, locals, None, level) |
|
80 | 80 | # load submodules |
|
81 | 81 | def subload(mod, p): |
|
82 | 82 | h, t = p, None |
|
83 | 83 | if '.' in p: |
|
84 | 84 | h, t = p.split('.', 1) |
|
85 | 85 | if getattr(mod, h, nothing) is nothing: |
|
86 | 86 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) |
|
87 | 87 | elif t: |
|
88 | 88 | subload(getattr(mod, h), t) |
|
89 | 89 | |
|
90 | 90 | for x in after: |
|
91 | 91 | subload(mod, x) |
|
92 | 92 | |
|
93 | 93 | # are we in the locals dictionary still? |
|
94 | 94 | if locals and locals.get(head) == self: |
|
95 | 95 | locals[head] = mod |
|
96 | 96 | object.__setattr__(self, "_module", mod) |
|
97 | 97 | |
|
98 | 98 | def __repr__(self): |
|
99 | 99 | if self._module: |
|
100 | 100 | return "<proxied module '%s'>" % self._data[0] |
|
101 | 101 | return "<unloaded module '%s'>" % self._data[0] |
|
102 | 102 | def __call__(self, *args, **kwargs): |
|
103 | 103 | raise TypeError("%s object is not callable" % repr(self)) |
|
104 | 104 | def __getattribute__(self, attr): |
|
105 | 105 | if attr in ('_data', '_extend', '_load', '_module'): |
|
106 | 106 | return object.__getattribute__(self, attr) |
|
107 | 107 | self._load() |
|
108 | 108 | return getattr(self._module, attr) |
|
109 | 109 | def __setattr__(self, attr, val): |
|
110 | 110 | self._load() |
|
111 | 111 | setattr(self._module, attr, val) |
|
112 | 112 | |
|
113 | 113 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=level): |
|
114 | 114 | if not locals or name in ignore or fromlist == ('*',): |
|
115 | 115 | # these cases we can't really delay |
|
116 | 116 | return _hgextimport(_import, name, globals, locals, fromlist, level) |
|
117 | 117 | elif not fromlist: |
|
118 | 118 | # import a [as b] |
|
119 | 119 | if '.' in name: # a.b |
|
120 | 120 | base, rest = name.split('.', 1) |
|
121 | 121 | # email.__init__ loading email.mime |
|
122 | 122 | if globals and globals.get('__name__', None) == base: |
|
123 | 123 | return _import(name, globals, locals, fromlist, level) |
|
124 | 124 | # if a is already demand-loaded, add b to its submodule list |
|
125 | 125 | if base in locals: |
|
126 | 126 | if isinstance(locals[base], _demandmod): |
|
127 | 127 | locals[base]._extend(rest) |
|
128 | 128 | return locals[base] |
|
129 | 129 | return _demandmod(name, globals, locals, level) |
|
130 | 130 | else: |
|
131 | 131 | # There is a fromlist. |
|
132 | 132 | # from a import b,c,d |
|
133 | 133 | # from . import b,c,d |
|
134 | 134 | # from .a import b,c,d |
|
135 | 135 | |
|
136 | 136 | # level == -1: relative and absolute attempted (Python 2 only). |
|
137 | 137 | # level >= 0: absolute only (Python 2 w/ absolute_import and Python 3). |
|
138 | 138 | # The modern Mercurial convention is to use absolute_import everywhere, |
|
139 | 139 | # so modern Mercurial code will have level >= 0. |
|
140 | 140 | |
|
141 | 141 | if level >= 0: |
|
142 | return _origimport(name, globals, locals, fromlist, level) | |
|
142 | # Mercurial's enforced import style does not use | |
|
143 | # "from a import b,c,d" or "from .a import b,c,d" syntax. In | |
|
144 | # addition, this appears to be giving errors with some modules | |
|
145 | # for unknown reasons. Since we shouldn't be using this syntax | |
|
146 | # much, work around the problems. | |
|
147 | if name: | |
|
148 | return _hgextimport(_origimport, name, globals, locals, | |
|
149 | fromlist, level) | |
|
150 | ||
|
151 | mod = _hgextimport(_origimport, name, globals, locals, level=level) | |
|
152 | for x in fromlist: | |
|
153 | # Missing symbols mean they weren't defined in the module | |
|
154 | # itself which means they are sub-modules. | |
|
155 | if getattr(mod, x, nothing) is nothing: | |
|
156 | setattr(mod, x, | |
|
157 | _demandmod(x, mod.__dict__, locals, level=level)) | |
|
158 | ||
|
159 | return mod | |
|
143 | 160 | |
|
144 | 161 | # But, we still need to support lazy loading of standard library and 3rd |
|
145 | 162 | # party modules. So handle level == -1. |
|
146 | 163 | mod = _hgextimport(_origimport, name, globals, locals) |
|
147 | 164 | # recurse down the module chain |
|
148 | 165 | for comp in name.split('.')[1:]: |
|
149 | 166 | if getattr(mod, comp, nothing) is nothing: |
|
150 | 167 | setattr(mod, comp, |
|
151 | 168 | _demandmod(comp, mod.__dict__, mod.__dict__)) |
|
152 | 169 | mod = getattr(mod, comp) |
|
153 | 170 | for x in fromlist: |
|
154 | 171 | # set requested submodules for demand load |
|
155 | 172 | if getattr(mod, x, nothing) is nothing: |
|
156 | 173 | setattr(mod, x, _demandmod(x, mod.__dict__, locals)) |
|
157 | 174 | return mod |
|
158 | 175 | |
|
159 | 176 | ignore = [ |
|
160 | 177 | '__future__', |
|
161 | 178 | '_hashlib', |
|
162 | 179 | '_xmlplus', |
|
163 | 180 | 'fcntl', |
|
164 | 181 | 'win32com.gen_py', |
|
165 | 182 | '_winreg', # 2.7 mimetypes needs immediate ImportError |
|
166 | 183 | 'pythoncom', |
|
167 | 184 | # imported by tarfile, not available under Windows |
|
168 | 185 | 'pwd', |
|
169 | 186 | 'grp', |
|
170 | 187 | # imported by profile, itself imported by hotshot.stats, |
|
171 | 188 | # not available under Windows |
|
172 | 189 | 'resource', |
|
173 | 190 | # this trips up many extension authors |
|
174 | 191 | 'gtk', |
|
175 | 192 | # setuptools' pkg_resources.py expects "from __main__ import x" to |
|
176 | 193 | # raise ImportError if x not defined |
|
177 | 194 | '__main__', |
|
178 | 195 | '_ssl', # conditional imports in the stdlib, issue1964 |
|
179 | 196 | 'rfc822', |
|
180 | 197 | 'mimetools', |
|
181 | 198 | # setuptools 8 expects this module to explode early when not on windows |
|
182 | 199 | 'distutils.msvc9compiler' |
|
183 | 200 | ] |
|
184 | 201 | |
|
185 | 202 | def isenabled(): |
|
186 | 203 | return builtins.__import__ == _demandimport |
|
187 | 204 | |
|
188 | 205 | def enable(): |
|
189 | 206 | "enable global demand-loading of modules" |
|
190 | 207 | if os.environ.get('HGDEMANDIMPORT') != 'disable': |
|
191 | 208 | builtins.__import__ = _demandimport |
|
192 | 209 | |
|
193 | 210 | def disable(): |
|
194 | 211 | "disable global demand-loading of modules" |
|
195 | 212 | builtins.__import__ = _origimport |
|
196 | 213 | |
|
197 | 214 | @contextmanager |
|
198 | 215 | def deactivated(): |
|
199 | 216 | "context manager for disabling demandimport in 'with' blocks" |
|
200 | 217 | demandenabled = isenabled() |
|
201 | 218 | if demandenabled: |
|
202 | 219 | disable() |
|
203 | 220 | |
|
204 | 221 | try: |
|
205 | 222 | yield |
|
206 | 223 | finally: |
|
207 | 224 | if demandenabled: |
|
208 | 225 | enable() |
General Comments 0
You need to be logged in to leave comments.
Login now