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