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