##// END OF EJS Templates
demandload: give better diagnostic for call of an unloaded module
Matt Mackall -
r5639:7dd5cf9d default
parent child Browse files
Show More
@@ -1,131 +1,131
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
6 6 # of the GNU General Public License, incorporated herein by reference.
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 _origimport = __import__
28 28
29 29 class _demandmod(object):
30 30 """module demand-loader and proxy"""
31 31 def __init__(self, name, globals, locals):
32 32 if '.' in name:
33 33 head, rest = name.split('.', 1)
34 34 after = [rest]
35 35 else:
36 36 head = name
37 37 after = []
38 38 object.__setattr__(self, "_data", (head, globals, locals, after))
39 39 object.__setattr__(self, "_module", None)
40 40 def _extend(self, name):
41 41 """add to the list of submodules to load"""
42 42 self._data[3].append(name)
43 43 def _load(self):
44 44 if not self._module:
45 45 head, globals, locals, after = self._data
46 46 mod = _origimport(head, globals, locals)
47 47 # load submodules
48 48 def subload(mod, p):
49 49 h, t = p, None
50 50 if '.' in p:
51 51 h, t = p.split('.', 1)
52 52 if not hasattr(mod, h):
53 53 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
54 54 elif t:
55 55 subload(getattr(mod, h), t)
56 56
57 57 for x in after:
58 58 subload(mod, x)
59 59
60 60 # are we in the locals dictionary still?
61 61 if locals and locals.get(head) == self:
62 62 locals[head] = mod
63 63 object.__setattr__(self, "_module", mod)
64 64
65 65 def __repr__(self):
66 66 if self._module:
67 67 return "<proxied module '%s'>" % self._data[0]
68 68 return "<unloaded module '%s'>" % self._data[0]
69 69 def __call__(self, *args, **kwargs):
70 raise TypeError("'unloaded module' object is not callable")
70 raise TypeError("%s object is not callable" % repr(self))
71 71 def __getattribute__(self, attr):
72 72 if attr in ('_data', '_extend', '_load', '_module'):
73 73 return object.__getattribute__(self, attr)
74 74 self._load()
75 75 return getattr(self._module, attr)
76 76 def __setattr__(self, attr, val):
77 77 self._load()
78 78 setattr(self._module, attr, val)
79 79
80 80 def _demandimport(name, globals=None, locals=None, fromlist=None):
81 81 if not locals or name in ignore or fromlist == ('*',):
82 82 # these cases we can't really delay
83 83 return _origimport(name, globals, locals, fromlist)
84 84 elif not fromlist:
85 85 # import a [as b]
86 86 if '.' in name: # a.b
87 87 base, rest = name.split('.', 1)
88 88 # email.__init__ loading email.mime
89 89 if globals and globals.get('__name__', None) == base:
90 90 return _origimport(name, globals, locals, fromlist)
91 91 # if a is already demand-loaded, add b to its submodule list
92 92 if base in locals:
93 93 if isinstance(locals[base], _demandmod):
94 94 locals[base]._extend(rest)
95 95 return locals[base]
96 96 return _demandmod(name, globals, locals)
97 97 else:
98 98 # from a import b,c,d
99 99 mod = _origimport(name, globals, locals)
100 100 # recurse down the module chain
101 101 for comp in name.split('.')[1:]:
102 102 if not hasattr(mod, comp):
103 103 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
104 104 mod = getattr(mod, comp)
105 105 for x in fromlist:
106 106 # set requested submodules for demand load
107 107 if not(hasattr(mod, x)):
108 108 setattr(mod, x, _demandmod(x, mod.__dict__, locals))
109 109 return mod
110 110
111 111 ignore = [
112 112 '_hashlib',
113 113 '_xmlplus',
114 114 'fcntl',
115 115 'win32com.gen_py',
116 116 # imported by tarfile, not available under Windows
117 117 'pwd',
118 118 'grp',
119 119 # imported by profile, itself imported by hotshot.stats,
120 120 # not available under Windows
121 121 'resource',
122 122 ]
123 123
124 124 def enable():
125 125 "enable global demand-loading of modules"
126 126 __builtins__["__import__"] = _demandimport
127 127
128 128 def disable():
129 129 "disable global demand-loading of modules"
130 130 __builtins__["__import__"] = _origimport
131 131
General Comments 0
You need to be logged in to leave comments. Login now