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