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