##// END OF EJS Templates
demandimport: fix issue579 and add a test...
Matt Mackall -
r4631:e3afa670 default
parent child Browse files
Show More
@@ -0,0 +1,47 b''
1 #!/usr/bin/env python
2
3 from mercurial import demandimport
4 demandimport.enable()
5
6 import re
7
8 rsub = re.sub
9 def f(obj):
10 l = repr(obj)
11 l = rsub("0x[0-9a-f]+", "0x?", l)
12 l = rsub("from '.*'", "from '?'", l)
13 return l
14
15 import os
16
17 print "os =", f(os)
18 print "os.system =", f(os.system)
19 print "os =", f(os)
20
21 import mercurial.version
22
23 print "mercurial.version =", f(mercurial.version)
24 print "mercurial.version.get_version =", f(mercurial.version.get_version)
25 print "mercurial.version =", f(mercurial.version)
26 print "mercurial =", f(mercurial)
27
28 from mercurial import util
29
30 print "util =", f(util)
31 print "util.system =", f(util.system)
32 print "util =", f(util)
33 print "util.system =", f(util.system)
34
35 import re as fred
36 print "fred =", f(fred)
37
38 import sys as re
39 print "re =", f(re)
40
41 print "fred =", f(fred)
42 print "fred.sub =", f(fred.sub)
43 print "fred =", f(fred)
44
45 print "re =", f(re)
46 print "re.stdout =", f(re.stdout)
47 print "re =", f(re)
@@ -0,0 +1,19 b''
1 os = <unloaded module 'os'>
2 os.system = <built-in function system>
3 os = <module 'os' from '?'>
4 mercurial.version = <unloaded module 'version'>
5 mercurial.version.get_version = <function get_version at 0x?>
6 mercurial.version = <module 'mercurial.version' from '?'>
7 mercurial = <module 'mercurial' from '?'>
8 util = <unloaded module 'util'>
9 util.system = <function system at 0x?>
10 util = <module 'mercurial.util' from '?'>
11 util.system = <function system at 0x?>
12 fred = <unloaded module 're'>
13 re = <unloaded module 'sys'>
14 fred = <unloaded module 're'>
15 fred.sub = <function sub at 0x?>
16 fred = <proxied module 're'>
17 re = <unloaded module 'sys'>
18 re.stdout = <open file '<stdout>', mode 'w' at 0x?>
19 re = <proxied module 'sys'>
@@ -1,117 +1,120 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 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
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 _origimport = __import__
27 _origimport = __import__
28
28
29 class _demandmod(object):
29 class _demandmod(object):
30 """module demand-loader and proxy"""
30 """module demand-loader and proxy"""
31 def __init__(self, name, globals, locals):
31 def __init__(self, name, globals, locals):
32 if '.' in name:
32 if '.' in name:
33 head, rest = name.split('.', 1)
33 head, rest = name.split('.', 1)
34 after = [rest]
34 after = [rest]
35 else:
35 else:
36 head = name
36 head = name
37 after = []
37 after = []
38 object.__setattr__(self, "_data", (head, globals, locals, after))
38 object.__setattr__(self, "_data", (head, globals, locals, after))
39 object.__setattr__(self, "_module", None)
39 object.__setattr__(self, "_module", None)
40 def _extend(self, name):
40 def _extend(self, name):
41 """add to the list of submodules to load"""
41 """add to the list of submodules to load"""
42 self._data[3].append(name)
42 self._data[3].append(name)
43 def _load(self):
43 def _load(self):
44 if not self._module:
44 if not self._module:
45 head, globals, locals, after = self._data
45 head, globals, locals, after = self._data
46 mod = _origimport(head, globals, locals)
46 mod = _origimport(head, globals, locals)
47 # load submodules
47 # load submodules
48 def subload(mod, p):
48 def subload(mod, p):
49 h, t = p, None
49 h, t = p, None
50 if '.' in p:
50 if '.' in p:
51 h, t = p.split('.', 1)
51 h, t = p.split('.', 1)
52 if not hasattr(mod, h):
52 if not hasattr(mod, h):
53 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
53 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
54 elif t:
54 elif t:
55 subload(getattr(mod, h), t)
55 subload(getattr(mod, h), t)
56
56
57 for x in after:
57 for x in after:
58 subload(mod, x)
58 subload(mod, x)
59
59
60 # are we in the locals dictionary still?
60 # are we in the locals dictionary still?
61 if locals and locals.get(head) == self:
61 if locals and locals.get(head) == self:
62 locals[head] = mod
62 locals[head] = mod
63 object.__setattr__(self, "_module", mod)
63 object.__setattr__(self, "_module", mod)
64
64 def __repr__(self):
65 def __repr__(self):
66 if self._module:
67 return "<proxied module '%s'>" % self._data[0]
65 return "<unloaded module '%s'>" % self._data[0]
68 return "<unloaded module '%s'>" % self._data[0]
66 def __call__(self, *args, **kwargs):
69 def __call__(self, *args, **kwargs):
67 raise TypeError("'unloaded module' object is not callable")
70 raise TypeError("'unloaded module' object is not callable")
68 def __getattribute__(self, attr):
71 def __getattribute__(self, attr):
69 if attr in ('_data', '_extend', '_load', '_module'):
72 if attr in ('_data', '_extend', '_load', '_module'):
70 return object.__getattribute__(self, attr)
73 return object.__getattribute__(self, attr)
71 self._load()
74 self._load()
72 return getattr(self._module, attr)
75 return getattr(self._module, attr)
73 def __setattr__(self, attr, val):
76 def __setattr__(self, attr, val):
74 self._load()
77 self._load()
75 setattr(self._module, attr, val)
78 setattr(self._module, attr, val)
76
79
77 def _demandimport(name, globals=None, locals=None, fromlist=None):
80 def _demandimport(name, globals=None, locals=None, fromlist=None):
78 if not locals or name in ignore or fromlist == ('*',):
81 if not locals or name in ignore or fromlist == ('*',):
79 # these cases we can't really delay
82 # these cases we can't really delay
80 return _origimport(name, globals, locals, fromlist)
83 return _origimport(name, globals, locals, fromlist)
81 elif not fromlist:
84 elif not fromlist:
82 # import a [as b]
85 # import a [as b]
83 if '.' in name: # a.b
86 if '.' in name: # a.b
84 base, rest = name.split('.', 1)
87 base, rest = name.split('.', 1)
85 # email.__init__ loading email.mime
88 # email.__init__ loading email.mime
86 if globals and globals.get('__name__', None) == base:
89 if globals and globals.get('__name__', None) == base:
87 return _origimport(name, globals, locals, fromlist)
90 return _origimport(name, globals, locals, fromlist)
88 # if a is already demand-loaded, add b to its submodule list
91 # if a is already demand-loaded, add b to its submodule list
89 if base in locals:
92 if base in locals:
90 if isinstance(locals[base], _demandmod):
93 if isinstance(locals[base], _demandmod):
91 locals[base]._extend(rest)
94 locals[base]._extend(rest)
92 return locals[base]
95 return locals[base]
93 return _demandmod(name, globals, locals)
96 return _demandmod(name, globals, locals)
94 else:
97 else:
95 # from a import b,c,d
98 # from a import b,c,d
96 mod = _origimport(name, globals, locals)
99 mod = _origimport(name, globals, locals)
97 # recurse down the module chain
100 # recurse down the module chain
98 for comp in name.split('.')[1:]:
101 for comp in name.split('.')[1:]:
99 if not hasattr(mod, comp):
102 if not hasattr(mod, comp):
100 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
103 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
101 mod = getattr(mod, comp)
104 mod = getattr(mod, comp)
102 for x in fromlist:
105 for x in fromlist:
103 # set requested submodules for demand load
106 # set requested submodules for demand load
104 if not(hasattr(mod, x)):
107 if not(hasattr(mod, x)):
105 setattr(mod, x, _demandmod(x, mod.__dict__, mod.__dict__))
108 setattr(mod, x, _demandmod(x, mod.__dict__, locals))
106 return mod
109 return mod
107
110
108 ignore = ['_hashlib', '_xmlplus', 'fcntl', 'win32com.gen_py']
111 ignore = ['_hashlib', '_xmlplus', 'fcntl', 'win32com.gen_py']
109
112
110 def enable():
113 def enable():
111 "enable global demand-loading of modules"
114 "enable global demand-loading of modules"
112 __builtins__["__import__"] = _demandimport
115 __builtins__["__import__"] = _demandimport
113
116
114 def disable():
117 def disable():
115 "disable global demand-loading of modules"
118 "disable global demand-loading of modules"
116 __builtins__["__import__"] = _origimport
119 __builtins__["__import__"] = _origimport
117
120
General Comments 0
You need to be logged in to leave comments. Login now