##// END OF EJS Templates
demandimport: blacklist rfc822 and mimetools to prevent spurious warnings
Augie Fackler -
r14976:04a950b1 default
parent child Browse files
Show More
@@ -1,149 +1,151 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):
32 def __init__(self, name, globals, locals):
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))
39 object.__setattr__(self, "_data", (head, globals, locals, after))
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 = self._data
46 head, globals, locals, after = self._data
47 mod = _origimport(head, globals, locals)
47 mod = _origimport(head, globals, locals)
48 # load submodules
48 # load submodules
49 def subload(mod, p):
49 def subload(mod, p):
50 h, t = p, None
50 h, t = p, None
51 if '.' in p:
51 if '.' in p:
52 h, t = p.split('.', 1)
52 h, t = p.split('.', 1)
53 if not hasattr(mod, h):
53 if not hasattr(mod, h):
54 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
54 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
55 elif t:
55 elif t:
56 subload(getattr(mod, h), t)
56 subload(getattr(mod, h), t)
57
57
58 for x in after:
58 for x in after:
59 subload(mod, x)
59 subload(mod, x)
60
60
61 # are we in the locals dictionary still?
61 # are we in the locals dictionary still?
62 if locals and locals.get(head) == self:
62 if locals and locals.get(head) == self:
63 locals[head] = mod
63 locals[head] = mod
64 object.__setattr__(self, "_module", mod)
64 object.__setattr__(self, "_module", mod)
65
65
66 def __repr__(self):
66 def __repr__(self):
67 if self._module:
67 if self._module:
68 return "<proxied module '%s'>" % self._data[0]
68 return "<proxied module '%s'>" % self._data[0]
69 return "<unloaded module '%s'>" % self._data[0]
69 return "<unloaded module '%s'>" % self._data[0]
70 def __call__(self, *args, **kwargs):
70 def __call__(self, *args, **kwargs):
71 raise TypeError("%s object is not callable" % repr(self))
71 raise TypeError("%s object is not callable" % repr(self))
72 def __getattribute__(self, attr):
72 def __getattribute__(self, attr):
73 if attr in ('_data', '_extend', '_load', '_module'):
73 if attr in ('_data', '_extend', '_load', '_module'):
74 return object.__getattribute__(self, attr)
74 return object.__getattribute__(self, attr)
75 self._load()
75 self._load()
76 return getattr(self._module, attr)
76 return getattr(self._module, attr)
77 def __setattr__(self, attr, val):
77 def __setattr__(self, attr, val):
78 self._load()
78 self._load()
79 setattr(self._module, attr, val)
79 setattr(self._module, attr, val)
80
80
81 def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
81 def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
82 if not locals or name in ignore or fromlist == ('*',):
82 if not locals or name in ignore or fromlist == ('*',):
83 # these cases we can't really delay
83 # these cases we can't really delay
84 if level == -1:
84 if level == -1:
85 return _origimport(name, globals, locals, fromlist)
85 return _origimport(name, globals, locals, fromlist)
86 else:
86 else:
87 return _origimport(name, globals, locals, fromlist, level)
87 return _origimport(name, globals, locals, fromlist, level)
88 elif not fromlist:
88 elif not fromlist:
89 # import a [as b]
89 # import a [as b]
90 if '.' in name: # a.b
90 if '.' in name: # a.b
91 base, rest = name.split('.', 1)
91 base, rest = name.split('.', 1)
92 # email.__init__ loading email.mime
92 # email.__init__ loading email.mime
93 if globals and globals.get('__name__', None) == base:
93 if globals and globals.get('__name__', None) == base:
94 if level != -1:
94 if level != -1:
95 return _origimport(name, globals, locals, fromlist, level)
95 return _origimport(name, globals, locals, fromlist, level)
96 else:
96 else:
97 return _origimport(name, globals, locals, fromlist)
97 return _origimport(name, globals, locals, fromlist)
98 # if a is already demand-loaded, add b to its submodule list
98 # if a is already demand-loaded, add b to its submodule list
99 if base in locals:
99 if base in locals:
100 if isinstance(locals[base], _demandmod):
100 if isinstance(locals[base], _demandmod):
101 locals[base]._extend(rest)
101 locals[base]._extend(rest)
102 return locals[base]
102 return locals[base]
103 return _demandmod(name, globals, locals)
103 return _demandmod(name, globals, locals)
104 else:
104 else:
105 if level != -1:
105 if level != -1:
106 # from . import b,c,d or from .a import b,c,d
106 # from . import b,c,d or from .a import b,c,d
107 return _origimport(name, globals, locals, fromlist, level)
107 return _origimport(name, globals, locals, fromlist, level)
108 # from a import b,c,d
108 # from a import b,c,d
109 mod = _origimport(name, globals, locals)
109 mod = _origimport(name, globals, locals)
110 # recurse down the module chain
110 # recurse down the module chain
111 for comp in name.split('.')[1:]:
111 for comp in name.split('.')[1:]:
112 if not hasattr(mod, comp):
112 if not hasattr(mod, comp):
113 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
113 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
114 mod = getattr(mod, comp)
114 mod = getattr(mod, comp)
115 for x in fromlist:
115 for x in fromlist:
116 # set requested submodules for demand load
116 # set requested submodules for demand load
117 if not hasattr(mod, x):
117 if not hasattr(mod, x):
118 setattr(mod, x, _demandmod(x, mod.__dict__, locals))
118 setattr(mod, x, _demandmod(x, mod.__dict__, locals))
119 return mod
119 return mod
120
120
121 ignore = [
121 ignore = [
122 '_hashlib',
122 '_hashlib',
123 '_xmlplus',
123 '_xmlplus',
124 'fcntl',
124 'fcntl',
125 'win32com.gen_py',
125 'win32com.gen_py',
126 '_winreg', # 2.7 mimetypes needs immediate ImportError
126 '_winreg', # 2.7 mimetypes needs immediate ImportError
127 'pythoncom',
127 'pythoncom',
128 # imported by tarfile, not available under Windows
128 # imported by tarfile, not available under Windows
129 'pwd',
129 'pwd',
130 'grp',
130 'grp',
131 # imported by profile, itself imported by hotshot.stats,
131 # imported by profile, itself imported by hotshot.stats,
132 # not available under Windows
132 # not available under Windows
133 'resource',
133 'resource',
134 # this trips up many extension authors
134 # this trips up many extension authors
135 'gtk',
135 'gtk',
136 # setuptools' pkg_resources.py expects "from __main__ import x" to
136 # setuptools' pkg_resources.py expects "from __main__ import x" to
137 # raise ImportError if x not defined
137 # raise ImportError if x not defined
138 '__main__',
138 '__main__',
139 '_ssl', # conditional imports in the stdlib, issue1964
139 '_ssl', # conditional imports in the stdlib, issue1964
140 'rfc822',
141 'mimetools',
140 ]
142 ]
141
143
142 def enable():
144 def enable():
143 "enable global demand-loading of modules"
145 "enable global demand-loading of modules"
144 __builtin__.__import__ = _demandimport
146 __builtin__.__import__ = _demandimport
145
147
146 def disable():
148 def disable():
147 "disable global demand-loading of modules"
149 "disable global demand-loading of modules"
148 __builtin__.__import__ = _origimport
150 __builtin__.__import__ = _origimport
149
151
General Comments 0
You need to be logged in to leave comments. Login now