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