Show More
@@ -1,195 +1,196 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 os, sys |
|
27 | import os, sys | |
28 | from contextlib import contextmanager |
|
28 | from contextlib import contextmanager | |
29 |
|
29 | |||
30 | # __builtin__ in Python 2, builtins in Python 3. |
|
30 | # __builtin__ in Python 2, builtins in Python 3. | |
31 | try: |
|
31 | try: | |
32 | import __builtin__ as builtins |
|
32 | import __builtin__ as builtins | |
33 | except ImportError: |
|
33 | except ImportError: | |
34 | import builtins |
|
34 | import builtins | |
35 |
|
35 | |||
36 | _origimport = __import__ |
|
36 | _origimport = __import__ | |
37 |
|
37 | |||
38 | nothing = object() |
|
38 | nothing = object() | |
39 |
|
39 | |||
40 | # Python 3 doesn't have relative imports nor level -1. |
|
40 | # Python 3 doesn't have relative imports nor level -1. | |
41 | level = -1 |
|
41 | level = -1 | |
42 | if sys.version_info[0] >= 3: |
|
42 | if sys.version_info[0] >= 3: | |
43 | level = 0 |
|
43 | level = 0 | |
44 | _import = _origimport |
|
44 | _import = _origimport | |
45 |
|
45 | |||
46 | def _hgextimport(importfunc, name, globals, *args): |
|
46 | def _hgextimport(importfunc, name, globals, *args): | |
47 | try: |
|
47 | try: | |
48 | return importfunc(name, globals, *args) |
|
48 | return importfunc(name, globals, *args) | |
49 | except ImportError: |
|
49 | except ImportError: | |
50 | if not globals: |
|
50 | if not globals: | |
51 | raise |
|
51 | raise | |
52 | # extensions are loaded with "hgext_" prefix |
|
52 | # extensions are loaded with "hgext_" prefix | |
53 | hgextname = 'hgext_%s' % name |
|
53 | hgextname = 'hgext_%s' % name | |
54 | nameroot = hgextname.split('.', 1)[0] |
|
54 | nameroot = hgextname.split('.', 1)[0] | |
55 | contextroot = globals.get('__name__', '').split('.', 1)[0] |
|
55 | contextroot = globals.get('__name__', '').split('.', 1)[0] | |
56 | if nameroot != contextroot: |
|
56 | if nameroot != contextroot: | |
57 | raise |
|
57 | raise | |
58 | # retry to import with "hgext_" prefix |
|
58 | # retry to import with "hgext_" prefix | |
59 | return importfunc(hgextname, globals, *args) |
|
59 | return importfunc(hgextname, globals, *args) | |
60 |
|
60 | |||
61 | class _demandmod(object): |
|
61 | class _demandmod(object): | |
62 | """module demand-loader and proxy""" |
|
62 | """module demand-loader and proxy""" | |
63 | def __init__(self, name, globals, locals, level=level): |
|
63 | def __init__(self, name, globals, locals, level=level): | |
64 | if '.' in name: |
|
64 | if '.' in name: | |
65 | head, rest = name.split('.', 1) |
|
65 | head, rest = name.split('.', 1) | |
66 | after = [rest] |
|
66 | after = [rest] | |
67 | else: |
|
67 | else: | |
68 | head = name |
|
68 | head = name | |
69 | after = [] |
|
69 | after = [] | |
70 | object.__setattr__(self, "_data", |
|
70 | object.__setattr__(self, "_data", | |
71 | (head, globals, locals, after, level)) |
|
71 | (head, globals, locals, after, level)) | |
72 | object.__setattr__(self, "_module", None) |
|
72 | object.__setattr__(self, "_module", None) | |
73 | def _extend(self, name): |
|
73 | def _extend(self, name): | |
74 | """add to the list of submodules to load""" |
|
74 | """add to the list of submodules to load""" | |
75 | self._data[3].append(name) |
|
75 | self._data[3].append(name) | |
76 | def _load(self): |
|
76 | def _load(self): | |
77 | if not self._module: |
|
77 | if not self._module: | |
78 | head, globals, locals, after, level = self._data |
|
78 | head, globals, locals, after, level = self._data | |
79 | mod = _hgextimport(_import, head, globals, locals, None, level) |
|
79 | mod = _hgextimport(_import, head, globals, locals, None, level) | |
80 | # load submodules |
|
80 | # load submodules | |
81 | def subload(mod, p): |
|
81 | def subload(mod, p): | |
82 | h, t = p, None |
|
82 | h, t = p, None | |
83 | if '.' in p: |
|
83 | if '.' in p: | |
84 | h, t = p.split('.', 1) |
|
84 | h, t = p.split('.', 1) | |
85 | if getattr(mod, h, nothing) is nothing: |
|
85 | if getattr(mod, h, nothing) is nothing: | |
86 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) |
|
86 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) | |
87 | elif t: |
|
87 | elif t: | |
88 | subload(getattr(mod, h), t) |
|
88 | subload(getattr(mod, h), t) | |
89 |
|
89 | |||
90 | for x in after: |
|
90 | for x in after: | |
91 | subload(mod, x) |
|
91 | subload(mod, x) | |
92 |
|
92 | |||
93 | # are we in the locals dictionary still? |
|
93 | # are we in the locals dictionary still? | |
94 | if locals and locals.get(head) == self: |
|
94 | if locals and locals.get(head) == self: | |
95 | locals[head] = mod |
|
95 | locals[head] = mod | |
96 | object.__setattr__(self, "_module", mod) |
|
96 | object.__setattr__(self, "_module", mod) | |
97 |
|
97 | |||
98 | def __repr__(self): |
|
98 | def __repr__(self): | |
99 | if self._module: |
|
99 | if self._module: | |
100 | return "<proxied module '%s'>" % self._data[0] |
|
100 | return "<proxied module '%s'>" % self._data[0] | |
101 | return "<unloaded module '%s'>" % self._data[0] |
|
101 | return "<unloaded module '%s'>" % self._data[0] | |
102 | def __call__(self, *args, **kwargs): |
|
102 | def __call__(self, *args, **kwargs): | |
103 | raise TypeError("%s object is not callable" % repr(self)) |
|
103 | raise TypeError("%s object is not callable" % repr(self)) | |
104 | def __getattribute__(self, attr): |
|
104 | def __getattribute__(self, attr): | |
105 | if attr in ('_data', '_extend', '_load', '_module'): |
|
105 | if attr in ('_data', '_extend', '_load', '_module'): | |
106 | return object.__getattribute__(self, attr) |
|
106 | return object.__getattribute__(self, attr) | |
107 | self._load() |
|
107 | self._load() | |
108 | return getattr(self._module, attr) |
|
108 | return getattr(self._module, attr) | |
109 | def __setattr__(self, attr, val): |
|
109 | def __setattr__(self, attr, val): | |
110 | self._load() |
|
110 | self._load() | |
111 | setattr(self._module, attr, val) |
|
111 | setattr(self._module, attr, val) | |
112 |
|
112 | |||
113 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=level): |
|
113 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=level): | |
114 | if not locals or name in ignore or fromlist == ('*',): |
|
114 | if not locals or name in ignore or fromlist == ('*',): | |
115 | # these cases we can't really delay |
|
115 | # these cases we can't really delay | |
116 | return _hgextimport(_import, name, globals, locals, fromlist, level) |
|
116 | return _hgextimport(_import, name, globals, locals, fromlist, level) | |
117 | elif not fromlist: |
|
117 | elif not fromlist: | |
118 | # import a [as b] |
|
118 | # import a [as b] | |
119 | if '.' in name: # a.b |
|
119 | if '.' in name: # a.b | |
120 | base, rest = name.split('.', 1) |
|
120 | base, rest = name.split('.', 1) | |
121 | # email.__init__ loading email.mime |
|
121 | # email.__init__ loading email.mime | |
122 | if globals and globals.get('__name__', None) == base: |
|
122 | if globals and globals.get('__name__', None) == base: | |
123 | return _import(name, globals, locals, fromlist, level) |
|
123 | return _import(name, globals, locals, fromlist, level) | |
124 | # if a is already demand-loaded, add b to its submodule list |
|
124 | # if a is already demand-loaded, add b to its submodule list | |
125 | if base in locals: |
|
125 | if base in locals: | |
126 | if isinstance(locals[base], _demandmod): |
|
126 | if isinstance(locals[base], _demandmod): | |
127 | locals[base]._extend(rest) |
|
127 | locals[base]._extend(rest) | |
128 | return locals[base] |
|
128 | return locals[base] | |
129 | return _demandmod(name, globals, locals, level) |
|
129 | return _demandmod(name, globals, locals, level) | |
130 | else: |
|
130 | else: | |
131 | if level != -1: |
|
131 | if level != -1: | |
132 | # from . import b,c,d or from .a import b,c,d |
|
132 | # from . import b,c,d or from .a import b,c,d | |
133 | return _origimport(name, globals, locals, fromlist, level) |
|
133 | return _origimport(name, globals, locals, fromlist, level) | |
134 | # from a import b,c,d |
|
134 | # from a import b,c,d | |
135 | mod = _hgextimport(_origimport, name, globals, locals) |
|
135 | mod = _hgextimport(_origimport, name, globals, locals) | |
136 | # recurse down the module chain |
|
136 | # recurse down the module chain | |
137 | for comp in name.split('.')[1:]: |
|
137 | for comp in name.split('.')[1:]: | |
138 | if getattr(mod, comp, nothing) is nothing: |
|
138 | if getattr(mod, comp, nothing) is nothing: | |
139 | setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__)) |
|
139 | setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__)) | |
140 | mod = getattr(mod, comp) |
|
140 | mod = getattr(mod, comp) | |
141 | for x in fromlist: |
|
141 | for x in fromlist: | |
142 | # set requested submodules for demand load |
|
142 | # set requested submodules for demand load | |
143 | if getattr(mod, x, nothing) is nothing: |
|
143 | if getattr(mod, x, nothing) is nothing: | |
144 | setattr(mod, x, _demandmod(x, mod.__dict__, locals)) |
|
144 | setattr(mod, x, _demandmod(x, mod.__dict__, locals)) | |
145 | return mod |
|
145 | return mod | |
146 |
|
146 | |||
147 | ignore = [ |
|
147 | ignore = [ | |
|
148 | '__future__', | |||
148 | '_hashlib', |
|
149 | '_hashlib', | |
149 | '_xmlplus', |
|
150 | '_xmlplus', | |
150 | 'fcntl', |
|
151 | 'fcntl', | |
151 | 'win32com.gen_py', |
|
152 | 'win32com.gen_py', | |
152 | '_winreg', # 2.7 mimetypes needs immediate ImportError |
|
153 | '_winreg', # 2.7 mimetypes needs immediate ImportError | |
153 | 'pythoncom', |
|
154 | 'pythoncom', | |
154 | # imported by tarfile, not available under Windows |
|
155 | # imported by tarfile, not available under Windows | |
155 | 'pwd', |
|
156 | 'pwd', | |
156 | 'grp', |
|
157 | 'grp', | |
157 | # imported by profile, itself imported by hotshot.stats, |
|
158 | # imported by profile, itself imported by hotshot.stats, | |
158 | # not available under Windows |
|
159 | # not available under Windows | |
159 | 'resource', |
|
160 | 'resource', | |
160 | # this trips up many extension authors |
|
161 | # this trips up many extension authors | |
161 | 'gtk', |
|
162 | 'gtk', | |
162 | # setuptools' pkg_resources.py expects "from __main__ import x" to |
|
163 | # setuptools' pkg_resources.py expects "from __main__ import x" to | |
163 | # raise ImportError if x not defined |
|
164 | # raise ImportError if x not defined | |
164 | '__main__', |
|
165 | '__main__', | |
165 | '_ssl', # conditional imports in the stdlib, issue1964 |
|
166 | '_ssl', # conditional imports in the stdlib, issue1964 | |
166 | 'rfc822', |
|
167 | 'rfc822', | |
167 | 'mimetools', |
|
168 | 'mimetools', | |
168 | # setuptools 8 expects this module to explode early when not on windows |
|
169 | # setuptools 8 expects this module to explode early when not on windows | |
169 | 'distutils.msvc9compiler' |
|
170 | 'distutils.msvc9compiler' | |
170 | ] |
|
171 | ] | |
171 |
|
172 | |||
172 | def isenabled(): |
|
173 | def isenabled(): | |
173 | return builtins.__import__ == _demandimport |
|
174 | return builtins.__import__ == _demandimport | |
174 |
|
175 | |||
175 | def enable(): |
|
176 | def enable(): | |
176 | "enable global demand-loading of modules" |
|
177 | "enable global demand-loading of modules" | |
177 | if os.environ.get('HGDEMANDIMPORT') != 'disable': |
|
178 | if os.environ.get('HGDEMANDIMPORT') != 'disable': | |
178 | builtins.__import__ = _demandimport |
|
179 | builtins.__import__ = _demandimport | |
179 |
|
180 | |||
180 | def disable(): |
|
181 | def disable(): | |
181 | "disable global demand-loading of modules" |
|
182 | "disable global demand-loading of modules" | |
182 | builtins.__import__ = _origimport |
|
183 | builtins.__import__ = _origimport | |
183 |
|
184 | |||
184 | @contextmanager |
|
185 | @contextmanager | |
185 | def deactivated(): |
|
186 | def deactivated(): | |
186 | "context manager for disabling demandimport in 'with' blocks" |
|
187 | "context manager for disabling demandimport in 'with' blocks" | |
187 | demandenabled = isenabled() |
|
188 | demandenabled = isenabled() | |
188 | if demandenabled: |
|
189 | if demandenabled: | |
189 | disable() |
|
190 | disable() | |
190 |
|
191 | |||
191 | try: |
|
192 | try: | |
192 | yield |
|
193 | yield | |
193 | finally: |
|
194 | finally: | |
194 | if demandenabled: |
|
195 | if demandenabled: | |
195 | enable() |
|
196 | enable() |
General Comments 0
You need to be logged in to leave comments.
Login now