##// 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 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'],
@@ -16,6 +20,16 b" if subprocess.call(['python', '%s/hghave"
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
@@ -43,6 +57,9 b' 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.
60 if ispy3:
61 assert not isinstance(node, _LazyModule)
62 else:
46 assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
63 assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
47
64
48 # now enable it for real
65 # now enable it for real
@@ -50,8 +67,16 b" 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
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:
54 assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy)
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__
@@ -61,50 +86,122 b" assert errorproxy.__name__ == 'mercurial"
61 name = errorproxy.__dict__['__name__']
86 name = errorproxy.__dict__['__name__']
62 assert name == 'mercurial.error', name
87 assert name == 'mercurial.error', name
63
88
89 if ispy3:
90 assert not isinstance(errorproxy, _LazyModule)
91 assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy)
92 else:
64 assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)
93 assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)
65
94
66 import os
95 import os
67
96
97 if ispy3:
98 assert not isinstance(os, _LazyModule)
99 assert f(os) == "<module 'os' from '?'>", f(os)
100 else:
68 assert f(os) == "<unloaded module 'os'>", f(os)
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
109 if ispy3:
110 assert isinstance(procutil, _LazyModule)
111 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
112 procutil
113 )
114 else:
74 assert f(procutil) == "<unloaded module 'procutil'>", f(procutil)
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
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:
82 assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
135 assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
83 assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(hgweb.hgweb_mod)
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
143
144 if ispy3:
145 assert not isinstance(fred, _LazyModule)
146 assert f(fred) == "<module 're' from '?'>"
147 else:
87 assert f(fred) == "<unloaded module 're'>", f(fred)
148 assert f(fred) == "<unloaded module 're'>", f(fred)
88
149
89 import re as remod
150 import re as remod
151
152 if ispy3:
153 assert not isinstance(remod, _LazyModule)
154 assert f(remod) == "<module 're' from '?'>"
155 else:
90 assert f(remod) == "<unloaded module 're'>", f(remod)
156 assert f(remod) == "<unloaded module 're'>", f(remod)
91
157
92 import sys as re
158 import sys as re
159
160 if ispy3:
161 assert not isinstance(re, _LazyModule)
162 assert f(re) == "<module 'sys' (built-in)>"
163 else:
93 assert f(re) == "<unloaded module 'sys'>", f(re)
164 assert f(re) == "<unloaded module 'sys'>", f(re)
94
165
166 if ispy3:
167 assert not isinstance(fred, _LazyModule)
168 assert f(fred) == "<module 're' from '?'>", f(fred)
169 else:
95 assert f(fred) == "<unloaded module 're'>", f(fred)
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)
173
174 if ispy3:
175 assert not isinstance(fred, _LazyModule)
176 assert f(fred) == "<module 're' from '?'>", f(fred)
177 else:
97 assert f(fred) == "<proxied module 're'>", f(fred)
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
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:
102 assert f(re) == "<unloaded module 'sys'>", f(re)
191 assert f(re) == "<unloaded module 'sys'>", f(re)
103 assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(re.stderr)
192 assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(
193 re.stderr
194 )
104 assert f(re) == "<proxied module 'sys'>", f(re)
195 assert f(re) == "<proxied module 'sys'>", f(re)
105
196
106 import contextlib
197 import contextlib
198
199 if ispy3:
200 assert not isinstance(contextlib, _LazyModule)
201 assert f(contextlib) == "<module 'contextlib' from '?'>"
202 else:
107 assert f(contextlib) == "<unloaded module 'contextlib'>", f(contextlib)
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
@@ -113,7 +210,9 b' try:'
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
General Comments 0
You need to be logged in to leave comments. Login now