##// END OF EJS Templates
tests: use unimported modules in test-demandimport.py...
Gregory Szorc -
r41648:30248d6b default
parent child Browse files
Show More
@@ -1,224 +1,226 b''
1 from __future__ import absolute_import, print_function
1 from __future__ import absolute_import, print_function
2
2
3 from mercurial import demandimport
3 from mercurial import demandimport
4 demandimport.enable()
4 demandimport.enable()
5
5
6 import os
6 import os
7 import subprocess
7 import subprocess
8 import sys
8 import sys
9 import types
9 import types
10
10
11 # Don't import pycompat because it has too many side-effects.
11 # Don't import pycompat because it has too many side-effects.
12 ispy3 = sys.version_info[0] >= 3
12 ispy3 = sys.version_info[0] >= 3
13
13
14 # Only run if demandimport is allowed
14 # Only run if demandimport is allowed
15 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
15 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
16 'demandimport']):
16 'demandimport']):
17 sys.exit(80)
17 sys.exit(80)
18
18
19 # We rely on assert, which gets optimized out.
19 # We rely on assert, which gets optimized out.
20 if sys.flags.optimize:
20 if sys.flags.optimize:
21 sys.exit(80)
21 sys.exit(80)
22
22
23 if ispy3:
23 if ispy3:
24 from importlib.util import _LazyModule
24 from importlib.util import _LazyModule
25
25
26 try:
26 try:
27 from importlib.util import _Module as moduletype
27 from importlib.util import _Module as moduletype
28 except ImportError:
28 except ImportError:
29 moduletype = types.ModuleType
29 moduletype = types.ModuleType
30 else:
30 else:
31 moduletype = types.ModuleType
31 moduletype = types.ModuleType
32
32
33 if os.name != 'nt':
33 if os.name != 'nt':
34 try:
34 try:
35 import distutils.msvc9compiler
35 import distutils.msvc9compiler
36 print('distutils.msvc9compiler needs to be an immediate '
36 print('distutils.msvc9compiler needs to be an immediate '
37 'importerror on non-windows platforms')
37 'importerror on non-windows platforms')
38 distutils.msvc9compiler
38 distutils.msvc9compiler
39 except ImportError:
39 except ImportError:
40 pass
40 pass
41
41
42 import re
42 import re
43
43
44 rsub = re.sub
44 rsub = re.sub
45 def f(obj):
45 def f(obj):
46 l = repr(obj)
46 l = repr(obj)
47 l = rsub("0x[0-9a-fA-F]+", "0x?", l)
47 l = rsub("0x[0-9a-fA-F]+", "0x?", l)
48 l = rsub("from '.*'", "from '?'", l)
48 l = rsub("from '.*'", "from '?'", l)
49 l = rsub("'<[a-z]*>'", "'<whatever>'", l)
49 l = rsub("'<[a-z]*>'", "'<whatever>'", l)
50 return l
50 return l
51
51
52 demandimport.disable()
52 demandimport.disable()
53 os.environ['HGDEMANDIMPORT'] = 'disable'
53 os.environ['HGDEMANDIMPORT'] = 'disable'
54 # this enable call should not actually enable demandimport!
54 # this enable call should not actually enable demandimport!
55 demandimport.enable()
55 demandimport.enable()
56 from mercurial import node
56 from mercurial import node
57
57
58 # We use assert instead of a unittest test case because having imports inside
58 # We use assert instead of a unittest test case because having imports inside
59 # functions changes behavior of the demand importer.
59 # functions changes behavior of the demand importer.
60 if ispy3:
60 if ispy3:
61 assert not isinstance(node, _LazyModule)
61 assert not isinstance(node, _LazyModule)
62 else:
62 else:
63 assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
63 assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
64
64
65 # now enable it for real
65 # now enable it for real
66 del os.environ['HGDEMANDIMPORT']
66 del os.environ['HGDEMANDIMPORT']
67 demandimport.enable()
67 demandimport.enable()
68
68
69 # Test access to special attributes through demandmod proxy
69 # Test access to special attributes through demandmod proxy
70 assert 'mercurial.error' not in sys.modules
70 assert 'mercurial.error' not in sys.modules
71 from mercurial import error as errorproxy
71 from mercurial import error as errorproxy
72
72
73 if ispy3:
73 if ispy3:
74 # unsure why this isn't lazy.
74 # unsure why this isn't lazy.
75 assert not isinstance(f, _LazyModule)
75 assert not isinstance(f, _LazyModule)
76 assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
76 assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
77 else:
77 else:
78 assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy)
78 assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy)
79
79
80 doc = ' '.join(errorproxy.__doc__.split()[:3])
80 doc = ' '.join(errorproxy.__doc__.split()[:3])
81 assert doc == 'Mercurial exceptions. This', doc
81 assert doc == 'Mercurial exceptions. This', doc
82 assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__
82 assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__
83
83
84 # __name__ must be accessible via __dict__ so the relative imports can be
84 # __name__ must be accessible via __dict__ so the relative imports can be
85 # resolved
85 # resolved
86 name = errorproxy.__dict__['__name__']
86 name = errorproxy.__dict__['__name__']
87 assert name == 'mercurial.error', name
87 assert name == 'mercurial.error', name
88
88
89 if ispy3:
89 if ispy3:
90 assert not isinstance(errorproxy, _LazyModule)
90 assert not isinstance(errorproxy, _LazyModule)
91 assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
91 assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
92 else:
92 else:
93 assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)
93 assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)
94
94
95 import os
95 import os
96
96
97 if ispy3:
97 if ispy3:
98 assert not isinstance(os, _LazyModule)
98 assert not isinstance(os, _LazyModule)
99 assert f(os) == "<module 'os' from '?'>", f(os)
99 assert f(os) == "<module 'os' from '?'>", f(os)
100 else:
100 else:
101 assert f(os) == "<unloaded module 'os'>", f(os)
101 assert f(os) == "<unloaded module 'os'>", f(os)
102
102
103 assert f(os.system) == '<built-in function system>', f(os.system)
103 assert f(os.system) == '<built-in function system>', f(os.system)
104 assert f(os) == "<module 'os' from '?'>", f(os)
104 assert f(os) == "<module 'os' from '?'>", f(os)
105
105
106 assert 'mercurial.utils.procutil' not in sys.modules
106 assert 'mercurial.utils.procutil' not in sys.modules
107 from mercurial.utils import procutil
107 from mercurial.utils import procutil
108
108
109 if ispy3:
109 if ispy3:
110 assert isinstance(procutil, _LazyModule)
110 assert isinstance(procutil, _LazyModule)
111 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
111 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
112 procutil
112 procutil
113 )
113 )
114 else:
114 else:
115 assert f(procutil) == "<unloaded module 'procutil'>", f(procutil)
115 assert f(procutil) == "<unloaded module 'procutil'>", f(procutil)
116
116
117 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
117 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
118 assert procutil.__class__ == moduletype, procutil.__class__
118 assert procutil.__class__ == moduletype, procutil.__class__
119 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
119 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
120 procutil
120 procutil
121 )
121 )
122 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
122 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
123
123
124 assert 'mercurial.hgweb' not in sys.modules
124 assert 'mercurial.hgweb' not in sys.modules
125 from mercurial import hgweb
125 from mercurial import hgweb
126
126
127 if ispy3:
127 if ispy3:
128 assert not isinstance(hgweb, _LazyModule)
128 assert not isinstance(hgweb, _LazyModule)
129 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
129 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
130 assert isinstance(hgweb.hgweb_mod, _LazyModule)
130 assert isinstance(hgweb.hgweb_mod, _LazyModule)
131 assert (
131 assert (
132 f(hgweb.hgweb_mod) == "<module 'mercurial.hgweb.hgweb_mod' from '?'>"
132 f(hgweb.hgweb_mod) == "<module 'mercurial.hgweb.hgweb_mod' from '?'>"
133 ), f(hgweb.hgweb_mod)
133 ), f(hgweb.hgweb_mod)
134 else:
134 else:
135 assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
135 assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
136 assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(
136 assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(
137 hgweb.hgweb_mod
137 hgweb.hgweb_mod
138 )
138 )
139
139
140 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
140 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
141
141
142 import re as fred
142 import re as fred
143
143
144 if ispy3:
144 if ispy3:
145 assert not isinstance(fred, _LazyModule)
145 assert not isinstance(fred, _LazyModule)
146 assert f(fred) == "<module 're' from '?'>"
146 assert f(fred) == "<module 're' from '?'>"
147 else:
147 else:
148 assert f(fred) == "<unloaded module 're'>", f(fred)
148 assert f(fred) == "<unloaded module 're'>", f(fred)
149
149
150 import re as remod
150 import re as remod
151
151
152 if ispy3:
152 if ispy3:
153 assert not isinstance(remod, _LazyModule)
153 assert not isinstance(remod, _LazyModule)
154 assert f(remod) == "<module 're' from '?'>"
154 assert f(remod) == "<module 're' from '?'>"
155 else:
155 else:
156 assert f(remod) == "<unloaded module 're'>", f(remod)
156 assert f(remod) == "<unloaded module 're'>", f(remod)
157
157
158 import sys as re
158 import sys as re
159
159
160 if ispy3:
160 if ispy3:
161 assert not isinstance(re, _LazyModule)
161 assert not isinstance(re, _LazyModule)
162 assert f(re) == "<module 'sys' (built-in)>"
162 assert f(re) == "<module 'sys' (built-in)>"
163 else:
163 else:
164 assert f(re) == "<unloaded module 'sys'>", f(re)
164 assert f(re) == "<unloaded module 'sys'>", f(re)
165
165
166 if ispy3:
166 if ispy3:
167 assert not isinstance(fred, _LazyModule)
167 assert not isinstance(fred, _LazyModule)
168 assert f(fred) == "<module 're' from '?'>", f(fred)
168 assert f(fred) == "<module 're' from '?'>", f(fred)
169 else:
169 else:
170 assert f(fred) == "<unloaded module 're'>", f(fred)
170 assert f(fred) == "<unloaded module 're'>", f(fred)
171
171
172 assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub)
172 assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub)
173
173
174 if ispy3:
174 if ispy3:
175 assert not isinstance(fred, _LazyModule)
175 assert not isinstance(fred, _LazyModule)
176 assert f(fred) == "<module 're' from '?'>", f(fred)
176 assert f(fred) == "<module 're' from '?'>", f(fred)
177 else:
177 else:
178 assert f(fred) == "<proxied module 're'>", f(fred)
178 assert f(fred) == "<proxied module 're'>", f(fred)
179
179
180 remod.escape # use remod
180 remod.escape # use remod
181 assert f(remod) == "<module 're' from '?'>", f(remod)
181 assert f(remod) == "<module 're' from '?'>", f(remod)
182
182
183 if ispy3:
183 if ispy3:
184 assert not isinstance(re, _LazyModule)
184 assert not isinstance(re, _LazyModule)
185 assert f(re) == "<module 'sys' (built-in)>"
185 assert f(re) == "<module 'sys' (built-in)>"
186 assert f(type(re.stderr)) == "<class '_io.TextIOWrapper'>", f(
186 assert f(type(re.stderr)) == "<class '_io.TextIOWrapper'>", f(
187 type(re.stderr)
187 type(re.stderr)
188 )
188 )
189 assert f(re) == "<module 'sys' (built-in)>"
189 assert f(re) == "<module 'sys' (built-in)>"
190 else:
190 else:
191 assert f(re) == "<unloaded module 'sys'>", f(re)
191 assert f(re) == "<unloaded module 'sys'>", f(re)
192 assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(
192 assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(
193 re.stderr
193 re.stderr
194 )
194 )
195 assert f(re) == "<proxied module 'sys'>", f(re)
195 assert f(re) == "<proxied module 'sys'>", f(re)
196
196
197 import contextlib
197 assert 'telnetlib' not in sys.modules
198 import telnetlib
198
199
199 if ispy3:
200 if ispy3:
200 assert not isinstance(contextlib, _LazyModule)
201 assert not isinstance(telnetlib, _LazyModule)
201 assert f(contextlib) == "<module 'contextlib' from '?'>"
202 assert f(telnetlib) == "<module 'telnetlib' from '?'>"
202 else:
203 else:
203 assert f(contextlib) == "<unloaded module 'contextlib'>", f(contextlib)
204 assert f(telnetlib) == "<unloaded module 'telnetlib'>", f(telnetlib)
204
205
205 try:
206 try:
206 from contextlib import unknownattr
207 from telnetlib import unknownattr
207
208
208 assert False, (
209 assert False, (
209 'no demandmod should be created for attribute of non-package '
210 'no demandmod should be created for attribute of non-package '
210 'module:\ncontextlib.unknownattr = %s' % f(unknownattr)
211 'module:\ntelnetlib.unknownattr = %s' % f(unknownattr)
211 )
212 )
212 except ImportError as inst:
213 except ImportError as inst:
213 assert rsub(r"'", '', str(inst)).startswith(
214 assert rsub(r"'", '', str(inst)).startswith(
214 'cannot import name unknownattr'
215 'cannot import name unknownattr'
215 )
216 )
216
217
217 from mercurial import util
218 from mercurial import util
218
219
219 # Unlike the import statement, __import__() function should not raise
220 # Unlike the import statement, __import__() function should not raise
220 # ImportError even if fromlist has an unknown item
221 # ImportError even if fromlist has an unknown item
221 # (see Python/import.c:import_module_level() and ensure_fromlist())
222 # (see Python/import.c:import_module_level() and ensure_fromlist())
222 contextlibimp = __import__('contextlib', globals(), locals(), ['unknownattr'])
223 assert 'zipfile' not in sys.modules
223 assert f(contextlibimp) == "<module 'contextlib' from '?'>", f(contextlibimp)
224 zipfileimp = __import__('zipfile', globals(), locals(), ['unknownattr'])
224 assert not util.safehasattr(contextlibimp, 'unknownattr')
225 assert f(zipfileimp) == "<module 'zipfile' from '?'>", f(zipfileimp)
226 assert not util.safehasattr(zipfileimp, 'unknownattr')
General Comments 0
You need to be logged in to leave comments. Login now