##// END OF EJS Templates
py3: conditionalize test-demandimport.py for Python 3...
Gregory Szorc -
r41647:3e89736b default
parent child Browse files
Show More
@@ -6,6 +6,10 b' demandimport.enable()'
6 6 import os
7 7 import subprocess
8 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 14 # Only run if demandimport is allowed
11 15 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
@@ -16,6 +20,16 b" if subprocess.call(['python', '%s/hghave"
16 20 if sys.flags.optimize:
17 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 33 if os.name != 'nt':
20 34 try:
21 35 import distutils.msvc9compiler
@@ -43,15 +57,26 b' from mercurial import node'
43 57
44 58 # We use assert instead of a unittest test case because having imports inside
45 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 65 # now enable it for real
49 66 del os.environ['HGDEMANDIMPORT']
50 67 demandimport.enable()
51 68
52 69 # Test access to special attributes through demandmod proxy
70 assert 'mercurial.error' not in sys.modules
53 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 80 doc = ' '.join(errorproxy.__doc__.split()[:3])
56 81 assert doc == 'Mercurial exceptions. This', doc
57 82 assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__
@@ -61,50 +86,122 b" assert errorproxy.__name__ == 'mercurial"
61 86 name = errorproxy.__dict__['__name__']
62 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 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 103 assert f(os.system) == '<built-in function system>', f(os.system)
70 104 assert f(os) == "<module 'os' from '?'>", f(os)
71 105
106 assert 'mercurial.utils.procutil' not in sys.modules
72 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 117 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
118 assert procutil.__class__ == moduletype, procutil.__class__
76 119 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
77 120 procutil
78 121 )
79 122 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
80 123
124 assert 'mercurial.hgweb' not in sys.modules
81 125 from mercurial import hgweb
82 assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
83 assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(hgweb.hgweb_mod)
126
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 140 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
85 141
86 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 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 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 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 180 remod.escape # use remod
100 181 assert f(remod) == "<module 're' from '?'>", f(remod)
101 182
102 assert f(re) == "<unloaded module 'sys'>", f(re)
103 assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(re.stderr)
104 assert f(re) == "<proxied module 'sys'>", f(re)
183 if ispy3:
184 assert not isinstance(re, _LazyModule)
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 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 205 try:
109 206 from contextlib import unknownattr
110 207
@@ -113,7 +210,9 b' try:'
113 210 'module:\ncontextlib.unknownattr = %s' % f(unknownattr)
114 211 )
115 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 217 from mercurial import util
119 218
General Comments 0
You need to be logged in to leave comments. Login now