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