1 | n/a | # We import importlib *ASAP* in order to test #15386 |
---|
2 | n/a | import importlib |
---|
3 | n/a | import importlib.util |
---|
4 | n/a | from importlib._bootstrap_external import _get_sourcefile |
---|
5 | n/a | import builtins |
---|
6 | n/a | import marshal |
---|
7 | n/a | import os |
---|
8 | n/a | import platform |
---|
9 | n/a | import py_compile |
---|
10 | n/a | import random |
---|
11 | n/a | import stat |
---|
12 | n/a | import sys |
---|
13 | n/a | import unittest |
---|
14 | n/a | import unittest.mock as mock |
---|
15 | n/a | import textwrap |
---|
16 | n/a | import errno |
---|
17 | n/a | import contextlib |
---|
18 | n/a | |
---|
19 | n/a | import test.support |
---|
20 | n/a | from test.support import ( |
---|
21 | n/a | EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, |
---|
22 | n/a | make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, |
---|
23 | n/a | unlink, unload, create_empty_file, cpython_only, TESTFN_UNENCODABLE, |
---|
24 | n/a | temp_dir) |
---|
25 | n/a | from test.support import script_helper |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | skip_if_dont_write_bytecode = unittest.skipIf( |
---|
29 | n/a | sys.dont_write_bytecode, |
---|
30 | n/a | "test meaningful only when writing bytecode") |
---|
31 | n/a | |
---|
32 | n/a | def remove_files(name): |
---|
33 | n/a | for f in (name + ".py", |
---|
34 | n/a | name + ".pyc", |
---|
35 | n/a | name + ".pyw", |
---|
36 | n/a | name + "$py.class"): |
---|
37 | n/a | unlink(f) |
---|
38 | n/a | rmtree('__pycache__') |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | @contextlib.contextmanager |
---|
42 | n/a | def _ready_to_import(name=None, source=""): |
---|
43 | n/a | # sets up a temporary directory and removes it |
---|
44 | n/a | # creates the module file |
---|
45 | n/a | # temporarily clears the module from sys.modules (if any) |
---|
46 | n/a | # reverts or removes the module when cleaning up |
---|
47 | n/a | name = name or "spam" |
---|
48 | n/a | with temp_dir() as tempdir: |
---|
49 | n/a | path = script_helper.make_script(tempdir, name, source) |
---|
50 | n/a | old_module = sys.modules.pop(name, None) |
---|
51 | n/a | try: |
---|
52 | n/a | sys.path.insert(0, tempdir) |
---|
53 | n/a | yield name, path |
---|
54 | n/a | sys.path.remove(tempdir) |
---|
55 | n/a | finally: |
---|
56 | n/a | if old_module is not None: |
---|
57 | n/a | sys.modules[name] = old_module |
---|
58 | n/a | elif name in sys.modules: |
---|
59 | n/a | del sys.modules[name] |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | class ImportTests(unittest.TestCase): |
---|
63 | n/a | |
---|
64 | n/a | def setUp(self): |
---|
65 | n/a | remove_files(TESTFN) |
---|
66 | n/a | importlib.invalidate_caches() |
---|
67 | n/a | |
---|
68 | n/a | def tearDown(self): |
---|
69 | n/a | unload(TESTFN) |
---|
70 | n/a | |
---|
71 | n/a | def test_import_raises_ModuleNotFoundError(self): |
---|
72 | n/a | with self.assertRaises(ModuleNotFoundError): |
---|
73 | n/a | import something_that_should_not_exist_anywhere |
---|
74 | n/a | |
---|
75 | n/a | def test_from_import_missing_module_raises_ModuleNotFoundError(self): |
---|
76 | n/a | with self.assertRaises(ModuleNotFoundError): |
---|
77 | n/a | from something_that_should_not_exist_anywhere import blah |
---|
78 | n/a | |
---|
79 | n/a | def test_from_import_missing_attr_raises_ImportError(self): |
---|
80 | n/a | with self.assertRaises(ImportError): |
---|
81 | n/a | from importlib import something_that_should_not_exist_anywhere |
---|
82 | n/a | |
---|
83 | n/a | def test_case_sensitivity(self): |
---|
84 | n/a | # Brief digression to test that import is case-sensitive: if we got |
---|
85 | n/a | # this far, we know for sure that "random" exists. |
---|
86 | n/a | with self.assertRaises(ImportError): |
---|
87 | n/a | import RAnDoM |
---|
88 | n/a | |
---|
89 | n/a | def test_double_const(self): |
---|
90 | n/a | # Another brief digression to test the accuracy of manifest float |
---|
91 | n/a | # constants. |
---|
92 | n/a | from test import double_const # don't blink -- that *was* the test |
---|
93 | n/a | |
---|
94 | n/a | def test_import(self): |
---|
95 | n/a | def test_with_extension(ext): |
---|
96 | n/a | # The extension is normally ".py", perhaps ".pyw". |
---|
97 | n/a | source = TESTFN + ext |
---|
98 | n/a | if is_jython: |
---|
99 | n/a | pyc = TESTFN + "$py.class" |
---|
100 | n/a | else: |
---|
101 | n/a | pyc = TESTFN + ".pyc" |
---|
102 | n/a | |
---|
103 | n/a | with open(source, "w") as f: |
---|
104 | n/a | print("# This tests Python's ability to import a", |
---|
105 | n/a | ext, "file.", file=f) |
---|
106 | n/a | a = random.randrange(1000) |
---|
107 | n/a | b = random.randrange(1000) |
---|
108 | n/a | print("a =", a, file=f) |
---|
109 | n/a | print("b =", b, file=f) |
---|
110 | n/a | |
---|
111 | n/a | if TESTFN in sys.modules: |
---|
112 | n/a | del sys.modules[TESTFN] |
---|
113 | n/a | importlib.invalidate_caches() |
---|
114 | n/a | try: |
---|
115 | n/a | try: |
---|
116 | n/a | mod = __import__(TESTFN) |
---|
117 | n/a | except ImportError as err: |
---|
118 | n/a | self.fail("import from %s failed: %s" % (ext, err)) |
---|
119 | n/a | |
---|
120 | n/a | self.assertEqual(mod.a, a, |
---|
121 | n/a | "module loaded (%s) but contents invalid" % mod) |
---|
122 | n/a | self.assertEqual(mod.b, b, |
---|
123 | n/a | "module loaded (%s) but contents invalid" % mod) |
---|
124 | n/a | finally: |
---|
125 | n/a | forget(TESTFN) |
---|
126 | n/a | unlink(source) |
---|
127 | n/a | unlink(pyc) |
---|
128 | n/a | |
---|
129 | n/a | sys.path.insert(0, os.curdir) |
---|
130 | n/a | try: |
---|
131 | n/a | test_with_extension(".py") |
---|
132 | n/a | if sys.platform.startswith("win"): |
---|
133 | n/a | for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]: |
---|
134 | n/a | test_with_extension(ext) |
---|
135 | n/a | finally: |
---|
136 | n/a | del sys.path[0] |
---|
137 | n/a | |
---|
138 | n/a | def test_module_with_large_stack(self, module='longlist'): |
---|
139 | n/a | # Regression test for http://bugs.python.org/issue561858. |
---|
140 | n/a | filename = module + '.py' |
---|
141 | n/a | |
---|
142 | n/a | # Create a file with a list of 65000 elements. |
---|
143 | n/a | with open(filename, 'w') as f: |
---|
144 | n/a | f.write('d = [\n') |
---|
145 | n/a | for i in range(65000): |
---|
146 | n/a | f.write('"",\n') |
---|
147 | n/a | f.write(']') |
---|
148 | n/a | |
---|
149 | n/a | try: |
---|
150 | n/a | # Compile & remove .py file; we only need .pyc. |
---|
151 | n/a | # Bytecode must be relocated from the PEP 3147 bytecode-only location. |
---|
152 | n/a | py_compile.compile(filename) |
---|
153 | n/a | finally: |
---|
154 | n/a | unlink(filename) |
---|
155 | n/a | |
---|
156 | n/a | # Need to be able to load from current dir. |
---|
157 | n/a | sys.path.append('') |
---|
158 | n/a | importlib.invalidate_caches() |
---|
159 | n/a | |
---|
160 | n/a | namespace = {} |
---|
161 | n/a | try: |
---|
162 | n/a | make_legacy_pyc(filename) |
---|
163 | n/a | # This used to crash. |
---|
164 | n/a | exec('import ' + module, None, namespace) |
---|
165 | n/a | finally: |
---|
166 | n/a | # Cleanup. |
---|
167 | n/a | del sys.path[-1] |
---|
168 | n/a | unlink(filename + 'c') |
---|
169 | n/a | unlink(filename + 'o') |
---|
170 | n/a | |
---|
171 | n/a | # Remove references to the module (unload the module) |
---|
172 | n/a | namespace.clear() |
---|
173 | n/a | try: |
---|
174 | n/a | del sys.modules[module] |
---|
175 | n/a | except KeyError: |
---|
176 | n/a | pass |
---|
177 | n/a | |
---|
178 | n/a | def test_failing_import_sticks(self): |
---|
179 | n/a | source = TESTFN + ".py" |
---|
180 | n/a | with open(source, "w") as f: |
---|
181 | n/a | print("a = 1/0", file=f) |
---|
182 | n/a | |
---|
183 | n/a | # New in 2.4, we shouldn't be able to import that no matter how often |
---|
184 | n/a | # we try. |
---|
185 | n/a | sys.path.insert(0, os.curdir) |
---|
186 | n/a | importlib.invalidate_caches() |
---|
187 | n/a | if TESTFN in sys.modules: |
---|
188 | n/a | del sys.modules[TESTFN] |
---|
189 | n/a | try: |
---|
190 | n/a | for i in [1, 2, 3]: |
---|
191 | n/a | self.assertRaises(ZeroDivisionError, __import__, TESTFN) |
---|
192 | n/a | self.assertNotIn(TESTFN, sys.modules, |
---|
193 | n/a | "damaged module in sys.modules on %i try" % i) |
---|
194 | n/a | finally: |
---|
195 | n/a | del sys.path[0] |
---|
196 | n/a | remove_files(TESTFN) |
---|
197 | n/a | |
---|
198 | n/a | def test_import_name_binding(self): |
---|
199 | n/a | # import x.y.z binds x in the current namespace |
---|
200 | n/a | import test as x |
---|
201 | n/a | import test.support |
---|
202 | n/a | self.assertIs(x, test, x.__name__) |
---|
203 | n/a | self.assertTrue(hasattr(test.support, "__file__")) |
---|
204 | n/a | |
---|
205 | n/a | # import x.y.z as w binds z as w |
---|
206 | n/a | import test.support as y |
---|
207 | n/a | self.assertIs(y, test.support, y.__name__) |
---|
208 | n/a | |
---|
209 | n/a | def test_failing_reload(self): |
---|
210 | n/a | # A failing reload should leave the module object in sys.modules. |
---|
211 | n/a | source = TESTFN + os.extsep + "py" |
---|
212 | n/a | with open(source, "w") as f: |
---|
213 | n/a | f.write("a = 1\nb=2\n") |
---|
214 | n/a | |
---|
215 | n/a | sys.path.insert(0, os.curdir) |
---|
216 | n/a | try: |
---|
217 | n/a | mod = __import__(TESTFN) |
---|
218 | n/a | self.assertIn(TESTFN, sys.modules) |
---|
219 | n/a | self.assertEqual(mod.a, 1, "module has wrong attribute values") |
---|
220 | n/a | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
---|
221 | n/a | |
---|
222 | n/a | # On WinXP, just replacing the .py file wasn't enough to |
---|
223 | n/a | # convince reload() to reparse it. Maybe the timestamp didn't |
---|
224 | n/a | # move enough. We force it to get reparsed by removing the |
---|
225 | n/a | # compiled file too. |
---|
226 | n/a | remove_files(TESTFN) |
---|
227 | n/a | |
---|
228 | n/a | # Now damage the module. |
---|
229 | n/a | with open(source, "w") as f: |
---|
230 | n/a | f.write("a = 10\nb=20//0\n") |
---|
231 | n/a | |
---|
232 | n/a | self.assertRaises(ZeroDivisionError, importlib.reload, mod) |
---|
233 | n/a | # But we still expect the module to be in sys.modules. |
---|
234 | n/a | mod = sys.modules.get(TESTFN) |
---|
235 | n/a | self.assertIsNotNone(mod, "expected module to be in sys.modules") |
---|
236 | n/a | |
---|
237 | n/a | # We should have replaced a w/ 10, but the old b value should |
---|
238 | n/a | # stick. |
---|
239 | n/a | self.assertEqual(mod.a, 10, "module has wrong attribute values") |
---|
240 | n/a | self.assertEqual(mod.b, 2, "module has wrong attribute values") |
---|
241 | n/a | |
---|
242 | n/a | finally: |
---|
243 | n/a | del sys.path[0] |
---|
244 | n/a | remove_files(TESTFN) |
---|
245 | n/a | unload(TESTFN) |
---|
246 | n/a | |
---|
247 | n/a | @skip_if_dont_write_bytecode |
---|
248 | n/a | def test_file_to_source(self): |
---|
249 | n/a | # check if __file__ points to the source file where available |
---|
250 | n/a | source = TESTFN + ".py" |
---|
251 | n/a | with open(source, "w") as f: |
---|
252 | n/a | f.write("test = None\n") |
---|
253 | n/a | |
---|
254 | n/a | sys.path.insert(0, os.curdir) |
---|
255 | n/a | try: |
---|
256 | n/a | mod = __import__(TESTFN) |
---|
257 | n/a | self.assertTrue(mod.__file__.endswith('.py')) |
---|
258 | n/a | os.remove(source) |
---|
259 | n/a | del sys.modules[TESTFN] |
---|
260 | n/a | make_legacy_pyc(source) |
---|
261 | n/a | importlib.invalidate_caches() |
---|
262 | n/a | mod = __import__(TESTFN) |
---|
263 | n/a | base, ext = os.path.splitext(mod.__file__) |
---|
264 | n/a | self.assertEqual(ext, '.pyc') |
---|
265 | n/a | finally: |
---|
266 | n/a | del sys.path[0] |
---|
267 | n/a | remove_files(TESTFN) |
---|
268 | n/a | if TESTFN in sys.modules: |
---|
269 | n/a | del sys.modules[TESTFN] |
---|
270 | n/a | |
---|
271 | n/a | def test_import_by_filename(self): |
---|
272 | n/a | path = os.path.abspath(TESTFN) |
---|
273 | n/a | encoding = sys.getfilesystemencoding() |
---|
274 | n/a | try: |
---|
275 | n/a | path.encode(encoding) |
---|
276 | n/a | except UnicodeEncodeError: |
---|
277 | n/a | self.skipTest('path is not encodable to {}'.format(encoding)) |
---|
278 | n/a | with self.assertRaises(ImportError) as c: |
---|
279 | n/a | __import__(path) |
---|
280 | n/a | |
---|
281 | n/a | def test_import_in_del_does_not_crash(self): |
---|
282 | n/a | # Issue 4236 |
---|
283 | n/a | testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\ |
---|
284 | n/a | import sys |
---|
285 | n/a | class C: |
---|
286 | n/a | def __del__(self): |
---|
287 | n/a | import importlib |
---|
288 | n/a | sys.argv.insert(0, C()) |
---|
289 | n/a | """)) |
---|
290 | n/a | script_helper.assert_python_ok(testfn) |
---|
291 | n/a | |
---|
292 | n/a | @skip_if_dont_write_bytecode |
---|
293 | n/a | def test_timestamp_overflow(self): |
---|
294 | n/a | # A modification timestamp larger than 2**32 should not be a problem |
---|
295 | n/a | # when importing a module (issue #11235). |
---|
296 | n/a | sys.path.insert(0, os.curdir) |
---|
297 | n/a | try: |
---|
298 | n/a | source = TESTFN + ".py" |
---|
299 | n/a | compiled = importlib.util.cache_from_source(source) |
---|
300 | n/a | with open(source, 'w') as f: |
---|
301 | n/a | pass |
---|
302 | n/a | try: |
---|
303 | n/a | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
---|
304 | n/a | except OverflowError: |
---|
305 | n/a | self.skipTest("cannot set modification time to large integer") |
---|
306 | n/a | except OSError as e: |
---|
307 | n/a | if e.errno not in (getattr(errno, 'EOVERFLOW', None), |
---|
308 | n/a | getattr(errno, 'EINVAL', None)): |
---|
309 | n/a | raise |
---|
310 | n/a | self.skipTest("cannot set modification time to large integer ({})".format(e)) |
---|
311 | n/a | __import__(TESTFN) |
---|
312 | n/a | # The pyc file was created. |
---|
313 | n/a | os.stat(compiled) |
---|
314 | n/a | finally: |
---|
315 | n/a | del sys.path[0] |
---|
316 | n/a | remove_files(TESTFN) |
---|
317 | n/a | |
---|
318 | n/a | def test_bogus_fromlist(self): |
---|
319 | n/a | try: |
---|
320 | n/a | __import__('http', fromlist=['blah']) |
---|
321 | n/a | except ImportError: |
---|
322 | n/a | self.fail("fromlist must allow bogus names") |
---|
323 | n/a | |
---|
324 | n/a | @cpython_only |
---|
325 | n/a | def test_delete_builtins_import(self): |
---|
326 | n/a | args = ["-c", "del __builtins__.__import__; import os"] |
---|
327 | n/a | popen = script_helper.spawn_python(*args) |
---|
328 | n/a | stdout, stderr = popen.communicate() |
---|
329 | n/a | self.assertIn(b"ImportError", stdout) |
---|
330 | n/a | |
---|
331 | n/a | def test_from_import_message_for_nonexistent_module(self): |
---|
332 | n/a | with self.assertRaisesRegex(ImportError, "^No module named 'bogus'"): |
---|
333 | n/a | from bogus import foo |
---|
334 | n/a | |
---|
335 | n/a | def test_from_import_message_for_existing_module(self): |
---|
336 | n/a | with self.assertRaisesRegex(ImportError, "^cannot import name 'bogus'"): |
---|
337 | n/a | from re import bogus |
---|
338 | n/a | |
---|
339 | n/a | def test_from_import_AttributeError(self): |
---|
340 | n/a | # Issue #24492: trying to import an attribute that raises an |
---|
341 | n/a | # AttributeError should lead to an ImportError. |
---|
342 | n/a | class AlwaysAttributeError: |
---|
343 | n/a | def __getattr__(self, _): |
---|
344 | n/a | raise AttributeError |
---|
345 | n/a | |
---|
346 | n/a | module_name = 'test_from_import_AttributeError' |
---|
347 | n/a | self.addCleanup(unload, module_name) |
---|
348 | n/a | sys.modules[module_name] = AlwaysAttributeError() |
---|
349 | n/a | with self.assertRaises(ImportError): |
---|
350 | n/a | from test_from_import_AttributeError import does_not_exist |
---|
351 | n/a | |
---|
352 | n/a | |
---|
353 | n/a | @skip_if_dont_write_bytecode |
---|
354 | n/a | class FilePermissionTests(unittest.TestCase): |
---|
355 | n/a | # tests for file mode on cached .pyc files |
---|
356 | n/a | |
---|
357 | n/a | @unittest.skipUnless(os.name == 'posix', |
---|
358 | n/a | "test meaningful only on posix systems") |
---|
359 | n/a | def test_creation_mode(self): |
---|
360 | n/a | mask = 0o022 |
---|
361 | n/a | with temp_umask(mask), _ready_to_import() as (name, path): |
---|
362 | n/a | cached_path = importlib.util.cache_from_source(path) |
---|
363 | n/a | module = __import__(name) |
---|
364 | n/a | if not os.path.exists(cached_path): |
---|
365 | n/a | self.fail("__import__ did not result in creation of " |
---|
366 | n/a | "a .pyc file") |
---|
367 | n/a | stat_info = os.stat(cached_path) |
---|
368 | n/a | |
---|
369 | n/a | # Check that the umask is respected, and the executable bits |
---|
370 | n/a | # aren't set. |
---|
371 | n/a | self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), |
---|
372 | n/a | oct(0o666 & ~mask)) |
---|
373 | n/a | |
---|
374 | n/a | @unittest.skipUnless(os.name == 'posix', |
---|
375 | n/a | "test meaningful only on posix systems") |
---|
376 | n/a | def test_cached_mode_issue_2051(self): |
---|
377 | n/a | # permissions of .pyc should match those of .py, regardless of mask |
---|
378 | n/a | mode = 0o600 |
---|
379 | n/a | with temp_umask(0o022), _ready_to_import() as (name, path): |
---|
380 | n/a | cached_path = importlib.util.cache_from_source(path) |
---|
381 | n/a | os.chmod(path, mode) |
---|
382 | n/a | __import__(name) |
---|
383 | n/a | if not os.path.exists(cached_path): |
---|
384 | n/a | self.fail("__import__ did not result in creation of " |
---|
385 | n/a | "a .pyc file") |
---|
386 | n/a | stat_info = os.stat(cached_path) |
---|
387 | n/a | |
---|
388 | n/a | self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(mode)) |
---|
389 | n/a | |
---|
390 | n/a | @unittest.skipUnless(os.name == 'posix', |
---|
391 | n/a | "test meaningful only on posix systems") |
---|
392 | n/a | def test_cached_readonly(self): |
---|
393 | n/a | mode = 0o400 |
---|
394 | n/a | with temp_umask(0o022), _ready_to_import() as (name, path): |
---|
395 | n/a | cached_path = importlib.util.cache_from_source(path) |
---|
396 | n/a | os.chmod(path, mode) |
---|
397 | n/a | __import__(name) |
---|
398 | n/a | if not os.path.exists(cached_path): |
---|
399 | n/a | self.fail("__import__ did not result in creation of " |
---|
400 | n/a | "a .pyc file") |
---|
401 | n/a | stat_info = os.stat(cached_path) |
---|
402 | n/a | |
---|
403 | n/a | expected = mode | 0o200 # Account for fix for issue #6074 |
---|
404 | n/a | self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(expected)) |
---|
405 | n/a | |
---|
406 | n/a | def test_pyc_always_writable(self): |
---|
407 | n/a | # Initially read-only .pyc files on Windows used to cause problems |
---|
408 | n/a | # with later updates, see issue #6074 for details |
---|
409 | n/a | with _ready_to_import() as (name, path): |
---|
410 | n/a | # Write a Python file, make it read-only and import it |
---|
411 | n/a | with open(path, 'w') as f: |
---|
412 | n/a | f.write("x = 'original'\n") |
---|
413 | n/a | # Tweak the mtime of the source to ensure pyc gets updated later |
---|
414 | n/a | s = os.stat(path) |
---|
415 | n/a | os.utime(path, (s.st_atime, s.st_mtime-100000000)) |
---|
416 | n/a | os.chmod(path, 0o400) |
---|
417 | n/a | m = __import__(name) |
---|
418 | n/a | self.assertEqual(m.x, 'original') |
---|
419 | n/a | # Change the file and then reimport it |
---|
420 | n/a | os.chmod(path, 0o600) |
---|
421 | n/a | with open(path, 'w') as f: |
---|
422 | n/a | f.write("x = 'rewritten'\n") |
---|
423 | n/a | unload(name) |
---|
424 | n/a | importlib.invalidate_caches() |
---|
425 | n/a | m = __import__(name) |
---|
426 | n/a | self.assertEqual(m.x, 'rewritten') |
---|
427 | n/a | # Now delete the source file and check the pyc was rewritten |
---|
428 | n/a | unlink(path) |
---|
429 | n/a | unload(name) |
---|
430 | n/a | importlib.invalidate_caches() |
---|
431 | n/a | bytecode_only = path + "c" |
---|
432 | n/a | os.rename(importlib.util.cache_from_source(path), bytecode_only) |
---|
433 | n/a | m = __import__(name) |
---|
434 | n/a | self.assertEqual(m.x, 'rewritten') |
---|
435 | n/a | |
---|
436 | n/a | |
---|
437 | n/a | class PycRewritingTests(unittest.TestCase): |
---|
438 | n/a | # Test that the `co_filename` attribute on code objects always points |
---|
439 | n/a | # to the right file, even when various things happen (e.g. both the .py |
---|
440 | n/a | # and the .pyc file are renamed). |
---|
441 | n/a | |
---|
442 | n/a | module_name = "unlikely_module_name" |
---|
443 | n/a | module_source = """ |
---|
444 | n/a | import sys |
---|
445 | n/a | code_filename = sys._getframe().f_code.co_filename |
---|
446 | n/a | module_filename = __file__ |
---|
447 | n/a | constant = 1 |
---|
448 | n/a | def func(): |
---|
449 | n/a | pass |
---|
450 | n/a | func_filename = func.__code__.co_filename |
---|
451 | n/a | """ |
---|
452 | n/a | dir_name = os.path.abspath(TESTFN) |
---|
453 | n/a | file_name = os.path.join(dir_name, module_name) + os.extsep + "py" |
---|
454 | n/a | compiled_name = importlib.util.cache_from_source(file_name) |
---|
455 | n/a | |
---|
456 | n/a | def setUp(self): |
---|
457 | n/a | self.sys_path = sys.path[:] |
---|
458 | n/a | self.orig_module = sys.modules.pop(self.module_name, None) |
---|
459 | n/a | os.mkdir(self.dir_name) |
---|
460 | n/a | with open(self.file_name, "w") as f: |
---|
461 | n/a | f.write(self.module_source) |
---|
462 | n/a | sys.path.insert(0, self.dir_name) |
---|
463 | n/a | importlib.invalidate_caches() |
---|
464 | n/a | |
---|
465 | n/a | def tearDown(self): |
---|
466 | n/a | sys.path[:] = self.sys_path |
---|
467 | n/a | if self.orig_module is not None: |
---|
468 | n/a | sys.modules[self.module_name] = self.orig_module |
---|
469 | n/a | else: |
---|
470 | n/a | unload(self.module_name) |
---|
471 | n/a | unlink(self.file_name) |
---|
472 | n/a | unlink(self.compiled_name) |
---|
473 | n/a | rmtree(self.dir_name) |
---|
474 | n/a | |
---|
475 | n/a | def import_module(self): |
---|
476 | n/a | ns = globals() |
---|
477 | n/a | __import__(self.module_name, ns, ns) |
---|
478 | n/a | return sys.modules[self.module_name] |
---|
479 | n/a | |
---|
480 | n/a | def test_basics(self): |
---|
481 | n/a | mod = self.import_module() |
---|
482 | n/a | self.assertEqual(mod.module_filename, self.file_name) |
---|
483 | n/a | self.assertEqual(mod.code_filename, self.file_name) |
---|
484 | n/a | self.assertEqual(mod.func_filename, self.file_name) |
---|
485 | n/a | del sys.modules[self.module_name] |
---|
486 | n/a | mod = self.import_module() |
---|
487 | n/a | self.assertEqual(mod.module_filename, self.file_name) |
---|
488 | n/a | self.assertEqual(mod.code_filename, self.file_name) |
---|
489 | n/a | self.assertEqual(mod.func_filename, self.file_name) |
---|
490 | n/a | |
---|
491 | n/a | def test_incorrect_code_name(self): |
---|
492 | n/a | py_compile.compile(self.file_name, dfile="another_module.py") |
---|
493 | n/a | mod = self.import_module() |
---|
494 | n/a | self.assertEqual(mod.module_filename, self.file_name) |
---|
495 | n/a | self.assertEqual(mod.code_filename, self.file_name) |
---|
496 | n/a | self.assertEqual(mod.func_filename, self.file_name) |
---|
497 | n/a | |
---|
498 | n/a | def test_module_without_source(self): |
---|
499 | n/a | target = "another_module.py" |
---|
500 | n/a | py_compile.compile(self.file_name, dfile=target) |
---|
501 | n/a | os.remove(self.file_name) |
---|
502 | n/a | pyc_file = make_legacy_pyc(self.file_name) |
---|
503 | n/a | importlib.invalidate_caches() |
---|
504 | n/a | mod = self.import_module() |
---|
505 | n/a | self.assertEqual(mod.module_filename, pyc_file) |
---|
506 | n/a | self.assertEqual(mod.code_filename, target) |
---|
507 | n/a | self.assertEqual(mod.func_filename, target) |
---|
508 | n/a | |
---|
509 | n/a | def test_foreign_code(self): |
---|
510 | n/a | py_compile.compile(self.file_name) |
---|
511 | n/a | with open(self.compiled_name, "rb") as f: |
---|
512 | n/a | header = f.read(12) |
---|
513 | n/a | code = marshal.load(f) |
---|
514 | n/a | constants = list(code.co_consts) |
---|
515 | n/a | foreign_code = importlib.import_module.__code__ |
---|
516 | n/a | pos = constants.index(1) |
---|
517 | n/a | constants[pos] = foreign_code |
---|
518 | n/a | code = type(code)(code.co_argcount, code.co_kwonlyargcount, |
---|
519 | n/a | code.co_nlocals, code.co_stacksize, |
---|
520 | n/a | code.co_flags, code.co_code, tuple(constants), |
---|
521 | n/a | code.co_names, code.co_varnames, code.co_filename, |
---|
522 | n/a | code.co_name, code.co_firstlineno, code.co_lnotab, |
---|
523 | n/a | code.co_freevars, code.co_cellvars) |
---|
524 | n/a | with open(self.compiled_name, "wb") as f: |
---|
525 | n/a | f.write(header) |
---|
526 | n/a | marshal.dump(code, f) |
---|
527 | n/a | mod = self.import_module() |
---|
528 | n/a | self.assertEqual(mod.constant.co_filename, foreign_code.co_filename) |
---|
529 | n/a | |
---|
530 | n/a | |
---|
531 | n/a | class PathsTests(unittest.TestCase): |
---|
532 | n/a | SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8', |
---|
533 | n/a | 'test\u00b0\u00b3\u00b2') |
---|
534 | n/a | path = TESTFN |
---|
535 | n/a | |
---|
536 | n/a | def setUp(self): |
---|
537 | n/a | os.mkdir(self.path) |
---|
538 | n/a | self.syspath = sys.path[:] |
---|
539 | n/a | |
---|
540 | n/a | def tearDown(self): |
---|
541 | n/a | rmtree(self.path) |
---|
542 | n/a | sys.path[:] = self.syspath |
---|
543 | n/a | |
---|
544 | n/a | # Regression test for http://bugs.python.org/issue1293. |
---|
545 | n/a | def test_trailing_slash(self): |
---|
546 | n/a | with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
---|
547 | n/a | f.write("testdata = 'test_trailing_slash'") |
---|
548 | n/a | sys.path.append(self.path+'/') |
---|
549 | n/a | mod = __import__("test_trailing_slash") |
---|
550 | n/a | self.assertEqual(mod.testdata, 'test_trailing_slash') |
---|
551 | n/a | unload("test_trailing_slash") |
---|
552 | n/a | |
---|
553 | n/a | # Regression test for http://bugs.python.org/issue3677. |
---|
554 | n/a | @unittest.skipUnless(sys.platform == 'win32', 'Windows-specific') |
---|
555 | n/a | def test_UNC_path(self): |
---|
556 | n/a | with open(os.path.join(self.path, 'test_unc_path.py'), 'w') as f: |
---|
557 | n/a | f.write("testdata = 'test_unc_path'") |
---|
558 | n/a | importlib.invalidate_caches() |
---|
559 | n/a | # Create the UNC path, like \\myhost\c$\foo\bar. |
---|
560 | n/a | path = os.path.abspath(self.path) |
---|
561 | n/a | import socket |
---|
562 | n/a | hn = socket.gethostname() |
---|
563 | n/a | drive = path[0] |
---|
564 | n/a | unc = "\\\\%s\\%s$"%(hn, drive) |
---|
565 | n/a | unc += path[2:] |
---|
566 | n/a | try: |
---|
567 | n/a | os.listdir(unc) |
---|
568 | n/a | except OSError as e: |
---|
569 | n/a | if e.errno in (errno.EPERM, errno.EACCES): |
---|
570 | n/a | # See issue #15338 |
---|
571 | n/a | self.skipTest("cannot access administrative share %r" % (unc,)) |
---|
572 | n/a | raise |
---|
573 | n/a | sys.path.insert(0, unc) |
---|
574 | n/a | try: |
---|
575 | n/a | mod = __import__("test_unc_path") |
---|
576 | n/a | except ImportError as e: |
---|
577 | n/a | self.fail("could not import 'test_unc_path' from %r: %r" |
---|
578 | n/a | % (unc, e)) |
---|
579 | n/a | self.assertEqual(mod.testdata, 'test_unc_path') |
---|
580 | n/a | self.assertTrue(mod.__file__.startswith(unc), mod.__file__) |
---|
581 | n/a | unload("test_unc_path") |
---|
582 | n/a | |
---|
583 | n/a | |
---|
584 | n/a | class RelativeImportTests(unittest.TestCase): |
---|
585 | n/a | |
---|
586 | n/a | def tearDown(self): |
---|
587 | n/a | unload("test.relimport") |
---|
588 | n/a | setUp = tearDown |
---|
589 | n/a | |
---|
590 | n/a | def test_relimport_star(self): |
---|
591 | n/a | # This will import * from .test_import. |
---|
592 | n/a | from .. import relimport |
---|
593 | n/a | self.assertTrue(hasattr(relimport, "RelativeImportTests")) |
---|
594 | n/a | |
---|
595 | n/a | def test_issue3221(self): |
---|
596 | n/a | # Note for mergers: the 'absolute' tests from the 2.x branch |
---|
597 | n/a | # are missing in Py3k because implicit relative imports are |
---|
598 | n/a | # a thing of the past |
---|
599 | n/a | # |
---|
600 | n/a | # Regression test for http://bugs.python.org/issue3221. |
---|
601 | n/a | def check_relative(): |
---|
602 | n/a | exec("from . import relimport", ns) |
---|
603 | n/a | |
---|
604 | n/a | # Check relative import OK with __package__ and __name__ correct |
---|
605 | n/a | ns = dict(__package__='test', __name__='test.notarealmodule') |
---|
606 | n/a | check_relative() |
---|
607 | n/a | |
---|
608 | n/a | # Check relative import OK with only __name__ wrong |
---|
609 | n/a | ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') |
---|
610 | n/a | check_relative() |
---|
611 | n/a | |
---|
612 | n/a | # Check relative import fails with only __package__ wrong |
---|
613 | n/a | ns = dict(__package__='foo', __name__='test.notarealmodule') |
---|
614 | n/a | self.assertRaises(SystemError, check_relative) |
---|
615 | n/a | |
---|
616 | n/a | # Check relative import fails with __package__ and __name__ wrong |
---|
617 | n/a | ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
---|
618 | n/a | self.assertRaises(SystemError, check_relative) |
---|
619 | n/a | |
---|
620 | n/a | # Check relative import fails with package set to a non-string |
---|
621 | n/a | ns = dict(__package__=object()) |
---|
622 | n/a | self.assertRaises(TypeError, check_relative) |
---|
623 | n/a | |
---|
624 | n/a | def test_absolute_import_without_future(self): |
---|
625 | n/a | # If explicit relative import syntax is used, then do not try |
---|
626 | n/a | # to perform an absolute import in the face of failure. |
---|
627 | n/a | # Issue #7902. |
---|
628 | n/a | with self.assertRaises(ImportError): |
---|
629 | n/a | from .os import sep |
---|
630 | n/a | self.fail("explicit relative import triggered an " |
---|
631 | n/a | "implicit absolute import") |
---|
632 | n/a | |
---|
633 | n/a | |
---|
634 | n/a | class OverridingImportBuiltinTests(unittest.TestCase): |
---|
635 | n/a | def test_override_builtin(self): |
---|
636 | n/a | # Test that overriding builtins.__import__ can bypass sys.modules. |
---|
637 | n/a | import os |
---|
638 | n/a | |
---|
639 | n/a | def foo(): |
---|
640 | n/a | import os |
---|
641 | n/a | return os |
---|
642 | n/a | self.assertEqual(foo(), os) # Quick sanity check. |
---|
643 | n/a | |
---|
644 | n/a | with swap_attr(builtins, "__import__", lambda *x: 5): |
---|
645 | n/a | self.assertEqual(foo(), 5) |
---|
646 | n/a | |
---|
647 | n/a | # Test what happens when we shadow __import__ in globals(); this |
---|
648 | n/a | # currently does not impact the import process, but if this changes, |
---|
649 | n/a | # other code will need to change, so keep this test as a tripwire. |
---|
650 | n/a | with swap_item(globals(), "__import__", lambda *x: 5): |
---|
651 | n/a | self.assertEqual(foo(), os) |
---|
652 | n/a | |
---|
653 | n/a | |
---|
654 | n/a | class PycacheTests(unittest.TestCase): |
---|
655 | n/a | # Test the various PEP 3147/488-related behaviors. |
---|
656 | n/a | |
---|
657 | n/a | def _clean(self): |
---|
658 | n/a | forget(TESTFN) |
---|
659 | n/a | rmtree('__pycache__') |
---|
660 | n/a | unlink(self.source) |
---|
661 | n/a | |
---|
662 | n/a | def setUp(self): |
---|
663 | n/a | self.source = TESTFN + '.py' |
---|
664 | n/a | self._clean() |
---|
665 | n/a | with open(self.source, 'w') as fp: |
---|
666 | n/a | print('# This is a test file written by test_import.py', file=fp) |
---|
667 | n/a | sys.path.insert(0, os.curdir) |
---|
668 | n/a | importlib.invalidate_caches() |
---|
669 | n/a | |
---|
670 | n/a | def tearDown(self): |
---|
671 | n/a | assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]' |
---|
672 | n/a | del sys.path[0] |
---|
673 | n/a | self._clean() |
---|
674 | n/a | |
---|
675 | n/a | @skip_if_dont_write_bytecode |
---|
676 | n/a | def test_import_pyc_path(self): |
---|
677 | n/a | self.assertFalse(os.path.exists('__pycache__')) |
---|
678 | n/a | __import__(TESTFN) |
---|
679 | n/a | self.assertTrue(os.path.exists('__pycache__')) |
---|
680 | n/a | pyc_path = importlib.util.cache_from_source(self.source) |
---|
681 | n/a | self.assertTrue(os.path.exists(pyc_path), |
---|
682 | n/a | 'bytecode file {!r} for {!r} does not ' |
---|
683 | n/a | 'exist'.format(pyc_path, TESTFN)) |
---|
684 | n/a | |
---|
685 | n/a | @unittest.skipUnless(os.name == 'posix', |
---|
686 | n/a | "test meaningful only on posix systems") |
---|
687 | n/a | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
---|
688 | n/a | "due to varying filesystem permission semantics (issue #11956)") |
---|
689 | n/a | @skip_if_dont_write_bytecode |
---|
690 | n/a | def test_unwritable_directory(self): |
---|
691 | n/a | # When the umask causes the new __pycache__ directory to be |
---|
692 | n/a | # unwritable, the import still succeeds but no .pyc file is written. |
---|
693 | n/a | with temp_umask(0o222): |
---|
694 | n/a | __import__(TESTFN) |
---|
695 | n/a | self.assertTrue(os.path.exists('__pycache__')) |
---|
696 | n/a | pyc_path = importlib.util.cache_from_source(self.source) |
---|
697 | n/a | self.assertFalse(os.path.exists(pyc_path), |
---|
698 | n/a | 'bytecode file {!r} for {!r} ' |
---|
699 | n/a | 'exists'.format(pyc_path, TESTFN)) |
---|
700 | n/a | |
---|
701 | n/a | @skip_if_dont_write_bytecode |
---|
702 | n/a | def test_missing_source(self): |
---|
703 | n/a | # With PEP 3147 cache layout, removing the source but leaving the pyc |
---|
704 | n/a | # file does not satisfy the import. |
---|
705 | n/a | __import__(TESTFN) |
---|
706 | n/a | pyc_file = importlib.util.cache_from_source(self.source) |
---|
707 | n/a | self.assertTrue(os.path.exists(pyc_file)) |
---|
708 | n/a | os.remove(self.source) |
---|
709 | n/a | forget(TESTFN) |
---|
710 | n/a | importlib.invalidate_caches() |
---|
711 | n/a | self.assertRaises(ImportError, __import__, TESTFN) |
---|
712 | n/a | |
---|
713 | n/a | @skip_if_dont_write_bytecode |
---|
714 | n/a | def test_missing_source_legacy(self): |
---|
715 | n/a | # Like test_missing_source() except that for backward compatibility, |
---|
716 | n/a | # when the pyc file lives where the py file would have been (and named |
---|
717 | n/a | # without the tag), it is importable. The __file__ of the imported |
---|
718 | n/a | # module is the pyc location. |
---|
719 | n/a | __import__(TESTFN) |
---|
720 | n/a | # pyc_file gets removed in _clean() via tearDown(). |
---|
721 | n/a | pyc_file = make_legacy_pyc(self.source) |
---|
722 | n/a | os.remove(self.source) |
---|
723 | n/a | unload(TESTFN) |
---|
724 | n/a | importlib.invalidate_caches() |
---|
725 | n/a | m = __import__(TESTFN) |
---|
726 | n/a | self.assertEqual(m.__file__, |
---|
727 | n/a | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
---|
728 | n/a | |
---|
729 | n/a | def test___cached__(self): |
---|
730 | n/a | # Modules now also have an __cached__ that points to the pyc file. |
---|
731 | n/a | m = __import__(TESTFN) |
---|
732 | n/a | pyc_file = importlib.util.cache_from_source(TESTFN + '.py') |
---|
733 | n/a | self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file)) |
---|
734 | n/a | |
---|
735 | n/a | @skip_if_dont_write_bytecode |
---|
736 | n/a | def test___cached___legacy_pyc(self): |
---|
737 | n/a | # Like test___cached__() except that for backward compatibility, |
---|
738 | n/a | # when the pyc file lives where the py file would have been (and named |
---|
739 | n/a | # without the tag), it is importable. The __cached__ of the imported |
---|
740 | n/a | # module is the pyc location. |
---|
741 | n/a | __import__(TESTFN) |
---|
742 | n/a | # pyc_file gets removed in _clean() via tearDown(). |
---|
743 | n/a | pyc_file = make_legacy_pyc(self.source) |
---|
744 | n/a | os.remove(self.source) |
---|
745 | n/a | unload(TESTFN) |
---|
746 | n/a | importlib.invalidate_caches() |
---|
747 | n/a | m = __import__(TESTFN) |
---|
748 | n/a | self.assertEqual(m.__cached__, |
---|
749 | n/a | os.path.join(os.curdir, os.path.relpath(pyc_file))) |
---|
750 | n/a | |
---|
751 | n/a | @skip_if_dont_write_bytecode |
---|
752 | n/a | def test_package___cached__(self): |
---|
753 | n/a | # Like test___cached__ but for packages. |
---|
754 | n/a | def cleanup(): |
---|
755 | n/a | rmtree('pep3147') |
---|
756 | n/a | unload('pep3147.foo') |
---|
757 | n/a | unload('pep3147') |
---|
758 | n/a | os.mkdir('pep3147') |
---|
759 | n/a | self.addCleanup(cleanup) |
---|
760 | n/a | # Touch the __init__.py |
---|
761 | n/a | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
---|
762 | n/a | pass |
---|
763 | n/a | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
---|
764 | n/a | pass |
---|
765 | n/a | importlib.invalidate_caches() |
---|
766 | n/a | m = __import__('pep3147.foo') |
---|
767 | n/a | init_pyc = importlib.util.cache_from_source( |
---|
768 | n/a | os.path.join('pep3147', '__init__.py')) |
---|
769 | n/a | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
---|
770 | n/a | foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py')) |
---|
771 | n/a | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
---|
772 | n/a | os.path.join(os.curdir, foo_pyc)) |
---|
773 | n/a | |
---|
774 | n/a | def test_package___cached___from_pyc(self): |
---|
775 | n/a | # Like test___cached__ but ensuring __cached__ when imported from a |
---|
776 | n/a | # PEP 3147 pyc file. |
---|
777 | n/a | def cleanup(): |
---|
778 | n/a | rmtree('pep3147') |
---|
779 | n/a | unload('pep3147.foo') |
---|
780 | n/a | unload('pep3147') |
---|
781 | n/a | os.mkdir('pep3147') |
---|
782 | n/a | self.addCleanup(cleanup) |
---|
783 | n/a | # Touch the __init__.py |
---|
784 | n/a | with open(os.path.join('pep3147', '__init__.py'), 'w'): |
---|
785 | n/a | pass |
---|
786 | n/a | with open(os.path.join('pep3147', 'foo.py'), 'w'): |
---|
787 | n/a | pass |
---|
788 | n/a | importlib.invalidate_caches() |
---|
789 | n/a | m = __import__('pep3147.foo') |
---|
790 | n/a | unload('pep3147.foo') |
---|
791 | n/a | unload('pep3147') |
---|
792 | n/a | importlib.invalidate_caches() |
---|
793 | n/a | m = __import__('pep3147.foo') |
---|
794 | n/a | init_pyc = importlib.util.cache_from_source( |
---|
795 | n/a | os.path.join('pep3147', '__init__.py')) |
---|
796 | n/a | self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc)) |
---|
797 | n/a | foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py')) |
---|
798 | n/a | self.assertEqual(sys.modules['pep3147.foo'].__cached__, |
---|
799 | n/a | os.path.join(os.curdir, foo_pyc)) |
---|
800 | n/a | |
---|
801 | n/a | def test_recompute_pyc_same_second(self): |
---|
802 | n/a | # Even when the source file doesn't change timestamp, a change in |
---|
803 | n/a | # source size is enough to trigger recomputation of the pyc file. |
---|
804 | n/a | __import__(TESTFN) |
---|
805 | n/a | unload(TESTFN) |
---|
806 | n/a | with open(self.source, 'a') as fp: |
---|
807 | n/a | print("x = 5", file=fp) |
---|
808 | n/a | m = __import__(TESTFN) |
---|
809 | n/a | self.assertEqual(m.x, 5) |
---|
810 | n/a | |
---|
811 | n/a | |
---|
812 | n/a | class TestSymbolicallyLinkedPackage(unittest.TestCase): |
---|
813 | n/a | package_name = 'sample' |
---|
814 | n/a | tagged = package_name + '-tagged' |
---|
815 | n/a | |
---|
816 | n/a | def setUp(self): |
---|
817 | n/a | test.support.rmtree(self.tagged) |
---|
818 | n/a | test.support.rmtree(self.package_name) |
---|
819 | n/a | self.orig_sys_path = sys.path[:] |
---|
820 | n/a | |
---|
821 | n/a | # create a sample package; imagine you have a package with a tag and |
---|
822 | n/a | # you want to symbolically link it from its untagged name. |
---|
823 | n/a | os.mkdir(self.tagged) |
---|
824 | n/a | self.addCleanup(test.support.rmtree, self.tagged) |
---|
825 | n/a | init_file = os.path.join(self.tagged, '__init__.py') |
---|
826 | n/a | test.support.create_empty_file(init_file) |
---|
827 | n/a | assert os.path.exists(init_file) |
---|
828 | n/a | |
---|
829 | n/a | # now create a symlink to the tagged package |
---|
830 | n/a | # sample -> sample-tagged |
---|
831 | n/a | os.symlink(self.tagged, self.package_name, target_is_directory=True) |
---|
832 | n/a | self.addCleanup(test.support.unlink, self.package_name) |
---|
833 | n/a | importlib.invalidate_caches() |
---|
834 | n/a | |
---|
835 | n/a | self.assertEqual(os.path.isdir(self.package_name), True) |
---|
836 | n/a | |
---|
837 | n/a | assert os.path.isfile(os.path.join(self.package_name, '__init__.py')) |
---|
838 | n/a | |
---|
839 | n/a | def tearDown(self): |
---|
840 | n/a | sys.path[:] = self.orig_sys_path |
---|
841 | n/a | |
---|
842 | n/a | # regression test for issue6727 |
---|
843 | n/a | @unittest.skipUnless( |
---|
844 | n/a | not hasattr(sys, 'getwindowsversion') |
---|
845 | n/a | or sys.getwindowsversion() >= (6, 0), |
---|
846 | n/a | "Windows Vista or later required") |
---|
847 | n/a | @test.support.skip_unless_symlink |
---|
848 | n/a | def test_symlinked_dir_importable(self): |
---|
849 | n/a | # make sure sample can only be imported from the current directory. |
---|
850 | n/a | sys.path[:] = ['.'] |
---|
851 | n/a | assert os.path.exists(self.package_name) |
---|
852 | n/a | assert os.path.exists(os.path.join(self.package_name, '__init__.py')) |
---|
853 | n/a | |
---|
854 | n/a | # Try to import the package |
---|
855 | n/a | importlib.import_module(self.package_name) |
---|
856 | n/a | |
---|
857 | n/a | |
---|
858 | n/a | @cpython_only |
---|
859 | n/a | class ImportlibBootstrapTests(unittest.TestCase): |
---|
860 | n/a | # These tests check that importlib is bootstrapped. |
---|
861 | n/a | |
---|
862 | n/a | def test_frozen_importlib(self): |
---|
863 | n/a | mod = sys.modules['_frozen_importlib'] |
---|
864 | n/a | self.assertTrue(mod) |
---|
865 | n/a | |
---|
866 | n/a | def test_frozen_importlib_is_bootstrap(self): |
---|
867 | n/a | from importlib import _bootstrap |
---|
868 | n/a | mod = sys.modules['_frozen_importlib'] |
---|
869 | n/a | self.assertIs(mod, _bootstrap) |
---|
870 | n/a | self.assertEqual(mod.__name__, 'importlib._bootstrap') |
---|
871 | n/a | self.assertEqual(mod.__package__, 'importlib') |
---|
872 | n/a | self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__) |
---|
873 | n/a | |
---|
874 | n/a | def test_frozen_importlib_external_is_bootstrap_external(self): |
---|
875 | n/a | from importlib import _bootstrap_external |
---|
876 | n/a | mod = sys.modules['_frozen_importlib_external'] |
---|
877 | n/a | self.assertIs(mod, _bootstrap_external) |
---|
878 | n/a | self.assertEqual(mod.__name__, 'importlib._bootstrap_external') |
---|
879 | n/a | self.assertEqual(mod.__package__, 'importlib') |
---|
880 | n/a | self.assertTrue(mod.__file__.endswith('_bootstrap_external.py'), mod.__file__) |
---|
881 | n/a | |
---|
882 | n/a | def test_there_can_be_only_one(self): |
---|
883 | n/a | # Issue #15386 revealed a tricky loophole in the bootstrapping |
---|
884 | n/a | # This test is technically redundant, since the bug caused importing |
---|
885 | n/a | # this test module to crash completely, but it helps prove the point |
---|
886 | n/a | from importlib import machinery |
---|
887 | n/a | mod = sys.modules['_frozen_importlib'] |
---|
888 | n/a | self.assertIs(machinery.ModuleSpec, mod.ModuleSpec) |
---|
889 | n/a | |
---|
890 | n/a | |
---|
891 | n/a | @cpython_only |
---|
892 | n/a | class GetSourcefileTests(unittest.TestCase): |
---|
893 | n/a | |
---|
894 | n/a | """Test importlib._bootstrap_external._get_sourcefile() as used by the C API. |
---|
895 | n/a | |
---|
896 | n/a | Because of the peculiarities of the need of this function, the tests are |
---|
897 | n/a | knowingly whitebox tests. |
---|
898 | n/a | |
---|
899 | n/a | """ |
---|
900 | n/a | |
---|
901 | n/a | def test_get_sourcefile(self): |
---|
902 | n/a | # Given a valid bytecode path, return the path to the corresponding |
---|
903 | n/a | # source file if it exists. |
---|
904 | n/a | with mock.patch('importlib._bootstrap_external._path_isfile') as _path_isfile: |
---|
905 | n/a | _path_isfile.return_value = True; |
---|
906 | n/a | path = TESTFN + '.pyc' |
---|
907 | n/a | expect = TESTFN + '.py' |
---|
908 | n/a | self.assertEqual(_get_sourcefile(path), expect) |
---|
909 | n/a | |
---|
910 | n/a | def test_get_sourcefile_no_source(self): |
---|
911 | n/a | # Given a valid bytecode path without a corresponding source path, |
---|
912 | n/a | # return the original bytecode path. |
---|
913 | n/a | with mock.patch('importlib._bootstrap_external._path_isfile') as _path_isfile: |
---|
914 | n/a | _path_isfile.return_value = False; |
---|
915 | n/a | path = TESTFN + '.pyc' |
---|
916 | n/a | self.assertEqual(_get_sourcefile(path), path) |
---|
917 | n/a | |
---|
918 | n/a | def test_get_sourcefile_bad_ext(self): |
---|
919 | n/a | # Given a path with an invalid bytecode extension, return the |
---|
920 | n/a | # bytecode path passed as the argument. |
---|
921 | n/a | path = TESTFN + '.bad_ext' |
---|
922 | n/a | self.assertEqual(_get_sourcefile(path), path) |
---|
923 | n/a | |
---|
924 | n/a | |
---|
925 | n/a | class ImportTracebackTests(unittest.TestCase): |
---|
926 | n/a | |
---|
927 | n/a | def setUp(self): |
---|
928 | n/a | os.mkdir(TESTFN) |
---|
929 | n/a | self.old_path = sys.path[:] |
---|
930 | n/a | sys.path.insert(0, TESTFN) |
---|
931 | n/a | |
---|
932 | n/a | def tearDown(self): |
---|
933 | n/a | sys.path[:] = self.old_path |
---|
934 | n/a | rmtree(TESTFN) |
---|
935 | n/a | |
---|
936 | n/a | def create_module(self, mod, contents, ext=".py"): |
---|
937 | n/a | fname = os.path.join(TESTFN, mod + ext) |
---|
938 | n/a | with open(fname, "w") as f: |
---|
939 | n/a | f.write(contents) |
---|
940 | n/a | self.addCleanup(unload, mod) |
---|
941 | n/a | importlib.invalidate_caches() |
---|
942 | n/a | return fname |
---|
943 | n/a | |
---|
944 | n/a | def assert_traceback(self, tb, files): |
---|
945 | n/a | deduped_files = [] |
---|
946 | n/a | while tb: |
---|
947 | n/a | code = tb.tb_frame.f_code |
---|
948 | n/a | fn = code.co_filename |
---|
949 | n/a | if not deduped_files or fn != deduped_files[-1]: |
---|
950 | n/a | deduped_files.append(fn) |
---|
951 | n/a | tb = tb.tb_next |
---|
952 | n/a | self.assertEqual(len(deduped_files), len(files), deduped_files) |
---|
953 | n/a | for fn, pat in zip(deduped_files, files): |
---|
954 | n/a | self.assertIn(pat, fn) |
---|
955 | n/a | |
---|
956 | n/a | def test_nonexistent_module(self): |
---|
957 | n/a | try: |
---|
958 | n/a | # assertRaises() clears __traceback__ |
---|
959 | n/a | import nonexistent_xyzzy |
---|
960 | n/a | except ImportError as e: |
---|
961 | n/a | tb = e.__traceback__ |
---|
962 | n/a | else: |
---|
963 | n/a | self.fail("ImportError should have been raised") |
---|
964 | n/a | self.assert_traceback(tb, [__file__]) |
---|
965 | n/a | |
---|
966 | n/a | def test_nonexistent_module_nested(self): |
---|
967 | n/a | self.create_module("foo", "import nonexistent_xyzzy") |
---|
968 | n/a | try: |
---|
969 | n/a | import foo |
---|
970 | n/a | except ImportError as e: |
---|
971 | n/a | tb = e.__traceback__ |
---|
972 | n/a | else: |
---|
973 | n/a | self.fail("ImportError should have been raised") |
---|
974 | n/a | self.assert_traceback(tb, [__file__, 'foo.py']) |
---|
975 | n/a | |
---|
976 | n/a | def test_exec_failure(self): |
---|
977 | n/a | self.create_module("foo", "1/0") |
---|
978 | n/a | try: |
---|
979 | n/a | import foo |
---|
980 | n/a | except ZeroDivisionError as e: |
---|
981 | n/a | tb = e.__traceback__ |
---|
982 | n/a | else: |
---|
983 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
984 | n/a | self.assert_traceback(tb, [__file__, 'foo.py']) |
---|
985 | n/a | |
---|
986 | n/a | def test_exec_failure_nested(self): |
---|
987 | n/a | self.create_module("foo", "import bar") |
---|
988 | n/a | self.create_module("bar", "1/0") |
---|
989 | n/a | try: |
---|
990 | n/a | import foo |
---|
991 | n/a | except ZeroDivisionError as e: |
---|
992 | n/a | tb = e.__traceback__ |
---|
993 | n/a | else: |
---|
994 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
995 | n/a | self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py']) |
---|
996 | n/a | |
---|
997 | n/a | # A few more examples from issue #15425 |
---|
998 | n/a | def test_syntax_error(self): |
---|
999 | n/a | self.create_module("foo", "invalid syntax is invalid") |
---|
1000 | n/a | try: |
---|
1001 | n/a | import foo |
---|
1002 | n/a | except SyntaxError as e: |
---|
1003 | n/a | tb = e.__traceback__ |
---|
1004 | n/a | else: |
---|
1005 | n/a | self.fail("SyntaxError should have been raised") |
---|
1006 | n/a | self.assert_traceback(tb, [__file__]) |
---|
1007 | n/a | |
---|
1008 | n/a | def _setup_broken_package(self, parent, child): |
---|
1009 | n/a | pkg_name = "_parent_foo" |
---|
1010 | n/a | self.addCleanup(unload, pkg_name) |
---|
1011 | n/a | pkg_path = os.path.join(TESTFN, pkg_name) |
---|
1012 | n/a | os.mkdir(pkg_path) |
---|
1013 | n/a | # Touch the __init__.py |
---|
1014 | n/a | init_path = os.path.join(pkg_path, '__init__.py') |
---|
1015 | n/a | with open(init_path, 'w') as f: |
---|
1016 | n/a | f.write(parent) |
---|
1017 | n/a | bar_path = os.path.join(pkg_path, 'bar.py') |
---|
1018 | n/a | with open(bar_path, 'w') as f: |
---|
1019 | n/a | f.write(child) |
---|
1020 | n/a | importlib.invalidate_caches() |
---|
1021 | n/a | return init_path, bar_path |
---|
1022 | n/a | |
---|
1023 | n/a | def test_broken_submodule(self): |
---|
1024 | n/a | init_path, bar_path = self._setup_broken_package("", "1/0") |
---|
1025 | n/a | try: |
---|
1026 | n/a | import _parent_foo.bar |
---|
1027 | n/a | except ZeroDivisionError as e: |
---|
1028 | n/a | tb = e.__traceback__ |
---|
1029 | n/a | else: |
---|
1030 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
1031 | n/a | self.assert_traceback(tb, [__file__, bar_path]) |
---|
1032 | n/a | |
---|
1033 | n/a | def test_broken_from(self): |
---|
1034 | n/a | init_path, bar_path = self._setup_broken_package("", "1/0") |
---|
1035 | n/a | try: |
---|
1036 | n/a | from _parent_foo import bar |
---|
1037 | n/a | except ZeroDivisionError as e: |
---|
1038 | n/a | tb = e.__traceback__ |
---|
1039 | n/a | else: |
---|
1040 | n/a | self.fail("ImportError should have been raised") |
---|
1041 | n/a | self.assert_traceback(tb, [__file__, bar_path]) |
---|
1042 | n/a | |
---|
1043 | n/a | def test_broken_parent(self): |
---|
1044 | n/a | init_path, bar_path = self._setup_broken_package("1/0", "") |
---|
1045 | n/a | try: |
---|
1046 | n/a | import _parent_foo.bar |
---|
1047 | n/a | except ZeroDivisionError as e: |
---|
1048 | n/a | tb = e.__traceback__ |
---|
1049 | n/a | else: |
---|
1050 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
1051 | n/a | self.assert_traceback(tb, [__file__, init_path]) |
---|
1052 | n/a | |
---|
1053 | n/a | def test_broken_parent_from(self): |
---|
1054 | n/a | init_path, bar_path = self._setup_broken_package("1/0", "") |
---|
1055 | n/a | try: |
---|
1056 | n/a | from _parent_foo import bar |
---|
1057 | n/a | except ZeroDivisionError as e: |
---|
1058 | n/a | tb = e.__traceback__ |
---|
1059 | n/a | else: |
---|
1060 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
1061 | n/a | self.assert_traceback(tb, [__file__, init_path]) |
---|
1062 | n/a | |
---|
1063 | n/a | @cpython_only |
---|
1064 | n/a | def test_import_bug(self): |
---|
1065 | n/a | # We simulate a bug in importlib and check that it's not stripped |
---|
1066 | n/a | # away from the traceback. |
---|
1067 | n/a | self.create_module("foo", "") |
---|
1068 | n/a | importlib = sys.modules['_frozen_importlib_external'] |
---|
1069 | n/a | if 'load_module' in vars(importlib.SourceLoader): |
---|
1070 | n/a | old_exec_module = importlib.SourceLoader.exec_module |
---|
1071 | n/a | else: |
---|
1072 | n/a | old_exec_module = None |
---|
1073 | n/a | try: |
---|
1074 | n/a | def exec_module(*args): |
---|
1075 | n/a | 1/0 |
---|
1076 | n/a | importlib.SourceLoader.exec_module = exec_module |
---|
1077 | n/a | try: |
---|
1078 | n/a | import foo |
---|
1079 | n/a | except ZeroDivisionError as e: |
---|
1080 | n/a | tb = e.__traceback__ |
---|
1081 | n/a | else: |
---|
1082 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
1083 | n/a | self.assert_traceback(tb, [__file__, '<frozen importlib', __file__]) |
---|
1084 | n/a | finally: |
---|
1085 | n/a | if old_exec_module is None: |
---|
1086 | n/a | del importlib.SourceLoader.exec_module |
---|
1087 | n/a | else: |
---|
1088 | n/a | importlib.SourceLoader.exec_module = old_exec_module |
---|
1089 | n/a | |
---|
1090 | n/a | @unittest.skipUnless(TESTFN_UNENCODABLE, 'need TESTFN_UNENCODABLE') |
---|
1091 | n/a | def test_unencodable_filename(self): |
---|
1092 | n/a | # Issue #11619: The Python parser and the import machinery must not |
---|
1093 | n/a | # encode filenames, especially on Windows |
---|
1094 | n/a | pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass') |
---|
1095 | n/a | self.addCleanup(unlink, pyname) |
---|
1096 | n/a | name = pyname[:-3] |
---|
1097 | n/a | script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name, |
---|
1098 | n/a | __isolated=False) |
---|
1099 | n/a | |
---|
1100 | n/a | |
---|
1101 | n/a | class CircularImportTests(unittest.TestCase): |
---|
1102 | n/a | |
---|
1103 | n/a | """See the docstrings of the modules being imported for the purpose of the |
---|
1104 | n/a | test.""" |
---|
1105 | n/a | |
---|
1106 | n/a | def tearDown(self): |
---|
1107 | n/a | """Make sure no modules pre-exist in sys.modules which are being used to |
---|
1108 | n/a | test.""" |
---|
1109 | n/a | for key in list(sys.modules.keys()): |
---|
1110 | n/a | if key.startswith('test.test_import.data.circular_imports'): |
---|
1111 | n/a | del sys.modules[key] |
---|
1112 | n/a | |
---|
1113 | n/a | def test_direct(self): |
---|
1114 | n/a | try: |
---|
1115 | n/a | import test.test_import.data.circular_imports.basic |
---|
1116 | n/a | except ImportError: |
---|
1117 | n/a | self.fail('circular import through relative imports failed') |
---|
1118 | n/a | |
---|
1119 | n/a | def test_indirect(self): |
---|
1120 | n/a | try: |
---|
1121 | n/a | import test.test_import.data.circular_imports.indirect |
---|
1122 | n/a | except ImportError: |
---|
1123 | n/a | self.fail('relative import in module contributing to circular ' |
---|
1124 | n/a | 'import failed') |
---|
1125 | n/a | |
---|
1126 | n/a | def test_subpackage(self): |
---|
1127 | n/a | try: |
---|
1128 | n/a | import test.test_import.data.circular_imports.subpackage |
---|
1129 | n/a | except ImportError: |
---|
1130 | n/a | self.fail('circular import involving a subpackage failed') |
---|
1131 | n/a | |
---|
1132 | n/a | def test_rebinding(self): |
---|
1133 | n/a | try: |
---|
1134 | n/a | import test.test_import.data.circular_imports.rebinding as rebinding |
---|
1135 | n/a | except ImportError: |
---|
1136 | n/a | self.fail('circular import with rebinding of module attribute failed') |
---|
1137 | n/a | from test.test_import.data.circular_imports.subpkg import util |
---|
1138 | n/a | self.assertIs(util.util, rebinding.util) |
---|
1139 | n/a | |
---|
1140 | n/a | |
---|
1141 | n/a | if __name__ == '__main__': |
---|
1142 | n/a | # Test needs to be a package, so we can do relative imports. |
---|
1143 | n/a | unittest.main() |
---|