Debian Codegraph @

Lib/test/test_import/__init__.py

raw
1 import builtins
2 import errno
3 import glob
4 import json
5 import importlib.util
6 from importlib._bootstrap_external import _get_sourcefile
7 from importlib.machinery import (
8 AppleFrameworkLoader,
9 BuiltinImporter,
10 ExtensionFileLoader,
11 FrozenImporter,
12 SourceFileLoader,
13 )
14 import marshal
15 import os
16 import py_compile
17 import random
18 import shutil
19 import stat
20 import subprocess
21 import sys
22 import textwrap
23 import threading
24 import time
25 import types
26 import unittest
27 from unittest import mock
28 import _imp
29
30 from test.support import os_helper
31 from test.support import (
32 STDLIB_DIR,
33 swap_attr,
34 swap_item,
35 cpython_only,
36 is_apple_mobile,
37 is_emscripten,
38 is_wasm32,
39 run_in_subinterp,
40 run_in_subinterp_with_config,
41 Py_TRACE_REFS,
42 requires_gil_enabled,
43 Py_GIL_DISABLED,
44 no_rerun,
45 force_not_colorized_test_class,
46 catch_unraisable_exception
47 )
48 from test.support.import_helper import (
49 forget, make_legacy_pyc, unlink, unload, ready_to_import,
50 DirsOnSysPath, CleanImport, import_module)
51 from test.support.os_helper import (
52 TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
53 from test.support import script_helper
54 from test.support import threading_helper
55 from test.test_importlib.util import uncache
56 from types import ModuleType
57 try:
58 import _testsinglephase
59 except ImportError:
60 _testsinglephase = None
61 try:
62 import _testmultiphase
63 except ImportError:
64 _testmultiphase = None
65 try:
66 import _interpreters
67 except ModuleNotFoundError:
68 _interpreters = None
69 try:
70 import _testinternalcapi
71 except ImportError:
72 _testinternalcapi = None
73
74
75 skip_if_dont_write_bytecode = unittest.skipIf(
76 sys.dont_write_bytecode,
77 "test meaningful only when writing bytecode")
78
79
80 def _require_loader(module, loader, skip):
81 if isinstance(module, str):
82 module = __import__(module)
83
84 MODULE_KINDS = {
85 BuiltinImporter: 'built-in',
86 ExtensionFileLoader: 'extension',
87 AppleFrameworkLoader: 'framework extension',
88 FrozenImporter: 'frozen',
89 SourceFileLoader: 'pure Python',
90 }
91
92 expected = loader
93 assert isinstance(expected, type), expected
94 expected = MODULE_KINDS[expected]
95
96 actual = module.__spec__.loader
97 if not isinstance(actual, type):
98 actual = type(actual)
99 actual = MODULE_KINDS[actual]
100
101 if actual != expected:
102 err = f'expected module to be {expected}, got {module.__spec__}'
103 if skip:
104 raise unittest.SkipTest(err)
105 raise Exception(err)
106 return module
107
108 def require_builtin(module, *, skip=False):
109 module = _require_loader(module, BuiltinImporter, skip)
110 assert module.__spec__.origin == 'built-in', module.__spec__
111
112 def require_extension(module, *, skip=False):
113 # Apple extensions must be distributed as frameworks. This requires
114 # a specialist loader.
115 if is_apple_mobile:
116 _require_loader(module, AppleFrameworkLoader, skip)
117 else:
118 _require_loader(module, ExtensionFileLoader, skip)
119
120 def require_frozen(module, *, skip=True):
121 module = _require_loader(module, FrozenImporter, skip)
122 assert module.__spec__.origin == 'frozen', module.__spec__
123
124 def require_pure_python(module, *, skip=False):
125 _require_loader(module, SourceFileLoader, skip)
126
127 def create_extension_loader(modname, filename):
128 # Apple extensions must be distributed as frameworks. This requires
129 # a specialist loader.
130 if is_apple_mobile:
131 return AppleFrameworkLoader(modname, filename)
132 else:
133 return ExtensionFileLoader(modname, filename)
134
135 def import_extension_from_file(modname, filename, *, put_in_sys_modules=True):
136 loader = create_extension_loader(modname, filename)
137 spec = importlib.util.spec_from_loader(modname, loader)
138 module = importlib.util.module_from_spec(spec)
139 loader.exec_module(module)
140 if put_in_sys_modules:
141 sys.modules[modname] = module
142 return module
143
144
145 def remove_files(name):
146 for f in (name + ".py",
147 name + ".pyc",
148 name + ".pyw",
149 name + "$py.class"):
150 unlink(f)
151 rmtree('__pycache__')
152
153
154 if _testsinglephase is not None:
155 def restore__testsinglephase(*, _orig=_testsinglephase):
156 # We started with the module imported and want to restore
157 # it to its nominal state.
158 sys.modules.pop('_testsinglephase', None)
159 _orig._clear_globals()
160 origin = _orig.__spec__.origin
161 _testinternalcapi.clear_extension('_testsinglephase', origin)
162 import _testsinglephase
163
164
165 def requires_singlephase_init(meth):
166 """Decorator to skip if single-phase init modules are not supported."""
167 if not isinstance(meth, type):
168 def meth(self, _meth=meth):
169 try:
170 return _meth(self)
171 finally:
172 restore__testsinglephase()
173 meth = cpython_only(meth)
174 msg = "gh-117694: free-threaded build does not currently support single-phase init modules in sub-interpreters"
175 meth = requires_gil_enabled(msg)(meth)
176 return unittest.skipIf(_testsinglephase is None,
177 'test requires _testsinglephase module')(meth)
178
179
180 def requires_subinterpreters(meth):
181 """Decorator to skip a test if subinterpreters are not supported."""
182 return unittest.skipIf(_interpreters is None,
183 'subinterpreters required')(meth)
184
185
186 class ModuleSnapshot(types.SimpleNamespace):
187 """A representation of a module for testing.
188
189 Fields:
190
191 * id - the module's object ID
192 * module - the actual module or an adequate substitute
193 * __file__
194 * __spec__
195 * name
196 * origin
197 * ns - a copy (dict) of the module's __dict__ (or None)
198 * ns_id - the object ID of the module's __dict__
199 * cached - the sys.modules[mod.__spec__.name] entry (or None)
200 * cached_id - the object ID of the sys.modules entry (or None)
201
202 In cases where the value is not available (e.g. due to serialization),
203 the value will be None.
204 """
205 _fields = tuple('id module ns ns_id cached cached_id'.split())
206
207 @classmethod
208 def from_module(cls, mod):
209 name = mod.__spec__.name
210 cached = sys.modules.get(name)
211 return cls(
212 id=id(mod),
213 module=mod,
214 ns=types.SimpleNamespace(**mod.__dict__),
215 ns_id=id(mod.__dict__),
216 cached=cached,
217 cached_id=id(cached),
218 )
219
220 SCRIPT = textwrap.dedent('''
221 {imports}
222
223 name = {name!r}
224
225 {prescript}
226
227 mod = {name}
228
229 {body}
230
231 {postscript}
232 ''')
233 IMPORTS = textwrap.dedent('''
234 import sys
235 ''').strip()
236 SCRIPT_BODY = textwrap.dedent('''
237 # Capture the snapshot data.
238 cached = sys.modules.get(name)
239 snapshot = dict(
240 id=id(mod),
241 module=dict(
242 __file__=mod.__file__,
243 __spec__=dict(
244 name=mod.__spec__.name,
245 origin=mod.__spec__.origin,
246 ),
247 ),
248 ns=None,
249 ns_id=id(mod.__dict__),
250 cached=None,
251 cached_id=id(cached) if cached else None,
252 )
253 ''').strip()
254 CLEANUP_SCRIPT = textwrap.dedent('''
255 # Clean up the module.
256 sys.modules.pop(name, None)
257 ''').strip()
258
259 @classmethod
260 def build_script(cls, name, *,
261 prescript=None,
262 import_first=False,
263 postscript=None,
264 postcleanup=False,
265 ):
266 if postcleanup is True:
267 postcleanup = cls.CLEANUP_SCRIPT
268 elif isinstance(postcleanup, str):
269 postcleanup = textwrap.dedent(postcleanup).strip()
270 postcleanup = cls.CLEANUP_SCRIPT + os.linesep + postcleanup
271 else:
272 postcleanup = ''
273 prescript = textwrap.dedent(prescript).strip() if prescript else ''
274 postscript = textwrap.dedent(postscript).strip() if postscript else ''
275
276 if postcleanup:
277 if postscript:
278 postscript = postscript + os.linesep * 2 + postcleanup
279 else:
280 postscript = postcleanup
281
282 if import_first:
283 prescript += textwrap.dedent(f'''
284
285 # Now import the module.
286 assert name not in sys.modules
287 import {name}''')
288
289 return cls.SCRIPT.format(
290 imports=cls.IMPORTS.strip(),
291 name=name,
292 prescript=prescript.strip(),
293 body=cls.SCRIPT_BODY.strip(),
294 postscript=postscript,
295 )
296
297 @classmethod
298 def parse(cls, text):
299 raw = json.loads(text)
300 mod = raw['module']
301 mod['__spec__'] = types.SimpleNamespace(**mod['__spec__'])
302 raw['module'] = types.SimpleNamespace(**mod)
303 return cls(**raw)
304
305 @classmethod
306 def from_subinterp(cls, name, interpid=None, *, pipe=None, **script_kwds):
307 if pipe is not None:
308 return cls._from_subinterp(name, interpid, pipe, script_kwds)
309 pipe = os.pipe()
310 try:
311 return cls._from_subinterp(name, interpid, pipe, script_kwds)
312 finally:
313 r, w = pipe
314 os.close(r)
315 os.close(w)
316
317 @classmethod
318 def _from_subinterp(cls, name, interpid, pipe, script_kwargs):
319 r, w = pipe
320
321 # Build the script.
322 postscript = textwrap.dedent(f'''
323 # Send the result over the pipe.
324 import json
325 import os
326 os.write({w}, json.dumps(snapshot).encode())
327
328 ''')
329 _postscript = script_kwargs.get('postscript')
330 if _postscript:
331 _postscript = textwrap.dedent(_postscript).lstrip()
332 postscript += _postscript
333 script_kwargs['postscript'] = postscript.strip()
334 script = cls.build_script(name, **script_kwargs)
335
336 # Run the script.
337 if interpid is None:
338 ret = run_in_subinterp(script)
339 if ret != 0:
340 raise AssertionError(f'{ret} != 0')
341 else:
342 _interpreters.run_string(interpid, script)
343
344 # Parse the results.
345 text = os.read(r, 1000)
346 return cls.parse(text.decode())
347
348
349 @force_not_colorized_test_class
350 class ImportTests(unittest.TestCase):
351
352 def setUp(self):
353 remove_files(TESTFN)
354 importlib.invalidate_caches()
355
356 def tearDown(self):
357 unload(TESTFN)
358
359 def test_import_raises_ModuleNotFoundError(self):
360 with self.assertRaises(ModuleNotFoundError):
361 import something_that_should_not_exist_anywhere
362
363 def test_from_import_missing_module_raises_ModuleNotFoundError(self):
364 with self.assertRaises(ModuleNotFoundError):
365 from something_that_should_not_exist_anywhere import blah
366
367 def test_from_import_missing_attr_raises_ImportError(self):
368 with self.assertRaises(ImportError):
369 from importlib import something_that_should_not_exist_anywhere
370
371 def test_from_import_missing_attr_has_name_and_path(self):
372 with CleanImport('os'):
373 import os
374 with self.assertRaises(ImportError) as cm:
375 from os import i_dont_exist
376 self.assertEqual(cm.exception.name, 'os')
377 self.assertEqual(cm.exception.path, os.__file__)
378 self.assertRegex(str(cm.exception), r"cannot import name 'i_dont_exist' from 'os' \(.*os.py\)")
379
380 @cpython_only
381 def test_from_import_missing_attr_has_name_and_so_path(self):
382 _testcapi = import_module("_testcapi")
383 with self.assertRaises(ImportError) as cm:
384 from _testcapi import i_dont_exist
385 self.assertEqual(cm.exception.name, '_testcapi')
386 if hasattr(_testcapi, "__file__"):
387 # The path on the exception is strictly the spec origin, not the
388 # module's __file__. For most cases, these are the same; but on
389 # iOS, the Framework relocation process results in the exception
390 # being raised from the spec location.
391 self.assertEqual(cm.exception.path, _testcapi.__spec__.origin)
392 self.assertRegex(
393 str(cm.exception),
394 r"cannot import name 'i_dont_exist' from '_testcapi' \(.*(\.(so|pyd))?\)"
395 )
396 else:
397 self.assertEqual(
398 str(cm.exception),
399 "cannot import name 'i_dont_exist' from '_testcapi' (unknown location)"
400 )
401
402 def test_from_import_missing_attr_has_name(self):
403 with self.assertRaises(ImportError) as cm:
404 # _warning has no path as it's a built-in module.
405 from _warning import i_dont_exist
406 self.assertEqual(cm.exception.name, '_warning')
407 self.assertIsNone(cm.exception.path)
408
409 def test_from_import_missing_attr_path_is_canonical(self):
410 with self.assertRaises(ImportError) as cm:
411 from os.path import i_dont_exist
412 self.assertIn(cm.exception.name, {'posixpath', 'ntpath'})
413 self.assertIsNotNone(cm.exception)
414
415 def test_from_import_star_invalid_type(self):
416 import re
417 with ready_to_import() as (name, path):
418 with open(path, 'w', encoding='utf-8') as f:
419 f.write("__all__ = [b'invalid_type']")
420 globals = {}
421 with self.assertRaisesRegex(
422 TypeError, f"{re.escape(name)}\\.__all__ must be str"
423 ):
424 exec(f"from {name} import *", globals)
425 self.assertNotIn(b"invalid_type", globals)
426 with ready_to_import() as (name, path):
427 with open(path, 'w', encoding='utf-8') as f:
428 f.write("globals()[b'invalid_type'] = object()")
429 globals = {}
430 with self.assertRaisesRegex(
431 TypeError, f"{re.escape(name)}\\.__dict__ must be str"
432 ):
433 exec(f"from {name} import *", globals)
434 self.assertNotIn(b"invalid_type", globals)
435
436 def test_case_sensitivity(self):
437 # Brief digression to test that import is case-sensitive: if we got
438 # this far, we know for sure that "random" exists.
439 with self.assertRaises(ImportError):
440 import RAnDoM
441
442 def test_double_const(self):
443 # Importing double_const checks that float constants
444 # serialized by marshal as PYC files don't lose precision
445 # (SF bug 422177).
446 from test.test_import.data import double_const
447 unload('test.test_import.data.double_const')
448 from test.test_import.data import double_const # noqa: F811
449
450 def test_import(self):
451 def test_with_extension(ext):
452 # The extension is normally ".py", perhaps ".pyw".
453 source = TESTFN + ext
454 pyc = TESTFN + ".pyc"
455
456 with open(source, "w", encoding='utf-8') as f:
457 print("# This tests Python's ability to import a",
458 ext, "file.", file=f)
459 a = random.randrange(1000)
460 b = random.randrange(1000)
461 print("a =", a, file=f)
462 print("b =", b, file=f)
463
464 if TESTFN in sys.modules:
465 del sys.modules[TESTFN]
466 importlib.invalidate_caches()
467 try:
468 try:
469 mod = __import__(TESTFN)
470 except ImportError as err:
471 self.fail("import from %s failed: %s" % (ext, err))
472
473 self.assertEqual(mod.a, a,
474 "module loaded (%s) but contents invalid" % mod)
475 self.assertEqual(mod.b, b,
476 "module loaded (%s) but contents invalid" % mod)
477 finally:
478 forget(TESTFN)
479 unlink(source)
480 unlink(pyc)
481
482 sys.path.insert(0, os.curdir)
483 try:
484 test_with_extension(".py")
485 if sys.platform.startswith("win"):
486 for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
487 test_with_extension(ext)
488 finally:
489 del sys.path[0]
490
491 def test_module_with_large_stack(self, module='longlist'):
492 # Regression test for http://bugs.python.org/issue561858.
493 filename = module + '.py'
494
495 # Create a file with a list of 65000 elements.
496 with open(filename, 'w', encoding='utf-8') as f:
497 f.write('d = [\n')
498 for i in range(65000):
499 f.write('"",\n')
500 f.write(']')
501
502 try:
503 # Compile & remove .py file; we only need .pyc.
504 # Bytecode must be relocated from the PEP 3147 bytecode-only location.
505 py_compile.compile(filename)
506 finally:
507 unlink(filename)
508
509 # Need to be able to load from current dir.
510 sys.path.append('')
511 importlib.invalidate_caches()
512
513 namespace = {}
514 try:
515 make_legacy_pyc(filename)
516 # This used to crash.
517 exec('import ' + module, None, namespace)
518 finally:
519 # Cleanup.
520 del sys.path[-1]
521 unlink(filename + 'c')
522 unlink(filename + 'o')
523
524 # Remove references to the module (unload the module)
525 namespace.clear()
526 try:
527 del sys.modules[module]
528 except KeyError:
529 pass
530
531 def test_failing_import_sticks(self):
532 source = TESTFN + ".py"
533 with open(source, "w", encoding='utf-8') as f:
534 print("a = 1/0", file=f)
535
536 # New in 2.4, we shouldn't be able to import that no matter how often
537 # we try.
538 sys.path.insert(0, os.curdir)
539 importlib.invalidate_caches()
540 if TESTFN in sys.modules:
541 del sys.modules[TESTFN]
542 try:
543 for i in [1, 2, 3]:
544 self.assertRaises(ZeroDivisionError, __import__, TESTFN)
545 self.assertNotIn(TESTFN, sys.modules,
546 "damaged module in sys.modules on %i try" % i)
547 finally:
548 del sys.path[0]
549 remove_files(TESTFN)
550
551 def test_import_name_binding(self):
552 # import x.y.z binds x in the current namespace
553 import test as x
554 import test.support
555 self.assertIs(x, test, x.__name__)
556 self.assertHasAttr(test.support, "__file__")
557
558 # import x.y.z as w binds z as w
559 import test.support as y
560 self.assertIs(y, test.support, y.__name__)
561
562 def test_issue31286(self):
563 # import in a 'finally' block resulted in SystemError
564 try:
565 x = ...
566 finally:
567 import test.support.script_helper as x
568
569 # import in a 'while' loop resulted in stack overflow
570 i = 0
571 while i < 10:
572 import test.support.script_helper as x
573 i += 1
574
575 # import in a 'for' loop resulted in segmentation fault
576 for i in range(2):
577 import test.support.script_helper as x # noqa: F811
578
579 def test_failing_reload(self):
580 # A failing reload should leave the module object in sys.modules.
581 source = TESTFN + os.extsep + "py"
582 with open(source, "w", encoding='utf-8') as f:
583 f.write("a = 1\nb=2\n")
584
585 sys.path.insert(0, os.curdir)
586 try:
587 mod = __import__(TESTFN)
588 self.assertIn(TESTFN, sys.modules)
589 self.assertEqual(mod.a, 1, "module has wrong attribute values")
590 self.assertEqual(mod.b, 2, "module has wrong attribute values")
591
592 # On WinXP, just replacing the .py file wasn't enough to
593 # convince reload() to reparse it. Maybe the timestamp didn't
594 # move enough. We force it to get reparsed by removing the
595 # compiled file too.
596 remove_files(TESTFN)
597
598 # Now damage the module.
599 with open(source, "w", encoding='utf-8') as f:
600 f.write("a = 10\nb=20//0\n")
601
602 self.assertRaises(ZeroDivisionError, importlib.reload, mod)
603 # But we still expect the module to be in sys.modules.
604 mod = sys.modules.get(TESTFN)
605 self.assertIsNotNone(mod, "expected module to be in sys.modules")
606
607 # We should have replaced a w/ 10, but the old b value should
608 # stick.
609 self.assertEqual(mod.a, 10, "module has wrong attribute values")
610 self.assertEqual(mod.b, 2, "module has wrong attribute values")
611
612 finally:
613 del sys.path[0]
614 remove_files(TESTFN)
615 unload(TESTFN)
616
617 @skip_if_dont_write_bytecode
618 def test_file_to_source(self):
619 # check if __file__ points to the source file where available
620 source = TESTFN + ".py"
621 with open(source, "w", encoding='utf-8') as f:
622 f.write("test = None\n")
623
624 sys.path.insert(0, os.curdir)
625 try:
626 mod = __import__(TESTFN)
627 self.assertEndsWith(mod.__file__, '.py')
628 os.remove(source)
629 del sys.modules[TESTFN]
630 make_legacy_pyc(source)
631 importlib.invalidate_caches()
632 mod = __import__(TESTFN)
633 base, ext = os.path.splitext(mod.__file__)
634 self.assertEqual(ext, '.pyc')
635 finally:
636 del sys.path[0]
637 remove_files(TESTFN)
638 if TESTFN in sys.modules:
639 del sys.modules[TESTFN]
640
641 def test_import_by_filename(self):
642 path = os.path.abspath(TESTFN)
643 encoding = sys.getfilesystemencoding()
644 try:
645 path.encode(encoding)
646 except UnicodeEncodeError:
647 self.skipTest('path is not encodable to {}'.format(encoding))
648 with self.assertRaises(ImportError) as c:
649 __import__(path)
650
651 def test_import_in_del_does_not_crash(self):
652 # Issue 4236
653 testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
654 import sys
655 class C:
656 def __del__(self):
657 import importlib
658 sys.argv.insert(0, C())
659 """))
660 script_helper.assert_python_ok(testfn)
661
662 @skip_if_dont_write_bytecode
663 def test_timestamp_overflow(self):
664 # A modification timestamp larger than 2**32 should not be a problem
665 # when importing a module (issue #11235).
666 sys.path.insert(0, os.curdir)
667 try:
668 source = TESTFN + ".py"
669 compiled = importlib.util.cache_from_source(source)
670 with open(source, 'w', encoding='utf-8') as f:
671 pass
672 try:
673 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
674 except OverflowError:
675 self.skipTest("cannot set modification time to large integer")
676 except OSError as e:
677 if e.errno not in (getattr(errno, 'EOVERFLOW', None),
678 getattr(errno, 'EINVAL', None)):
679 raise
680 self.skipTest("cannot set modification time to large integer ({})".format(e))
681 __import__(TESTFN)
682 # The pyc file was created.
683 os.stat(compiled)
684 finally:
685 del sys.path[0]
686 remove_files(TESTFN)
687
688 def test_bogus_fromlist(self):
689 try:
690 __import__('http', fromlist=['blah'])
691 except ImportError:
692 self.fail("fromlist must allow bogus names")
693
694 @cpython_only
695 def test_delete_builtins_import(self):
696 args = ["-c", "del __builtins__.__import__; import os"]
697 popen = script_helper.spawn_python(*args)
698 stdout, stderr = popen.communicate()
699 self.assertIn(b"ImportError", stdout)
700
701 def test_from_import_message_for_nonexistent_module(self):
702 with self.assertRaisesRegex(ImportError, "^No module named 'bogus'"):
703 from bogus import foo
704
705 def test_from_import_message_for_existing_module(self):
706 with self.assertRaisesRegex(ImportError, "^cannot import name 'bogus'"):
707 from re import bogus
708
709 def test_from_import_AttributeError(self):
710 # Issue #24492: trying to import an attribute that raises an
711 # AttributeError should lead to an ImportError.
712 class AlwaysAttributeError:
713 def __getattr__(self, _):
714 raise AttributeError
715
716 module_name = 'test_from_import_AttributeError'
717 self.addCleanup(unload, module_name)
718 sys.modules[module_name] = AlwaysAttributeError()
719 with self.assertRaises(ImportError) as cm:
720 from test_from_import_AttributeError import does_not_exist
721
722 self.assertEqual(str(cm.exception),
723 "cannot import name 'does_not_exist' from '<unknown module name>' (unknown location)")
724
725 @cpython_only
726 def test_issue31492(self):
727 # There shouldn't be an assertion failure in case of failing to import
728 # from a module with a bad __name__ attribute, or in case of failing
729 # to access an attribute of such a module.
730 with swap_attr(os, '__name__', None):
731 with self.assertRaises(ImportError):
732 from os import does_not_exist
733
734 with self.assertRaises(AttributeError):
735 os.does_not_exist
736
737 @threading_helper.requires_working_threading()
738 def test_concurrency(self):
739 # bpo 38091: this is a hack to slow down the code that calls
740 # has_deadlock(); the logic was itself sometimes deadlocking.
741 def delay_has_deadlock(frame, event, arg):
742 if event == 'call' and frame.f_code.co_name == 'has_deadlock':
743 time.sleep(0.1)
744
745 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))
746 try:
747 exc = None
748 def run():
749 sys.settrace(delay_has_deadlock)
750 event.wait()
751 try:
752 import package
753 except BaseException as e:
754 nonlocal exc
755 exc = e
756 sys.settrace(None)
757
758 for i in range(10):
759 event = threading.Event()
760 threads = [threading.Thread(target=run) for x in range(2)]
761 try:
762 with threading_helper.start_threads(threads, event.set):
763 time.sleep(0)
764 finally:
765 sys.modules.pop('package', None)
766 sys.modules.pop('package.submodule', None)
767 if exc is not None:
768 raise exc
769 finally:
770 del sys.path[0]
771
772 @unittest.skipUnless(sys.platform == "win32", "Windows-specific")
773 def test_dll_dependency_import(self):
774 from _winapi import GetModuleFileName
775 dllname = GetModuleFileName(sys.dllhandle)
776 pydname = importlib.util.find_spec("_sqlite3").origin
777 depname = os.path.join(
778 os.path.dirname(pydname),
779 "sqlite3{}.dll".format("_d" if "_d" in pydname else ""))
780
781 with os_helper.temp_dir() as tmp:
782 tmp2 = os.path.join(tmp, "DLLs")
783 os.mkdir(tmp2)
784
785 pyexe = os.path.join(tmp, os.path.basename(sys.executable))
786 shutil.copy(sys.executable, pyexe)
787 shutil.copy(dllname, tmp)
788 for f in glob.glob(os.path.join(glob.escape(sys.prefix), "vcruntime*.dll")):
789 shutil.copy(f, tmp)
790
791 shutil.copy(pydname, tmp2)
792
793 env = None
794 env = {k.upper(): os.environ[k] for k in os.environ}
795 env["PYTHONPATH"] = tmp2 + ";" + STDLIB_DIR
796
797 # Test 1: import with added DLL directory
798 subprocess.check_call([
799 pyexe, "-Sc", ";".join([
800 "import os",
801 "p = os.add_dll_directory({!r})".format(
802 os.path.dirname(depname)),
803 "import _sqlite3",
804 "p.close"
805 ])],
806 stderr=subprocess.STDOUT,
807 env=env,
808 cwd=os.path.dirname(pyexe))
809
810 # Test 2: import with DLL adjacent to PYD
811 shutil.copy(depname, tmp2)
812 subprocess.check_call([pyexe, "-Sc", "import _sqlite3"],
813 stderr=subprocess.STDOUT,
814 env=env,
815 cwd=os.path.dirname(pyexe))
816
817 def test_issue105979(self):
818 # this used to crash
819 with self.assertRaises(ImportError) as cm:
820 _imp.get_frozen_object("x", b"6\'\xd5Cu\x12")
821 self.assertIn("Frozen object named 'x' is invalid",
822 str(cm.exception))
823
824 def test_frozen_module_from_import_error(self):
825 with self.assertRaises(ImportError) as cm:
826 from os import this_will_never_exist
827 self.assertIn(
828 f"cannot import name 'this_will_never_exist' from 'os' ({os.__file__})",
829 str(cm.exception),
830 )
831 with self.assertRaises(ImportError) as cm:
832 from sys import this_will_never_exist
833 self.assertIn(
834 "cannot import name 'this_will_never_exist' from 'sys' (unknown location)",
835 str(cm.exception),
836 )
837
838 scripts = [
839 """
840 import os
841 os.__spec__.has_location = False
842 os.__file__ = []
843 from os import this_will_never_exist
844 """,
845 """
846 import os
847 os.__spec__.has_location = False
848 del os.__file__
849 from os import this_will_never_exist
850 """,
851 """
852 import os
853 os.__spec__.origin = []
854 os.__file__ = []
855 from os import this_will_never_exist
856 """
857 ]
858 for script in scripts:
859 with self.subTest(script=script):
860 expected_error = (
861 b"cannot import name 'this_will_never_exist' "
862 b"from 'os' (unknown location)"
863 )
864 popen = script_helper.spawn_python("-c", script)
865 stdout, stderr = popen.communicate()
866 self.assertIn(expected_error, stdout)
867
868 def test_non_module_from_import_error(self):
869 prefix = """
870 import sys
871 class NotAModule: ...
872 nm = NotAModule()
873 nm.symbol = 123
874 sys.modules["not_a_module"] = nm
875 from not_a_module import symbol
876 """
877 scripts = [
878 prefix + "from not_a_module import missing_symbol",
879 prefix + "nm.__spec__ = []\nfrom not_a_module import missing_symbol",
880 ]
881 for script in scripts:
882 with self.subTest(script=script):
883 expected_error = (
884 b"ImportError: cannot import name 'missing_symbol' from "
885 b"'<unknown module name>' (unknown location)"
886 )
887 popen = script_helper.spawn_python("-c", script)
888 stdout, stderr = popen.communicate()
889 self.assertIn(expected_error, stdout)
890
891 def test_script_shadowing_stdlib(self):
892 script_errors = [
893 (
894 "import fractions\nfractions.Fraction",
895 rb"AttributeError: module 'fractions' has no attribute 'Fraction'"
896 ),
897 (
898 "from fractions import Fraction",
899 rb"ImportError: cannot import name 'Fraction' from 'fractions'"
900 )
901 ]
902 for script, error in script_errors:
903 with self.subTest(script=script), os_helper.temp_dir() as tmp:
904 with open(os.path.join(tmp, "fractions.py"), "w", encoding='utf-8') as f:
905 f.write(script)
906
907 expected_error = error + (
908 rb" \(consider renaming '.*fractions.py' since it has the "
909 rb"same name as the standard library module named 'fractions' "
910 rb"and prevents importing that standard library module\)"
911 )
912
913 popen = script_helper.spawn_python(os.path.join(tmp, "fractions.py"), cwd=tmp)
914 stdout, stderr = popen.communicate()
915 self.assertRegex(stdout, expected_error)
916
917 popen = script_helper.spawn_python('-m', 'fractions', cwd=tmp)
918 stdout, stderr = popen.communicate()
919 self.assertRegex(stdout, expected_error)
920
921 popen = script_helper.spawn_python('-c', 'import fractions', cwd=tmp)
922 stdout, stderr = popen.communicate()
923 self.assertRegex(stdout, expected_error)
924
925 # and there's no error at all when using -P
926 popen = script_helper.spawn_python('-P', 'fractions.py', cwd=tmp)
927 stdout, stderr = popen.communicate()
928 self.assertEqual(stdout, b'')
929
930 tmp_child = os.path.join(tmp, "child")
931 os.mkdir(tmp_child)
932
933 # test the logic with different cwd
934 popen = script_helper.spawn_python(os.path.join(tmp, "fractions.py"), cwd=tmp_child)
935 stdout, stderr = popen.communicate()
936 self.assertRegex(stdout, expected_error)
937
938 popen = script_helper.spawn_python('-m', 'fractions', cwd=tmp_child)
939 stdout, stderr = popen.communicate()
940 self.assertEqual(stdout, b'') # no error
941
942 popen = script_helper.spawn_python('-c', 'import fractions', cwd=tmp_child)
943 stdout, stderr = popen.communicate()
944 self.assertEqual(stdout, b'') # no error
945
946 def test_package_shadowing_stdlib_module(self):
947 script_errors = [
948 (
949 "fractions.Fraction",
950 rb"AttributeError: module 'fractions' has no attribute 'Fraction'"
951 ),
952 (
953 "from fractions import Fraction",
954 rb"ImportError: cannot import name 'Fraction' from 'fractions'"
955 )
956 ]
957 for script, error in script_errors:
958 with self.subTest(script=script), os_helper.temp_dir() as tmp:
959 os.mkdir(os.path.join(tmp, "fractions"))
960 with open(
961 os.path.join(tmp, "fractions", "__init__.py"), "w", encoding='utf-8'
962 ) as f:
963 f.write("shadowing_module = True")
964 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
965 f.write("import fractions; fractions.shadowing_module\n")
966 f.write(script)
967
968 expected_error = error + (
969 rb" \(consider renaming '.*[\\/]fractions[\\/]+__init__.py' since it has the "
970 rb"same name as the standard library module named 'fractions' "
971 rb"and prevents importing that standard library module\)"
972 )
973
974 popen = script_helper.spawn_python(os.path.join(tmp, "main.py"), cwd=tmp)
975 stdout, stderr = popen.communicate()
976 self.assertRegex(stdout, expected_error)
977
978 popen = script_helper.spawn_python('-m', 'main', cwd=tmp)
979 stdout, stderr = popen.communicate()
980 self.assertRegex(stdout, expected_error)
981
982 # and there's no shadowing at all when using -P
983 popen = script_helper.spawn_python('-P', 'main.py', cwd=tmp)
984 stdout, stderr = popen.communicate()
985 self.assertRegex(stdout, b"module 'fractions' has no attribute 'shadowing_module'")
986
987 def test_script_shadowing_third_party(self):
988 script_errors = [
989 (
990 "import numpy\nnumpy.array",
991 rb"AttributeError: module 'numpy' has no attribute 'array'"
992 ),
993 (
994 "from numpy import array",
995 rb"ImportError: cannot import name 'array' from 'numpy'"
996 )
997 ]
998 for script, error in script_errors:
999 with self.subTest(script=script), os_helper.temp_dir() as tmp:
1000 with open(os.path.join(tmp, "numpy.py"), "w", encoding='utf-8') as f:
1001 f.write(script)
1002
1003 expected_error = error + (
1004 rb" \(consider renaming '.*numpy.py' if it has the "
1005 rb"same name as a library you intended to import\)\s+\z"
1006 )
1007
1008 popen = script_helper.spawn_python(os.path.join(tmp, "numpy.py"))
1009 stdout, stderr = popen.communicate()
1010 self.assertRegex(stdout, expected_error)
1011
1012 popen = script_helper.spawn_python('-m', 'numpy', cwd=tmp)
1013 stdout, stderr = popen.communicate()
1014 self.assertRegex(stdout, expected_error)
1015
1016 popen = script_helper.spawn_python('-c', 'import numpy', cwd=tmp)
1017 stdout, stderr = popen.communicate()
1018 self.assertRegex(stdout, expected_error)
1019
1020 def test_script_maybe_not_shadowing_third_party(self):
1021 with os_helper.temp_dir() as tmp:
1022 with open(os.path.join(tmp, "numpy.py"), "w", encoding='utf-8') as f:
1023 f.write("this_script_does_not_attempt_to_import_numpy = True")
1024
1025 expected_error = (
1026 rb"AttributeError: module 'numpy' has no attribute 'attr'\s+\z"
1027 )
1028 popen = script_helper.spawn_python('-c', 'import numpy; numpy.attr', cwd=tmp)
1029 stdout, stderr = popen.communicate()
1030 self.assertRegex(stdout, expected_error)
1031
1032 expected_error = (
1033 rb"ImportError: cannot import name 'attr' from 'numpy' \(.*\)\s+\z"
1034 )
1035 popen = script_helper.spawn_python('-c', 'from numpy import attr', cwd=tmp)
1036 stdout, stderr = popen.communicate()
1037 self.assertRegex(stdout, expected_error)
1038
1039 def test_script_shadowing_stdlib_edge_cases(self):
1040 with os_helper.temp_dir() as tmp:
1041 with open(os.path.join(tmp, "fractions.py"), "w", encoding='utf-8') as f:
1042 f.write("shadowing_module = True")
1043
1044 # Unhashable str subclass
1045 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1046 f.write("""
1047 import fractions
1048 fractions.shadowing_module
1049 class substr(str):
1050 __hash__ = None
1051 fractions.__name__ = substr('fractions')
1052 try:
1053 fractions.Fraction
1054 except TypeError as e:
1055 print(str(e))
1056 """)
1057 popen = script_helper.spawn_python("main.py", cwd=tmp)
1058 stdout, stderr = popen.communicate()
1059 self.assertIn(b"unhashable type: 'substr'", stdout.rstrip())
1060
1061 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1062 f.write("""
1063 import fractions
1064 fractions.shadowing_module
1065 class substr(str):
1066 __hash__ = None
1067 fractions.__name__ = substr('fractions')
1068 try:
1069 from fractions import Fraction
1070 except TypeError as e:
1071 print(str(e))
1072 """)
1073
1074 popen = script_helper.spawn_python("main.py", cwd=tmp)
1075 stdout, stderr = popen.communicate()
1076 self.assertIn(b"unhashable type: 'substr'", stdout.rstrip())
1077
1078 # Various issues with sys module
1079 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1080 f.write("""
1081 import fractions
1082 fractions.shadowing_module
1083
1084 import sys
1085 sys.stdlib_module_names = None
1086 try:
1087 fractions.Fraction
1088 except AttributeError as e:
1089 print(str(e))
1090
1091 del sys.stdlib_module_names
1092 try:
1093 fractions.Fraction
1094 except AttributeError as e:
1095 print(str(e))
1096
1097 sys.path = [0]
1098 try:
1099 fractions.Fraction
1100 except AttributeError as e:
1101 print(str(e))
1102 """)
1103 popen = script_helper.spawn_python("main.py", cwd=tmp)
1104 stdout, stderr = popen.communicate()
1105 lines = stdout.splitlines()
1106 self.assertEqual(len(lines), 3)
1107 for line in lines:
1108 self.assertEqual(line, b"module 'fractions' has no attribute 'Fraction'")
1109
1110 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1111 f.write("""
1112 import fractions
1113 fractions.shadowing_module
1114
1115 import sys
1116 sys.stdlib_module_names = None
1117 try:
1118 from fractions import Fraction
1119 except ImportError as e:
1120 print(str(e))
1121
1122 del sys.stdlib_module_names
1123 try:
1124 from fractions import Fraction
1125 except ImportError as e:
1126 print(str(e))
1127
1128 sys.path = [0]
1129 try:
1130 from fractions import Fraction
1131 except ImportError as e:
1132 print(str(e))
1133 """)
1134 popen = script_helper.spawn_python("main.py", cwd=tmp)
1135 stdout, stderr = popen.communicate()
1136 lines = stdout.splitlines()
1137 self.assertEqual(len(lines), 3)
1138 for line in lines:
1139 self.assertRegex(line, rb"cannot import name 'Fraction' from 'fractions' \(.*\)")
1140
1141 # Various issues with origin
1142 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1143 f.write("""
1144 import fractions
1145 fractions.shadowing_module
1146 del fractions.__spec__.origin
1147 try:
1148 fractions.Fraction
1149 except AttributeError as e:
1150 print(str(e))
1151
1152 fractions.__spec__.origin = []
1153 try:
1154 fractions.Fraction
1155 except AttributeError as e:
1156 print(str(e))
1157 """)
1158
1159 popen = script_helper.spawn_python("main.py", cwd=tmp)
1160 stdout, stderr = popen.communicate()
1161 lines = stdout.splitlines()
1162 self.assertEqual(len(lines), 2)
1163 for line in lines:
1164 self.assertEqual(line, b"module 'fractions' has no attribute 'Fraction'")
1165
1166 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1167 f.write("""
1168 import fractions
1169 fractions.shadowing_module
1170 del fractions.__spec__.origin
1171 try:
1172 from fractions import Fraction
1173 except ImportError as e:
1174 print(str(e))
1175
1176 fractions.__spec__.origin = []
1177 try:
1178 from fractions import Fraction
1179 except ImportError as e:
1180 print(str(e))
1181 """)
1182 popen = script_helper.spawn_python("main.py", cwd=tmp)
1183 stdout, stderr = popen.communicate()
1184 lines = stdout.splitlines()
1185 self.assertEqual(len(lines), 2)
1186 for line in lines:
1187 self.assertRegex(line, rb"cannot import name 'Fraction' from 'fractions' \(.*\)")
1188
1189 @unittest.skipIf(sys.platform == 'win32', 'Cannot delete cwd on Windows')
1190 @unittest.skipIf(sys.platform == 'sunos5', 'Cannot delete cwd on Solaris/Illumos')
1191 @unittest.skipIf(sys.platform.startswith('aix'), 'Cannot delete cwd on AIX')
1192 def test_script_shadowing_stdlib_cwd_failure(self):
1193 with os_helper.temp_dir() as tmp:
1194 subtmp = os.path.join(tmp, "subtmp")
1195 os.mkdir(subtmp)
1196 with open(os.path.join(subtmp, "main.py"), "w", encoding='utf-8') as f:
1197 f.write(f"""
1198 import sys
1199 assert sys.path[0] == ''
1200
1201 import os
1202 import shutil
1203 shutil.rmtree(os.getcwd())
1204
1205 os.does_not_exist
1206 """)
1207 # Use -c to ensure sys.path[0] is ""
1208 popen = script_helper.spawn_python("-c", "import main", cwd=subtmp)
1209 stdout, stderr = popen.communicate()
1210 expected_error = rb"AttributeError: module 'os' has no attribute 'does_not_exist'"
1211 self.assertRegex(stdout, expected_error)
1212
1213 def test_script_shadowing_stdlib_sys_path_modification(self):
1214 script_errors = [
1215 (
1216 "import fractions\nfractions.Fraction",
1217 rb"AttributeError: module 'fractions' has no attribute 'Fraction'"
1218 ),
1219 (
1220 "from fractions import Fraction",
1221 rb"ImportError: cannot import name 'Fraction' from 'fractions'"
1222 )
1223 ]
1224 for script, error in script_errors:
1225 with self.subTest(script=script), os_helper.temp_dir() as tmp:
1226 with open(os.path.join(tmp, "fractions.py"), "w", encoding='utf-8') as f:
1227 f.write("shadowing_module = True")
1228 with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
1229 f.write('import sys; sys.path.insert(0, "this_folder_does_not_exist")\n')
1230 f.write(script)
1231 expected_error = error + (
1232 rb" \(consider renaming '.*fractions.py' since it has the "
1233 rb"same name as the standard library module named 'fractions' "
1234 rb"and prevents importing that standard library module\)"
1235 )
1236
1237 popen = script_helper.spawn_python("main.py", cwd=tmp)
1238 stdout, stderr = popen.communicate()
1239 self.assertRegex(stdout, expected_error)
1240
1241 def test_create_dynamic_null(self):
1242 with self.assertRaisesRegex(ValueError, 'embedded null character'):
1243 class Spec:
1244 name = "a\x00b"
1245 origin = "abc"
1246 _imp.create_dynamic(Spec())
1247
1248 with self.assertRaisesRegex(ValueError, 'embedded null character'):
1249 class Spec2:
1250 name = "abc"
1251 origin = "a\x00b"
1252 _imp.create_dynamic(Spec2())
1253
1254
1255 @skip_if_dont_write_bytecode
1256 class FilePermissionTests(unittest.TestCase):
1257 # tests for file mode on cached .pyc files
1258
1259 @unittest.skipUnless(os.name == 'posix',
1260 "test meaningful only on posix systems")
1261 @unittest.skipIf(
1262 is_wasm32,
1263 "Emscripten's/WASI's umask is a stub."
1264 )
1265 def test_creation_mode(self):
1266 mask = 0o022
1267 with temp_umask(mask), ready_to_import() as (name, path):
1268 cached_path = importlib.util.cache_from_source(path)
1269 module = __import__(name)
1270 if not os.path.exists(cached_path):
1271 self.fail("__import__ did not result in creation of "
1272 "a .pyc file")
1273 stat_info = os.stat(cached_path)
1274
1275 # Check that the umask is respected, and the executable bits
1276 # aren't set.
1277 self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)),
1278 oct(0o666 & ~mask))
1279
1280 @unittest.skipUnless(os.name == 'posix',
1281 "test meaningful only on posix systems")
1282 @os_helper.skip_unless_working_chmod
1283 def test_cached_mode_issue_2051(self):
1284 # permissions of .pyc should match those of .py, regardless of mask
1285 mode = 0o600
1286 with temp_umask(0o022), ready_to_import() as (name, path):
1287 cached_path = importlib.util.cache_from_source(path)
1288 os.chmod(path, mode)
1289 __import__(name)
1290 if not os.path.exists(cached_path):
1291 self.fail("__import__ did not result in creation of "
1292 "a .pyc file")
1293 stat_info = os.stat(cached_path)
1294
1295 self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(mode))
1296
1297 @unittest.skipUnless(os.name == 'posix',
1298 "test meaningful only on posix systems")
1299 @os_helper.skip_unless_working_chmod
1300 def test_cached_readonly(self):
1301 mode = 0o400
1302 with temp_umask(0o022), ready_to_import() as (name, path):
1303 cached_path = importlib.util.cache_from_source(path)
1304 os.chmod(path, mode)
1305 __import__(name)
1306 if not os.path.exists(cached_path):
1307 self.fail("__import__ did not result in creation of "
1308 "a .pyc file")
1309 stat_info = os.stat(cached_path)
1310
1311 expected = mode | 0o200 # Account for fix for issue #6074
1312 self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(expected))
1313
1314 def test_pyc_always_writable(self):
1315 # Initially read-only .pyc files on Windows used to cause problems
1316 # with later updates, see issue #6074 for details
1317 with ready_to_import() as (name, path):
1318 # Write a Python file, make it read-only and import it
1319 with open(path, 'w', encoding='utf-8') as f:
1320 f.write("x = 'original'\n")
1321 # Tweak the mtime of the source to ensure pyc gets updated later
1322 s = os.stat(path)
1323 os.utime(path, (s.st_atime, s.st_mtime-100000000))
1324 os.chmod(path, 0o400)
1325 m = __import__(name)
1326 self.assertEqual(m.x, 'original')
1327 # Change the file and then reimport it
1328 os.chmod(path, 0o600)
1329 with open(path, 'w', encoding='utf-8') as f:
1330 f.write("x = 'rewritten'\n")
1331 unload(name)
1332 importlib.invalidate_caches()
1333 m = __import__(name)
1334 self.assertEqual(m.x, 'rewritten')
1335 # Now delete the source file and check the pyc was rewritten
1336 unlink(path)
1337 unload(name)
1338 importlib.invalidate_caches()
1339 bytecode_only = path + "c"
1340 os.rename(importlib.util.cache_from_source(path), bytecode_only)
1341 m = __import__(name)
1342 self.assertEqual(m.x, 'rewritten')
1343
1344
1345 class PycRewritingTests(unittest.TestCase):
1346 # Test that the `co_filename` attribute on code objects always points
1347 # to the right file, even when various things happen (e.g. both the .py
1348 # and the .pyc file are renamed).
1349
1350 module_name = "unlikely_module_name"
1351 module_source = """
1352 import sys
1353 code_filename = sys._getframe().f_code.co_filename
1354 module_filename = __file__
1355 constant = 1000
1356 def func():
1357 pass
1358 func_filename = func.__code__.co_filename
1359 """
1360 dir_name = os.path.abspath(TESTFN)
1361 file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
1362 compiled_name = importlib.util.cache_from_source(file_name)
1363
1364 def setUp(self):
1365 self.sys_path = sys.path[:]
1366 self.orig_module = sys.modules.pop(self.module_name, None)
1367 os.mkdir(self.dir_name)
1368 with open(self.file_name, "w", encoding='utf-8') as f:
1369 f.write(self.module_source)
1370 sys.path.insert(0, self.dir_name)
1371 importlib.invalidate_caches()
1372
1373 def tearDown(self):
1374 sys.path[:] = self.sys_path
1375 if self.orig_module is not None:
1376 sys.modules[self.module_name] = self.orig_module
1377 else:
1378 unload(self.module_name)
1379 unlink(self.file_name)
1380 unlink(self.compiled_name)
1381 rmtree(self.dir_name)
1382
1383 def import_module(self):
1384 ns = globals()
1385 __import__(self.module_name, ns, ns)
1386 return sys.modules[self.module_name]
1387
1388 def test_basics(self):
1389 mod = self.import_module()
1390 self.assertEqual(mod.module_filename, self.file_name)
1391 self.assertEqual(mod.code_filename, self.file_name)
1392 self.assertEqual(mod.func_filename, self.file_name)
1393 del sys.modules[self.module_name]
1394 mod = self.import_module()
1395 self.assertEqual(mod.module_filename, self.file_name)
1396 self.assertEqual(mod.code_filename, self.file_name)
1397 self.assertEqual(mod.func_filename, self.file_name)
1398
1399 def test_incorrect_code_name(self):
1400 py_compile.compile(self.file_name, dfile="another_module.py")
1401 mod = self.import_module()
1402 self.assertEqual(mod.module_filename, self.file_name)
1403 self.assertEqual(mod.code_filename, self.file_name)
1404 self.assertEqual(mod.func_filename, self.file_name)
1405
1406 def test_module_without_source(self):
1407 target = "another_module.py"
1408 py_compile.compile(self.file_name, dfile=target)
1409 os.remove(self.file_name)
1410 pyc_file = make_legacy_pyc(self.file_name)
1411 importlib.invalidate_caches()
1412 mod = self.import_module()
1413 self.assertEqual(mod.module_filename, pyc_file)
1414 self.assertEqual(mod.code_filename, target)
1415 self.assertEqual(mod.func_filename, target)
1416
1417 def test_foreign_code(self):
1418 py_compile.compile(self.file_name)
1419 with open(self.compiled_name, "rb") as f:
1420 header = f.read(16)
1421 code = marshal.load(f)
1422 constants = list(code.co_consts)
1423 foreign_code = importlib.import_module.__code__
1424 pos = constants.index(1000)
1425 constants[pos] = foreign_code
1426 code = code.replace(co_consts=tuple(constants))
1427 with open(self.compiled_name, "wb") as f:
1428 f.write(header)
1429 marshal.dump(code, f)
1430 mod = self.import_module()
1431 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
1432
1433
1434 class PathsTests(unittest.TestCase):
1435 SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
1436 'test\u00b0\u00b3\u00b2')
1437 path = TESTFN
1438
1439 def setUp(self):
1440 os.mkdir(self.path)
1441 self.syspath = sys.path[:]
1442
1443 def tearDown(self):
1444 rmtree(self.path)
1445 sys.path[:] = self.syspath
1446
1447 # Regression test for http://bugs.python.org/issue1293.
1448 def test_trailing_slash(self):
1449 with open(os.path.join(self.path, 'test_trailing_slash.py'),
1450 'w', encoding='utf-8') as f:
1451 f.write("testdata = 'test_trailing_slash'")
1452 sys.path.append(self.path+'/')
1453 mod = __import__("test_trailing_slash")
1454 self.assertEqual(mod.testdata, 'test_trailing_slash')
1455 unload("test_trailing_slash")
1456
1457 # Regression test for http://bugs.python.org/issue3677.
1458 @unittest.skipUnless(sys.platform == 'win32', 'Windows-specific')
1459 def test_UNC_path(self):
1460 with open(os.path.join(self.path, 'test_unc_path.py'), 'w') as f:
1461 f.write("testdata = 'test_unc_path'")
1462 importlib.invalidate_caches()
1463 # Create the UNC path, like \\myhost\c$\foo\bar.
1464 path = os.path.abspath(self.path)
1465 import socket
1466 hn = socket.gethostname()
1467 drive = path[0]
1468 unc = "\\\\%s\\%s$"%(hn, drive)
1469 unc += path[2:]
1470 try:
1471 os.listdir(unc)
1472 except OSError as e:
1473 if e.errno in (errno.EPERM, errno.EACCES, errno.ENOENT):
1474 # See issue #15338
1475 self.skipTest("cannot access administrative share %r" % (unc,))
1476 raise
1477 sys.path.insert(0, unc)
1478 try:
1479 mod = __import__("test_unc_path")
1480 except ImportError as e:
1481 self.fail("could not import 'test_unc_path' from %r: %r"
1482 % (unc, e))
1483 self.assertEqual(mod.testdata, 'test_unc_path')
1484 self.assertStartsWith(mod.__file__, unc)
1485 unload("test_unc_path")
1486
1487
1488 class RelativeImportTests(unittest.TestCase):
1489
1490 def tearDown(self):
1491 unload("test.relimport")
1492 setUp = tearDown
1493
1494 def test_relimport_star(self):
1495 # This will import * from .test_import.
1496 from .. import relimport
1497 self.assertHasAttr(relimport, "RelativeImportTests")
1498
1499 def test_issue3221(self):
1500 # Note for mergers: the 'absolute' tests from the 2.x branch
1501 # are missing in Py3k because implicit relative imports are
1502 # a thing of the past
1503 #
1504 # Regression test for http://bugs.python.org/issue3221.
1505 def check_relative():
1506 exec("from . import relimport", ns)
1507
1508 # Check relative import OK with __package__ and __name__ correct
1509 ns = dict(__package__='test', __name__='test.notarealmodule')
1510 check_relative()
1511
1512 # Check relative import OK with only __name__ wrong
1513 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
1514 check_relative()
1515
1516 # Check relative import fails with only __package__ wrong
1517 ns = dict(__package__='foo', __name__='test.notarealmodule')
1518 self.assertRaises(ModuleNotFoundError, check_relative)
1519
1520 # Check relative import fails with __package__ and __name__ wrong
1521 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
1522 self.assertRaises(ModuleNotFoundError, check_relative)
1523
1524 # Check relative import fails with package set to a non-string
1525 ns = dict(__package__=object())
1526 self.assertRaises(TypeError, check_relative)
1527
1528 def test_parentless_import_shadowed_by_global(self):
1529 # Test as if this were done from the REPL where this error most commonly occurs (bpo-37409).
1530 script_helper.assert_python_failure('-W', 'ignore', '-c',
1531 "foo = 1; from . import foo")
1532
1533 def test_absolute_import_without_future(self):
1534 # If explicit relative import syntax is used, then do not try
1535 # to perform an absolute import in the face of failure.
1536 # Issue #7902.
1537 with self.assertRaises(ImportError):
1538 from .os import sep
1539 self.fail("explicit relative import triggered an "
1540 "implicit absolute import")
1541
1542 def test_import_from_non_package(self):
1543 path = os.path.join(os.path.dirname(__file__), 'data', 'package2')
1544 with uncache('submodule1', 'submodule2'), DirsOnSysPath(path):
1545 with self.assertRaises(ImportError):
1546 import submodule1
1547 self.assertNotIn('submodule1', sys.modules)
1548 self.assertNotIn('submodule2', sys.modules)
1549
1550 def test_import_from_unloaded_package(self):
1551 with uncache('package2', 'package2.submodule1', 'package2.submodule2'), \
1552 DirsOnSysPath(os.path.join(os.path.dirname(__file__), 'data')):
1553 import package2.submodule1
1554 package2.submodule1.submodule2
1555
1556 def test_rebinding(self):
1557 # The same data is also used for testing pkgutil.resolve_name()
1558 # in test_pkgutil and mock.patch in test_unittest.
1559 path = os.path.join(os.path.dirname(__file__), 'data')
1560 with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
1561 from package3 import submodule
1562 self.assertEqual(submodule.attr, 'rebound')
1563 import package3.submodule as submodule
1564 self.assertEqual(submodule.attr, 'rebound')
1565 with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
1566 import package3.submodule as submodule
1567 self.assertEqual(submodule.attr, 'rebound')
1568 from package3 import submodule
1569 self.assertEqual(submodule.attr, 'rebound')
1570
1571 def test_rebinding2(self):
1572 path = os.path.join(os.path.dirname(__file__), 'data')
1573 with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
1574 import package4.submodule as submodule
1575 self.assertEqual(submodule.attr, 'submodule')
1576 from package4 import submodule
1577 self.assertEqual(submodule.attr, 'submodule')
1578 with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
1579 from package4 import submodule
1580 self.assertEqual(submodule.attr, 'origin')
1581 import package4.submodule as submodule
1582 self.assertEqual(submodule.attr, 'submodule')
1583
1584
1585 class OverridingImportBuiltinTests(unittest.TestCase):
1586 def test_override_builtin(self):
1587 # Test that overriding builtins.__import__ can bypass sys.modules.
1588 import os
1589
1590 def foo():
1591 import os
1592 return os
1593 self.assertEqual(foo(), os) # Quick sanity check.
1594
1595 with swap_attr(builtins, "__import__", lambda *x: 5):
1596 self.assertEqual(foo(), 5)
1597
1598 # Test what happens when we shadow __import__ in globals(); this
1599 # currently does not impact the import process, but if this changes,
1600 # other code will need to change, so keep this test as a tripwire.
1601 with swap_item(globals(), "__import__", lambda *x: 5):
1602 self.assertEqual(foo(), os)
1603
1604
1605 class PycacheTests(unittest.TestCase):
1606 # Test the various PEP 3147/488-related behaviors.
1607
1608 def _clean(self):
1609 forget(TESTFN)
1610 rmtree('__pycache__')
1611 unlink(self.source)
1612
1613 def setUp(self):
1614 self.source = TESTFN + '.py'
1615 self._clean()
1616 with open(self.source, 'w', encoding='utf-8') as fp:
1617 print('# This is a test file written by test_import.py', file=fp)
1618 sys.path.insert(0, os.curdir)
1619 importlib.invalidate_caches()
1620
1621 def tearDown(self):
1622 assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]'
1623 del sys.path[0]
1624 self._clean()
1625
1626 @skip_if_dont_write_bytecode
1627 def test_import_pyc_path(self):
1628 self.assertFalse(os.path.exists('__pycache__'))
1629 __import__(TESTFN)
1630 self.assertTrue(os.path.exists('__pycache__'))
1631 pyc_path = importlib.util.cache_from_source(self.source)
1632 self.assertTrue(os.path.exists(pyc_path),
1633 'bytecode file {!r} for {!r} does not '
1634 'exist'.format(pyc_path, TESTFN))
1635
1636 @unittest.skipUnless(os.name == 'posix',
1637 "test meaningful only on posix systems")
1638 @skip_if_dont_write_bytecode
1639 @os_helper.skip_unless_working_chmod
1640 @os_helper.skip_if_dac_override
1641 @unittest.skipIf(is_emscripten, "umask is a stub")
1642 def test_unwritable_directory(self):
1643 # When the umask causes the new __pycache__ directory to be
1644 # unwritable, the import still succeeds but no .pyc file is written.
1645 with temp_umask(0o222):
1646 __import__(TESTFN)
1647 self.assertTrue(os.path.exists('__pycache__'))
1648 pyc_path = importlib.util.cache_from_source(self.source)
1649 self.assertFalse(os.path.exists(pyc_path),
1650 'bytecode file {!r} for {!r} '
1651 'exists'.format(pyc_path, TESTFN))
1652
1653 @skip_if_dont_write_bytecode
1654 def test_missing_source(self):
1655 # With PEP 3147 cache layout, removing the source but leaving the pyc
1656 # file does not satisfy the import.
1657 __import__(TESTFN)
1658 pyc_file = importlib.util.cache_from_source(self.source)
1659 self.assertTrue(os.path.exists(pyc_file))
1660 os.remove(self.source)
1661 forget(TESTFN)
1662 importlib.invalidate_caches()
1663 self.assertRaises(ImportError, __import__, TESTFN)
1664
1665 @skip_if_dont_write_bytecode
1666 def test_missing_source_legacy(self):
1667 # Like test_missing_source() except that for backward compatibility,
1668 # when the pyc file lives where the py file would have been (and named
1669 # without the tag), it is importable. The __file__ of the imported
1670 # module is the pyc location.
1671 __import__(TESTFN)
1672 # pyc_file gets removed in _clean() via tearDown().
1673 pyc_file = make_legacy_pyc(self.source)
1674 os.remove(self.source)
1675 unload(TESTFN)
1676 importlib.invalidate_caches()
1677 m = __import__(TESTFN)
1678 try:
1679 self.assertEqual(m.__file__,
1680 os.path.join(os.getcwd(), os.path.relpath(pyc_file)))
1681 finally:
1682 os.remove(pyc_file)
1683
1684 def test___cached__(self):
1685 # Modules now also have an __cached__ that points to the pyc file.
1686 m = __import__(TESTFN)
1687 pyc_file = importlib.util.cache_from_source(TESTFN + '.py')
1688 self.assertEqual(m.__cached__, os.path.join(os.getcwd(), pyc_file))
1689
1690 @skip_if_dont_write_bytecode
1691 def test___cached___legacy_pyc(self):
1692 # Like test___cached__() except that for backward compatibility,
1693 # when the pyc file lives where the py file would have been (and named
1694 # without the tag), it is importable. The __cached__ of the imported
1695 # module is the pyc location.
1696 __import__(TESTFN)
1697 # pyc_file gets removed in _clean() via tearDown().
1698 pyc_file = make_legacy_pyc(self.source)
1699 os.remove(self.source)
1700 unload(TESTFN)
1701 importlib.invalidate_caches()
1702 m = __import__(TESTFN)
1703 self.assertEqual(m.__cached__,
1704 os.path.join(os.getcwd(), os.path.relpath(pyc_file)))
1705
1706 @skip_if_dont_write_bytecode
1707 def test_package___cached__(self):
1708 # Like test___cached__ but for packages.
1709 def cleanup():
1710 rmtree('pep3147')
1711 unload('pep3147.foo')
1712 unload('pep3147')
1713 os.mkdir('pep3147')
1714 self.addCleanup(cleanup)
1715 # Touch the __init__.py
1716 with open(os.path.join('pep3147', '__init__.py'), 'wb'):
1717 pass
1718 with open(os.path.join('pep3147', 'foo.py'), 'wb'):
1719 pass
1720 importlib.invalidate_caches()
1721 m = __import__('pep3147.foo')
1722 init_pyc = importlib.util.cache_from_source(
1723 os.path.join('pep3147', '__init__.py'))
1724 self.assertEqual(m.__cached__, os.path.join(os.getcwd(), init_pyc))
1725 foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py'))
1726 self.assertEqual(sys.modules['pep3147.foo'].__cached__,
1727 os.path.join(os.getcwd(), foo_pyc))
1728
1729 def test_package___cached___from_pyc(self):
1730 # Like test___cached__ but ensuring __cached__ when imported from a
1731 # PEP 3147 pyc file.
1732 def cleanup():
1733 rmtree('pep3147')
1734 unload('pep3147.foo')
1735 unload('pep3147')
1736 os.mkdir('pep3147')
1737 self.addCleanup(cleanup)
1738 # Touch the __init__.py
1739 with open(os.path.join('pep3147', '__init__.py'), 'wb'):
1740 pass
1741 with open(os.path.join('pep3147', 'foo.py'), 'wb'):
1742 pass
1743 importlib.invalidate_caches()
1744 m = __import__('pep3147.foo')
1745 unload('pep3147.foo')
1746 unload('pep3147')
1747 importlib.invalidate_caches()
1748 m = __import__('pep3147.foo')
1749 init_pyc = importlib.util.cache_from_source(
1750 os.path.join('pep3147', '__init__.py'))
1751 self.assertEqual(m.__cached__, os.path.join(os.getcwd(), init_pyc))
1752 foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py'))
1753 self.assertEqual(sys.modules['pep3147.foo'].__cached__,
1754 os.path.join(os.getcwd(), foo_pyc))
1755
1756 def test_recompute_pyc_same_second(self):
1757 # Even when the source file doesn't change timestamp, a change in
1758 # source size is enough to trigger recomputation of the pyc file.
1759 __import__(TESTFN)
1760 unload(TESTFN)
1761 with open(self.source, 'a', encoding='utf-8') as fp:
1762 print("x = 5", file=fp)
1763 m = __import__(TESTFN)
1764 self.assertEqual(m.x, 5)
1765
1766
1767 class TestSymbolicallyLinkedPackage(unittest.TestCase):
1768 package_name = 'sample'
1769 tagged = package_name + '-tagged'
1770
1771 def setUp(self):
1772 os_helper.rmtree(self.tagged)
1773 os_helper.rmtree(self.package_name)
1774 self.orig_sys_path = sys.path[:]
1775
1776 # create a sample package; imagine you have a package with a tag and
1777 # you want to symbolically link it from its untagged name.
1778 os.mkdir(self.tagged)
1779 self.addCleanup(os_helper.rmtree, self.tagged)
1780 init_file = os.path.join(self.tagged, '__init__.py')
1781 os_helper.create_empty_file(init_file)
1782 assert os.path.exists(init_file)
1783
1784 # now create a symlink to the tagged package
1785 # sample -> sample-tagged
1786 os.symlink(self.tagged, self.package_name, target_is_directory=True)
1787 self.addCleanup(os_helper.unlink, self.package_name)
1788 importlib.invalidate_caches()
1789
1790 self.assertEqual(os.path.isdir(self.package_name), True)
1791
1792 assert os.path.isfile(os.path.join(self.package_name, '__init__.py'))
1793
1794 def tearDown(self):
1795 sys.path[:] = self.orig_sys_path
1796
1797 # regression test for issue6727
1798 @unittest.skipUnless(
1799 not hasattr(sys, 'getwindowsversion')
1800 or sys.getwindowsversion() >= (6, 0),
1801 "Windows Vista or later required")
1802 @os_helper.skip_unless_symlink
1803 def test_symlinked_dir_importable(self):
1804 # make sure sample can only be imported from the current directory.
1805 sys.path[:] = ['.']
1806 assert os.path.exists(self.package_name)
1807 assert os.path.exists(os.path.join(self.package_name, '__init__.py'))
1808
1809 # Try to import the package
1810 importlib.import_module(self.package_name)
1811
1812
1813 @cpython_only
1814 class ImportlibBootstrapTests(unittest.TestCase):
1815 # These tests check that importlib is bootstrapped.
1816
1817 def test_frozen_importlib(self):
1818 mod = sys.modules['_frozen_importlib']
1819 self.assertTrue(mod)
1820
1821 def test_frozen_importlib_is_bootstrap(self):
1822 from importlib import _bootstrap
1823 mod = sys.modules['_frozen_importlib']
1824 self.assertIs(mod, _bootstrap)
1825 self.assertEqual(mod.__name__, 'importlib._bootstrap')
1826 self.assertEqual(mod.__package__, 'importlib')
1827 self.assertEndsWith(mod.__file__, '_bootstrap.py')
1828
1829 def test_frozen_importlib_external_is_bootstrap_external(self):
1830 from importlib import _bootstrap_external
1831 mod = sys.modules['_frozen_importlib_external']
1832 self.assertIs(mod, _bootstrap_external)
1833 self.assertEqual(mod.__name__, 'importlib._bootstrap_external')
1834 self.assertEqual(mod.__package__, 'importlib')
1835 self.assertEndsWith(mod.__file__, '_bootstrap_external.py')
1836
1837 def test_there_can_be_only_one(self):
1838 # Issue #15386 revealed a tricky loophole in the bootstrapping
1839 # This test is technically redundant, since the bug caused importing
1840 # this test module to crash completely, but it helps prove the point
1841 from importlib import machinery
1842 mod = sys.modules['_frozen_importlib']
1843 self.assertIs(machinery.ModuleSpec, mod.ModuleSpec)
1844
1845
1846 @cpython_only
1847 class GetSourcefileTests(unittest.TestCase):
1848
1849 """Test importlib._bootstrap_external._get_sourcefile() as used by the C API.
1850
1851 Because of the peculiarities of the need of this function, the tests are
1852 knowingly whitebox tests.
1853
1854 """
1855
1856 def test_get_sourcefile(self):
1857 # Given a valid bytecode path, return the path to the corresponding
1858 # source file if it exists.
1859 with mock.patch('importlib._bootstrap_external._path_isfile') as _path_isfile:
1860 _path_isfile.return_value = True
1861 path = TESTFN + '.pyc'
1862 expect = TESTFN + '.py'
1863 self.assertEqual(_get_sourcefile(path), expect)
1864
1865 def test_get_sourcefile_no_source(self):
1866 # Given a valid bytecode path without a corresponding source path,
1867 # return the original bytecode path.
1868 with mock.patch('importlib._bootstrap_external._path_isfile') as _path_isfile:
1869 _path_isfile.return_value = False
1870 path = TESTFN + '.pyc'
1871 self.assertEqual(_get_sourcefile(path), path)
1872
1873 def test_get_sourcefile_bad_ext(self):
1874 # Given a path with an invalid bytecode extension, return the
1875 # bytecode path passed as the argument.
1876 path = TESTFN + '.bad_ext'
1877 self.assertEqual(_get_sourcefile(path), path)
1878
1879
1880 class ImportTracebackTests(unittest.TestCase):
1881
1882 def setUp(self):
1883 os.mkdir(TESTFN)
1884 self.old_path = sys.path[:]
1885 sys.path.insert(0, TESTFN)
1886
1887 def tearDown(self):
1888 sys.path[:] = self.old_path
1889 rmtree(TESTFN)
1890
1891 def create_module(self, mod, contents, ext=".py"):
1892 fname = os.path.join(TESTFN, mod + ext)
1893 with open(fname, "w", encoding='utf-8') as f:
1894 f.write(contents)
1895 self.addCleanup(unload, mod)
1896 importlib.invalidate_caches()
1897 return fname
1898
1899 def assert_traceback(self, tb, files):
1900 deduped_files = []
1901 while tb:
1902 code = tb.tb_frame.f_code
1903 fn = code.co_filename
1904 if not deduped_files or fn != deduped_files[-1]:
1905 deduped_files.append(fn)
1906 tb = tb.tb_next
1907 self.assertEqual(len(deduped_files), len(files), deduped_files)
1908 for fn, pat in zip(deduped_files, files):
1909 self.assertIn(pat, fn)
1910
1911 def test_nonexistent_module(self):
1912 try:
1913 # assertRaises() clears __traceback__
1914 import nonexistent_xyzzy
1915 except ImportError as e:
1916 tb = e.__traceback__
1917 else:
1918 self.fail("ImportError should have been raised")
1919 self.assert_traceback(tb, [__file__])
1920
1921 def test_nonexistent_module_nested(self):
1922 self.create_module("foo", "import nonexistent_xyzzy")
1923 try:
1924 import foo
1925 except ImportError as e:
1926 tb = e.__traceback__
1927 else:
1928 self.fail("ImportError should have been raised")
1929 self.assert_traceback(tb, [__file__, 'foo.py'])
1930
1931 def test_exec_failure(self):
1932 self.create_module("foo", "1/0")
1933 try:
1934 import foo
1935 except ZeroDivisionError as e:
1936 tb = e.__traceback__
1937 else:
1938 self.fail("ZeroDivisionError should have been raised")
1939 self.assert_traceback(tb, [__file__, 'foo.py'])
1940
1941 def test_exec_failure_nested(self):
1942 self.create_module("foo", "import bar")
1943 self.create_module("bar", "1/0")
1944 try:
1945 import foo
1946 except ZeroDivisionError as e:
1947 tb = e.__traceback__
1948 else:
1949 self.fail("ZeroDivisionError should have been raised")
1950 self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py'])
1951
1952 # A few more examples from issue #15425
1953 def test_syntax_error(self):
1954 self.create_module("foo", "invalid syntax is invalid")
1955 try:
1956 import foo
1957 except SyntaxError as e:
1958 tb = e.__traceback__
1959 else:
1960 self.fail("SyntaxError should have been raised")
1961 self.assert_traceback(tb, [__file__])
1962
1963 def _setup_broken_package(self, parent, child):
1964 pkg_name = "_parent_foo"
1965 self.addCleanup(unload, pkg_name)
1966 pkg_path = os.path.join(TESTFN, pkg_name)
1967 os.mkdir(pkg_path)
1968 # Touch the __init__.py
1969 init_path = os.path.join(pkg_path, '__init__.py')
1970 with open(init_path, 'w', encoding='utf-8') as f:
1971 f.write(parent)
1972 bar_path = os.path.join(pkg_path, 'bar.py')
1973 with open(bar_path, 'w', encoding='utf-8') as f:
1974 f.write(child)
1975 importlib.invalidate_caches()
1976 return init_path, bar_path
1977
1978 def test_broken_submodule(self):
1979 init_path, bar_path = self._setup_broken_package("", "1/0")
1980 try:
1981 import _parent_foo.bar
1982 except ZeroDivisionError as e:
1983 tb = e.__traceback__
1984 else:
1985 self.fail("ZeroDivisionError should have been raised")
1986 self.assert_traceback(tb, [__file__, bar_path])
1987
1988 def test_broken_from(self):
1989 init_path, bar_path = self._setup_broken_package("", "1/0")
1990 try:
1991 from _parent_foo import bar
1992 except ZeroDivisionError as e:
1993 tb = e.__traceback__
1994 else:
1995 self.fail("ImportError should have been raised")
1996 self.assert_traceback(tb, [__file__, bar_path])
1997
1998 def test_broken_parent(self):
1999 init_path, bar_path = self._setup_broken_package("1/0", "")
2000 try:
2001 import _parent_foo.bar
2002 except ZeroDivisionError as e:
2003 tb = e.__traceback__
2004 else:
2005 self.fail("ZeroDivisionError should have been raised")
2006 self.assert_traceback(tb, [__file__, init_path])
2007
2008 def test_broken_parent_from(self):
2009 init_path, bar_path = self._setup_broken_package("1/0", "")
2010 try:
2011 from _parent_foo import bar
2012 except ZeroDivisionError as e:
2013 tb = e.__traceback__
2014 else:
2015 self.fail("ZeroDivisionError should have been raised")
2016 self.assert_traceback(tb, [__file__, init_path])
2017
2018 @cpython_only
2019 def test_import_bug(self):
2020 # We simulate a bug in importlib and check that it's not stripped
2021 # away from the traceback.
2022 self.create_module("foo", "")
2023 importlib = sys.modules['_frozen_importlib_external']
2024 if 'load_module' in vars(importlib.SourceLoader):
2025 old_exec_module = importlib.SourceLoader.exec_module
2026 else:
2027 old_exec_module = None
2028 try:
2029 def exec_module(*args):
2030 1/0
2031 importlib.SourceLoader.exec_module = exec_module
2032 try:
2033 import foo
2034 except ZeroDivisionError as e:
2035 tb = e.__traceback__
2036 else:
2037 self.fail("ZeroDivisionError should have been raised")
2038 self.assert_traceback(tb, [__file__, '<frozen importlib', __file__])
2039 finally:
2040 if old_exec_module is None:
2041 del importlib.SourceLoader.exec_module
2042 else:
2043 importlib.SourceLoader.exec_module = old_exec_module
2044
2045 @unittest.skipUnless(TESTFN_UNENCODABLE, 'need TESTFN_UNENCODABLE')
2046 def test_unencodable_filename(self):
2047 # Issue #11619: The Python parser and the import machinery must not
2048 # encode filenames, especially on Windows
2049 pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
2050 self.addCleanup(unlink, pyname)
2051 name = pyname[:-3]
2052 script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name,
2053 __isolated=False)
2054
2055
2056 class CircularImportTests(unittest.TestCase):
2057
2058 """See the docstrings of the modules being imported for the purpose of the
2059 test."""
2060
2061 def tearDown(self):
2062 """Make sure no modules pre-exist in sys.modules which are being used to
2063 test."""
2064 for key in list(sys.modules.keys()):
2065 if key.startswith('test.test_import.data.circular_imports'):
2066 del sys.modules[key]
2067
2068 def test_direct(self):
2069 try:
2070 import test.test_import.data.circular_imports.basic
2071 except ImportError:
2072 self.fail('circular import through relative imports failed')
2073
2074 def test_indirect(self):
2075 try:
2076 import test.test_import.data.circular_imports.indirect
2077 except ImportError:
2078 self.fail('relative import in module contributing to circular '
2079 'import failed')
2080
2081 def test_subpackage(self):
2082 try:
2083 import test.test_import.data.circular_imports.subpackage
2084 except ImportError:
2085 self.fail('circular import involving a subpackage failed')
2086
2087 def test_rebinding(self):
2088 try:
2089 import test.test_import.data.circular_imports.rebinding as rebinding
2090 except ImportError:
2091 self.fail('circular import with rebinding of module attribute failed')
2092 from test.test_import.data.circular_imports.subpkg import util
2093 self.assertIs(util.util, rebinding.util)
2094
2095 def test_binding(self):
2096 try:
2097 import test.test_import.data.circular_imports.binding
2098 except ImportError:
2099 self.fail('circular import with binding a submodule to a name failed')
2100
2101 def test_crossreference1(self):
2102 import test.test_import.data.circular_imports.use
2103 import test.test_import.data.circular_imports.source
2104
2105 def test_crossreference2(self):
2106 with self.assertRaises(AttributeError) as cm:
2107 import test.test_import.data.circular_imports.source
2108 errmsg = str(cm.exception)
2109 self.assertIn('test.test_import.data.circular_imports.source', errmsg)
2110 self.assertIn('spam', errmsg)
2111 self.assertIn('partially initialized module', errmsg)
2112 self.assertIn('circular import', errmsg)
2113
2114 def test_circular_from_import(self):
2115 with self.assertRaises(ImportError) as cm:
2116 import test.test_import.data.circular_imports.from_cycle1
2117 self.assertIn(
2118 "cannot import name 'b' from partially initialized module "
2119 "'test.test_import.data.circular_imports.from_cycle1' "
2120 "(most likely due to a circular import)",
2121 str(cm.exception),
2122 )
2123
2124 def test_circular_import(self):
2125 with self.assertRaisesRegex(
2126 AttributeError,
2127 r"partially initialized module 'test.test_import.data.circular_imports.import_cycle' "
2128 r"from '.*' has no attribute 'some_attribute' \(most likely due to a circular import\)"
2129 ):
2130 import test.test_import.data.circular_imports.import_cycle
2131
2132 def test_absolute_circular_submodule(self):
2133 with self.assertRaises(AttributeError) as cm:
2134 import test.test_import.data.circular_imports.subpkg2.parent
2135 self.assertIn(
2136 "cannot access submodule 'parent' of module "
2137 "'test.test_import.data.circular_imports.subpkg2' "
2138 "(most likely due to a circular import)",
2139 str(cm.exception),
2140 )
2141
2142 @requires_singlephase_init
2143 @unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
2144 def test_singlephase_circular(self):
2145 """Regression test for gh-123950
2146
2147 Import a single-phase-init module that imports itself
2148 from the PyInit_* function (before it's added to sys.modules).
2149 Manages its own cache (which is `static`, and so incompatible
2150 with multiple interpreters or interpreter reset).
2151 """
2152 name = '_testsinglephase_circular'
2153 helper_name = 'test.test_import.data.circular_imports.singlephase'
2154 with uncache(name, helper_name):
2155 filename = _testsinglephase.__file__
2156 # We don't put the module in sys.modules: that the *inner*
2157 # import should do that.
2158 mod = import_extension_from_file(name, filename,
2159 put_in_sys_modules=False)
2160
2161 self.assertEqual(mod.helper_mod_name, helper_name)
2162 self.assertIn(name, sys.modules)
2163 self.assertIn(helper_name, sys.modules)
2164
2165 self.assertIn(name, sys.modules)
2166 self.assertIn(helper_name, sys.modules)
2167 self.assertNotIn(name, sys.modules)
2168 self.assertNotIn(helper_name, sys.modules)
2169 self.assertIs(mod.clear_static_var(), mod)
2170 _testinternalcapi.clear_extension('_testsinglephase_circular',
2171 mod.__spec__.origin)
2172
2173 def test_unwritable_module(self):
2174 self.addCleanup(unload, "test.test_import.data.unwritable")
2175 self.addCleanup(unload, "test.test_import.data.unwritable.x")
2176
2177 import test.test_import.data.unwritable as unwritable
2178 with self.assertWarns(ImportWarning):
2179 from test.test_import.data.unwritable import x
2180
2181 self.assertNotEqual(type(unwritable), ModuleType)
2182 self.assertEqual(type(x), ModuleType)
2183 with self.assertRaises(AttributeError):
2184 unwritable.x = 42
2185
2186
2187 class SubinterpImportTests(unittest.TestCase):
2188
2189 RUN_KWARGS = dict(
2190 allow_fork=False,
2191 allow_exec=False,
2192 allow_threads=True,
2193 allow_daemon_threads=False,
2194 # Isolation-related config values aren't included here.
2195 )
2196 ISOLATED = dict(
2197 use_main_obmalloc=False,
2198 gil=2,
2199 )
2200 NOT_ISOLATED = {k: not v for k, v in ISOLATED.items()}
2201 NOT_ISOLATED['gil'] = 1
2202
2203 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
2204 def pipe(self):
2205 r, w = os.pipe()
2206 self.addCleanup(os.close, r)
2207 self.addCleanup(os.close, w)
2208 if hasattr(os, 'set_blocking'):
2209 os.set_blocking(r, False)
2210 return (r, w)
2211
2212 def import_script(self, name, fd, filename=None, check_override=None):
2213 override_text = ''
2214 if check_override is not None:
2215 override_text = f'''
2216 import _imp
2217 _imp._override_multi_interp_extensions_check({check_override})
2218 '''
2219 if filename:
2220 # Apple extensions must be distributed as frameworks. This requires
2221 # a specialist loader.
2222 if is_apple_mobile:
2223 loader = "AppleFrameworkLoader"
2224 else:
2225 loader = "ExtensionFileLoader"
2226
2227 return textwrap.dedent(f'''
2228 from importlib.util import spec_from_loader, module_from_spec
2229 from importlib.machinery import {loader}
2230 import os, sys
2231 {override_text}
2232 loader = {loader}({name!r}, {filename!r})
2233 spec = spec_from_loader({name!r}, loader)
2234 try:
2235 module = module_from_spec(spec)
2236 loader.exec_module(module)
2237 except ImportError as exc:
2238 text = 'ImportError: ' + str(exc)
2239 else:
2240 text = 'okay'
2241 os.write({fd}, text.encode('utf-8'))
2242 ''')
2243 else:
2244 return textwrap.dedent(f'''
2245 import os, sys
2246 {override_text}
2247 try:
2248 import {name}
2249 except ImportError as exc:
2250 text = 'ImportError: ' + str(exc)
2251 else:
2252 text = 'okay'
2253 os.write({fd}, text.encode('utf-8'))
2254 ''')
2255
2256 def run_here(self, name, filename=None, *,
2257 check_singlephase_setting=False,
2258 check_singlephase_override=None,
2259 isolated=False,
2260 ):
2261 """
2262 Try importing the named module in a subinterpreter.
2263
2264 The subinterpreter will be in the current process.
2265 The module will have already been imported in the main interpreter.
2266 Thus, for extension/builtin modules, the module definition will
2267 have been loaded already and cached globally.
2268
2269 "check_singlephase_setting" determines whether or not
2270 the interpreter will be configured to check for modules
2271 that are not compatible with use in multiple interpreters.
2272
2273 This should always return "okay" for all modules if the
2274 setting is False (with no override).
2275 """
2276 __import__(name)
2277
2278 kwargs = dict(
2279 **self.RUN_KWARGS,
2280 **(self.ISOLATED if isolated else self.NOT_ISOLATED),
2281 check_multi_interp_extensions=check_singlephase_setting,
2282 )
2283
2284 r, w = self.pipe()
2285 script = self.import_script(name, w, filename,
2286 check_singlephase_override)
2287
2288 ret = run_in_subinterp_with_config(script, **kwargs)
2289 self.assertEqual(ret, 0)
2290 return os.read(r, 100)
2291
2292 def check_compatible_here(self, name, filename=None, *,
2293 strict=False,
2294 isolated=False,
2295 ):
2296 # Verify that the named module may be imported in a subinterpreter.
2297 # (See run_here() for more info.)
2298 out = self.run_here(name, filename,
2299 check_singlephase_setting=strict,
2300 isolated=isolated,
2301 )
2302 self.assertEqual(out, b'okay')
2303
2304 def check_incompatible_here(self, name, filename=None, *, isolated=False):
2305 # Differences from check_compatible_here():
2306 # * verify that import fails
2307 # * "strict" is always True
2308 out = self.run_here(name, filename,
2309 check_singlephase_setting=True,
2310 isolated=isolated,
2311 )
2312 self.assertEqual(
2313 out.decode('utf-8'),
2314 f'ImportError: module {name} does not support loading in subinterpreters',
2315 )
2316
2317 def check_compatible_fresh(self, name, *, strict=False, isolated=False):
2318 # Differences from check_compatible_here():
2319 # * subinterpreter in a new process
2320 # * module has never been imported before in that process
2321 # * this tests importing the module for the first time
2322 kwargs = dict(
2323 **self.RUN_KWARGS,
2324 **(self.ISOLATED if isolated else self.NOT_ISOLATED),
2325 check_multi_interp_extensions=strict,
2326 )
2327 gil = kwargs['gil']
2328 kwargs['gil'] = 'default' if gil == 0 else (
2329 'shared' if gil == 1 else 'own' if gil == 2 else gil)
2330 _, out, err = script_helper.assert_python_ok('-c', textwrap.dedent(f'''
2331 import _testinternalcapi, sys
2332 assert (
2333 {name!r} in sys.builtin_module_names or
2334 {name!r} not in sys.modules
2335 ), repr({name!r})
2336 config = type(sys.implementation)(**{kwargs})
2337 ret = _testinternalcapi.run_in_subinterp_with_config(
2338 {self.import_script(name, "sys.stdout.fileno()")!r},
2339 config,
2340 )
2341 assert ret == 0, ret
2342 '''))
2343 self.assertEqual(err, b'')
2344 self.assertEqual(out, b'okay')
2345
2346 def check_incompatible_fresh(self, name, *, isolated=False):
2347 # Differences from check_compatible_fresh():
2348 # * verify that import fails
2349 # * "strict" is always True
2350 kwargs = dict(
2351 **self.RUN_KWARGS,
2352 **(self.ISOLATED if isolated else self.NOT_ISOLATED),
2353 check_multi_interp_extensions=True,
2354 )
2355 gil = kwargs['gil']
2356 kwargs['gil'] = 'default' if gil == 0 else (
2357 'shared' if gil == 1 else 'own' if gil == 2 else gil)
2358 _, out, err = script_helper.assert_python_ok('-c', textwrap.dedent(f'''
2359 import _testinternalcapi, sys
2360 assert {name!r} not in sys.modules, {name!r}
2361 config = type(sys.implementation)(**{kwargs})
2362 ret = _testinternalcapi.run_in_subinterp_with_config(
2363 {self.import_script(name, "sys.stdout.fileno()")!r},
2364 config,
2365 )
2366 assert ret == 0, ret
2367 '''))
2368 self.assertEqual(err, b'')
2369 self.assertEqual(
2370 out.decode('utf-8'),
2371 f'ImportError: module {name} does not support loading in subinterpreters',
2372 )
2373
2374 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
2375 def test_builtin_compat(self):
2376 # For now we avoid using sys or builtins
2377 # since they still don't implement multi-phase init.
2378 module = '_imp'
2379 require_builtin(module)
2380 if not Py_GIL_DISABLED:
2381 with self.subTest(f'{module}: not strict'):
2382 self.check_compatible_here(module, strict=False)
2383 with self.subTest(f'{module}: strict, not fresh'):
2384 self.check_compatible_here(module, strict=True)
2385
2386 @cpython_only
2387 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
2388 def test_frozen_compat(self):
2389 module = '_frozen_importlib'
2390 require_frozen(module, skip=True)
2391 if __import__(module).__spec__.origin != 'frozen':
2392 raise unittest.SkipTest(f'{module} is unexpectedly not frozen')
2393 if not Py_GIL_DISABLED:
2394 with self.subTest(f'{module}: not strict'):
2395 self.check_compatible_here(module, strict=False)
2396 with self.subTest(f'{module}: strict, not fresh'):
2397 self.check_compatible_here(module, strict=True)
2398
2399 @requires_singlephase_init
2400 def test_single_init_extension_compat(self):
2401 module = '_testsinglephase'
2402 require_extension(module)
2403 with self.subTest(f'{module}: not strict'):
2404 self.check_compatible_here(module, strict=False)
2405 with self.subTest(f'{module}: strict, not fresh'):
2406 self.check_incompatible_here(module)
2407 with self.subTest(f'{module}: strict, fresh'):
2408 self.check_incompatible_fresh(module)
2409 with self.subTest(f'{module}: isolated, fresh'):
2410 self.check_incompatible_fresh(module, isolated=True)
2411
2412 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
2413 def test_multi_init_extension_compat(self):
2414 # Module with Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
2415 module = '_testmultiphase'
2416 require_extension(module)
2417
2418 if not Py_GIL_DISABLED:
2419 with self.subTest(f'{module}: not strict'):
2420 self.check_compatible_here(module, strict=False)
2421 with self.subTest(f'{module}: strict, not fresh'):
2422 self.check_compatible_here(module, strict=True)
2423 with self.subTest(f'{module}: strict, fresh'):
2424 self.check_compatible_fresh(module, strict=True)
2425
2426 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
2427 def test_multi_init_extension_non_isolated_compat(self):
2428 # Module with Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED
2429 # and Py_MOD_GIL_NOT_USED
2430 modname = '_test_non_isolated'
2431 filename = _testmultiphase.__file__
2432 module = import_extension_from_file(modname, filename)
2433
2434 require_extension(module)
2435 with self.subTest(f'{modname}: isolated'):
2436 self.check_incompatible_here(modname, filename, isolated=True)
2437 with self.subTest(f'{modname}: not isolated'):
2438 self.check_incompatible_here(modname, filename, isolated=False)
2439 if not Py_GIL_DISABLED:
2440 with self.subTest(f'{modname}: not strict'):
2441 self.check_compatible_here(modname, filename, strict=False)
2442
2443 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
2444 def test_multi_init_extension_per_interpreter_gil_compat(self):
2445
2446 # _test_shared_gil_only:
2447 # Explicit Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED (default)
2448 # and Py_MOD_GIL_NOT_USED
2449 # _test_no_multiple_interpreter_slot:
2450 # No Py_mod_multiple_interpreters slot
2451 # and Py_MOD_GIL_NOT_USED
2452 for modname in ('_test_shared_gil_only',
2453 '_test_no_multiple_interpreter_slot'):
2454 with self.subTest(modname=modname):
2455
2456 filename = _testmultiphase.__file__
2457 module = import_extension_from_file(modname, filename)
2458
2459 require_extension(module)
2460 with self.subTest(f'{modname}: isolated, strict'):
2461 self.check_incompatible_here(modname, filename,
2462 isolated=True)
2463 with self.subTest(f'{modname}: not isolated, strict'):
2464 self.check_compatible_here(modname, filename,
2465 strict=True, isolated=False)
2466 if not Py_GIL_DISABLED:
2467 with self.subTest(f'{modname}: not isolated, not strict'):
2468 self.check_compatible_here(
2469 modname, filename, strict=False, isolated=False)
2470
2471 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
2472 def test_python_compat(self):
2473 module = 'threading'
2474 require_pure_python(module)
2475 if not Py_GIL_DISABLED:
2476 with self.subTest(f'{module}: not strict'):
2477 self.check_compatible_here(module, strict=False)
2478 with self.subTest(f'{module}: strict, not fresh'):
2479 self.check_compatible_here(module, strict=True)
2480 with self.subTest(f'{module}: strict, fresh'):
2481 self.check_compatible_fresh(module, strict=True)
2482
2483 @requires_singlephase_init
2484 def test_singlephase_check_with_setting_and_override(self):
2485 module = '_testsinglephase'
2486 require_extension(module)
2487
2488 def check_compatible(setting, override):
2489 out = self.run_here(
2490 module,
2491 check_singlephase_setting=setting,
2492 check_singlephase_override=override,
2493 )
2494 self.assertEqual(out, b'okay')
2495
2496 def check_incompatible(setting, override):
2497 out = self.run_here(
2498 module,
2499 check_singlephase_setting=setting,
2500 check_singlephase_override=override,
2501 )
2502 self.assertNotEqual(out, b'okay')
2503
2504 with self.subTest('config: check enabled; override: enabled'):
2505 check_incompatible(True, 1)
2506 with self.subTest('config: check enabled; override: use config'):
2507 check_incompatible(True, 0)
2508 with self.subTest('config: check enabled; override: disabled'):
2509 check_compatible(True, -1)
2510
2511 with self.subTest('config: check disabled; override: enabled'):
2512 check_incompatible(False, 1)
2513 with self.subTest('config: check disabled; override: use config'):
2514 check_compatible(False, 0)
2515 with self.subTest('config: check disabled; override: disabled'):
2516 check_compatible(False, -1)
2517
2518 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
2519 def test_isolated_config(self):
2520 module = 'threading'
2521 require_pure_python(module)
2522 with self.subTest(f'{module}: strict, not fresh'):
2523 self.check_compatible_here(module, strict=True, isolated=True)
2524 with self.subTest(f'{module}: strict, fresh'):
2525 self.check_compatible_fresh(module, strict=True, isolated=True)
2526
2527 @requires_subinterpreters
2528 @requires_singlephase_init
2529 def test_disallowed_reimport(self):
2530 # See https://github.com/python/cpython/issues/104621.
2531 script = textwrap.dedent('''
2532 import _testsinglephase
2533 print(_testsinglephase)
2534 ''')
2535 interpid = _interpreters.create()
2536 self.addCleanup(lambda: _interpreters.destroy(interpid))
2537
2538 excsnap = _interpreters.run_string(interpid, script)
2539 self.assertIsNot(excsnap, None)
2540
2541 excsnap = _interpreters.run_string(interpid, script)
2542 self.assertIsNot(excsnap, None)
2543
2544 @requires_subinterpreters
2545 def test_pyinit_function_raises_exception(self):
2546 # gh-144601: PyInit functions that raised exceptions would cause a
2547 # crash when imported from a subinterpreter.
2548 import _testsinglephase
2549 filename = _testsinglephase.__file__
2550 script = f"""if True:
2551 from test.test_import import import_extension_from_file
2552
2553 import_extension_from_file('_testsinglephase_raise_exception', {filename!r})"""
2554
2555 interp = _interpreters.create()
2556 try:
2557 with catch_unraisable_exception() as cm:
2558 exception = _interpreters.run_string(interp, script)
2559 unraisable = cm.unraisable
2560 finally:
2561 _interpreters.destroy(interp)
2562
2563 self.assertIsNotNone(exception)
2564 self.assertIsNotNone(exception.type.__name__, "ImportError")
2565 self.assertIsNotNone(exception.msg, "failed to import from subinterpreter due to exception")
2566 self.assertIsNotNone(unraisable)
2567 self.assertIs(unraisable.exc_type, RuntimeError)
2568 self.assertEqual(str(unraisable.exc_value), "evil")
2569
2570
2571 class TestSinglePhaseSnapshot(ModuleSnapshot):
2572 """A representation of a single-phase init module for testing.
2573
2574 Fields from ModuleSnapshot:
2575
2576 * id - id(mod)
2577 * module - mod or a SimpleNamespace with __file__ & __spec__
2578 * ns - a shallow copy of mod.__dict__
2579 * ns_id - id(mod.__dict__)
2580 * cached - sys.modules[name] (or None if not there or not snapshotable)
2581 * cached_id - id(sys.modules[name]) (or None if not there)
2582
2583 Extra fields:
2584
2585 * summed - the result of calling "mod.sum(1, 2)"
2586 * lookedup - the result of calling "mod.look_up_self()"
2587 * lookedup_id - the object ID of self.lookedup
2588 * state_initialized - the result of calling "mod.state_initialized()"
2589 * init_count - (optional) the result of calling "mod.initialized_count()"
2590
2591 Overridden methods from ModuleSnapshot:
2592
2593 * from_module()
2594 * parse()
2595
2596 Other methods from ModuleSnapshot:
2597
2598 * build_script()
2599 * from_subinterp()
2600
2601 ----
2602
2603 There are 5 modules in Modules/_testsinglephase.c:
2604
2605 * _testsinglephase
2606 * has global state
2607 * extra loads skip the init function, copy def.m_base.m_copy
2608 * counts calls to init function
2609 * _testsinglephase_basic_wrapper
2610 * _testsinglephase by another name (and separate init function symbol)
2611 * _testsinglephase_basic_copy
2612 * same as _testsinglephase but with own def (and init func)
2613 * _testsinglephase_with_reinit
2614 * has no global or module state
2615 * mod.state_initialized returns None
2616 * an extra load in the main interpreter calls the cached init func
2617 * an extra load in legacy subinterpreters does a full load
2618 * _testsinglephase_with_state
2619 * has module state
2620 * an extra load in the main interpreter calls the cached init func
2621 * an extra load in legacy subinterpreters does a full load
2622
2623 (See Modules/_testsinglephase.c for more info.)
2624
2625 For all those modules, the snapshot after the initial load (not in
2626 the global extensions cache) would look like the following:
2627
2628 * initial load
2629 * id: ID of nww module object
2630 * ns: exactly what the module init put there
2631 * ns_id: ID of new module's __dict__
2632 * cached_id: same as self.id
2633 * summed: 3 (never changes)
2634 * lookedup_id: same as self.id
2635 * state_initialized: a timestamp between the time of the load
2636 and the time of the snapshot
2637 * init_count: 1 (None for _testsinglephase_with_reinit)
2638
2639 For the other scenarios it varies.
2640
2641 For the _testsinglephase, _testsinglephase_basic_wrapper, and
2642 _testsinglephase_basic_copy modules, the snapshot should look
2643 like the following:
2644
2645 * reloaded
2646 * id: no change
2647 * ns: matches what the module init function put there,
2648 including the IDs of all contained objects,
2649 plus any extra attributes added before the reload
2650 * ns_id: no change
2651 * cached_id: no change
2652 * lookedup_id: no change
2653 * state_initialized: no change
2654 * init_count: no change
2655 * already loaded
2656 * (same as initial load except for ns and state_initialized)
2657 * ns: matches the initial load, incl. IDs of contained objects
2658 * state_initialized: no change from initial load
2659
2660 For _testsinglephase_with_reinit:
2661
2662 * reloaded: same as initial load (old module & ns is discarded)
2663 * already loaded: same as initial load (old module & ns is discarded)
2664
2665 For _testsinglephase_with_state:
2666
2667 * reloaded
2668 * (same as initial load (old module & ns is discarded),
2669 except init_count)
2670 * init_count: increase by 1
2671 * already loaded: same as reloaded
2672 """
2673
2674 @classmethod
2675 def from_module(cls, mod):
2676 self = super().from_module(mod)
2677 self.summed = mod.sum(1, 2)
2678 self.lookedup = mod.look_up_self()
2679 self.lookedup_id = id(self.lookedup)
2680 self.state_initialized = mod.state_initialized()
2681 if hasattr(mod, 'initialized_count'):
2682 self.init_count = mod.initialized_count()
2683 return self
2684
2685 SCRIPT_BODY = ModuleSnapshot.SCRIPT_BODY + textwrap.dedent('''
2686 snapshot['module'].update(dict(
2687 int_const=mod.int_const,
2688 str_const=mod.str_const,
2689 _module_initialized=mod._module_initialized,
2690 ))
2691 snapshot.update(dict(
2692 summed=mod.sum(1, 2),
2693 lookedup_id=id(mod.look_up_self()),
2694 state_initialized=mod.state_initialized(),
2695 init_count=mod.initialized_count(),
2696 has_spam=hasattr(mod, 'spam'),
2697 spam=getattr(mod, 'spam', None),
2698 ))
2699 ''').rstrip()
2700
2701 @classmethod
2702 def parse(cls, text):
2703 self = super().parse(text)
2704 if not self.has_spam:
2705 del self.spam
2706 del self.has_spam
2707 return self
2708
2709
2710 @requires_singlephase_init
2711 class SinglephaseInitTests(unittest.TestCase):
2712
2713 NAME = '_testsinglephase'
2714
2715 @classmethod
2716 def setUpClass(cls):
2717 spec = importlib.util.find_spec(cls.NAME)
2718 cls.LOADER = type(spec.loader)
2719
2720 # Apple extensions must be distributed as frameworks. This requires
2721 # a specialist loader, and we need to differentiate between the
2722 # spec.origin and the original file location.
2723 if is_apple_mobile:
2724 assert cls.LOADER is AppleFrameworkLoader
2725
2726 cls.ORIGIN = spec.origin
2727 with open(spec.origin + ".origin", "r") as f:
2728 cls.FILE = os.path.join(
2729 os.path.dirname(sys.executable),
2730 f.read().strip()
2731 )
2732 else:
2733 assert cls.LOADER is ExtensionFileLoader
2734
2735 cls.ORIGIN = spec.origin
2736 cls.FILE = spec.origin
2737
2738 # Start fresh.
2739 cls.clean_up()
2740
2741 def tearDown(self):
2742 # Clean up the module.
2743 self.clean_up()
2744
2745 @classmethod
2746 def clean_up(cls):
2747 name = cls.NAME
2748 if name in sys.modules:
2749 if hasattr(sys.modules[name], '_clear_globals'):
2750 assert sys.modules[name].__file__ == cls.FILE, \
2751 f"{sys.modules[name].__file__} != {cls.FILE}"
2752
2753 sys.modules[name]._clear_globals()
2754 del sys.modules[name]
2755 # Clear all internally cached data for the extension.
2756 _testinternalcapi.clear_extension(name, cls.ORIGIN)
2757
2758 #########################
2759 # helpers
2760
2761 def add_module_cleanup(self, name):
2762 def clean_up():
2763 # Clear all internally cached data for the extension.
2764 _testinternalcapi.clear_extension(name, self.ORIGIN)
2765 self.addCleanup(clean_up)
2766
2767 def _load_dynamic(self, name, path):
2768 """
2769 Load an extension module.
2770 """
2771 # This is essentially copied from the old imp module.
2772 from importlib._bootstrap import _load
2773 loader = self.LOADER(name, path)
2774
2775 # Issue bpo-24748: Skip the sys.modules check in _load_module_shim;
2776 # always load new extension.
2777 spec = importlib.util.spec_from_file_location(name, path,
2778 loader=loader)
2779 return _load(spec)
2780
2781 def load(self, name):
2782 try:
2783 already_loaded = self.already_loaded
2784 except AttributeError:
2785 already_loaded = self.already_loaded = {}
2786 assert name not in already_loaded
2787 mod = self._load_dynamic(name, self.ORIGIN)
2788 self.assertNotIn(mod, already_loaded.values())
2789 already_loaded[name] = mod
2790 return types.SimpleNamespace(
2791 name=name,
2792 module=mod,
2793 snapshot=TestSinglePhaseSnapshot.from_module(mod),
2794 )
2795
2796 def re_load(self, name, mod):
2797 assert sys.modules[name] is mod
2798 assert mod.__dict__ == mod.__dict__
2799 reloaded = self._load_dynamic(name, self.ORIGIN)
2800 return types.SimpleNamespace(
2801 name=name,
2802 module=reloaded,
2803 snapshot=TestSinglePhaseSnapshot.from_module(reloaded),
2804 )
2805
2806 # subinterpreters
2807
2808 def add_subinterpreter(self):
2809 interpid = _interpreters.create('legacy')
2810 def ensure_destroyed():
2811 try:
2812 _interpreters.destroy(interpid)
2813 except _interpreters.InterpreterNotFoundError:
2814 pass
2815 self.addCleanup(ensure_destroyed)
2816 _interpreters.exec(interpid, textwrap.dedent('''
2817 import sys
2818 import _testinternalcapi
2819 '''))
2820 def clean_up():
2821 _interpreters.exec(interpid, textwrap.dedent(f'''
2822 name = {self.NAME!r}
2823 if name in sys.modules:
2824 sys.modules.pop(name)._clear_globals()
2825 _testinternalcapi.clear_extension(name, {self.ORIGIN!r})
2826 '''))
2827 _interpreters.destroy(interpid)
2828 self.addCleanup(clean_up)
2829 return interpid
2830
2831 def import_in_subinterp(self, interpid=None, *,
2832 postscript=None,
2833 postcleanup=False,
2834 ):
2835 name = self.NAME
2836
2837 if postcleanup:
2838 import_ = 'import _testinternalcapi' if interpid is None else ''
2839 postcleanup = f'''
2840 {import_}
2841 mod._clear_globals()
2842 _testinternalcapi.clear_extension(name, {self.ORIGIN!r})
2843 '''
2844
2845 try:
2846 pipe = self._pipe
2847 except AttributeError:
2848 r, w = pipe = self._pipe = os.pipe()
2849 self.addCleanup(os.close, r)
2850 self.addCleanup(os.close, w)
2851
2852 snapshot = TestSinglePhaseSnapshot.from_subinterp(
2853 name,
2854 interpid,
2855 pipe=pipe,
2856 import_first=True,
2857 postscript=postscript,
2858 postcleanup=postcleanup,
2859 )
2860
2861 return types.SimpleNamespace(
2862 name=name,
2863 module=None,
2864 snapshot=snapshot,
2865 )
2866
2867 # checks
2868
2869 def check_common(self, loaded):
2870 isolated = False
2871
2872 mod = loaded.module
2873 if not mod:
2874 # It came from a subinterpreter.
2875 isolated = True
2876 mod = loaded.snapshot.module
2877 # mod.__name__ might not match, but the spec will.
2878 self.assertEqual(mod.__spec__.name, loaded.name)
2879 self.assertEqual(mod.__file__, self.FILE)
2880 self.assertEqual(mod.__spec__.origin, self.ORIGIN)
2881 if not isolated:
2882 self.assertIsSubclass(mod.error, Exception)
2883 self.assertEqual(mod.int_const, 1969)
2884 self.assertEqual(mod.str_const, 'something different')
2885 self.assertIsInstance(mod._module_initialized, float)
2886 self.assertGreater(mod._module_initialized, 0)
2887
2888 snap = loaded.snapshot
2889 self.assertEqual(snap.summed, 3)
2890 if snap.state_initialized is not None:
2891 self.assertIsInstance(snap.state_initialized, float)
2892 self.assertGreater(snap.state_initialized, 0)
2893 if isolated:
2894 # The "looked up" module is interpreter-specific
2895 # (interp->imports.modules_by_index was set for the module).
2896 self.assertEqual(snap.lookedup_id, snap.id)
2897 self.assertEqual(snap.cached_id, snap.id)
2898 with self.assertRaises(AttributeError):
2899 snap.spam
2900 else:
2901 self.assertIs(snap.lookedup, mod)
2902 self.assertIs(snap.cached, mod)
2903
2904 def check_direct(self, loaded):
2905 # The module has its own PyModuleDef, with a matching name.
2906 self.assertEqual(loaded.module.__name__, loaded.name)
2907 self.assertIs(loaded.snapshot.lookedup, loaded.module)
2908
2909 def check_indirect(self, loaded, orig):
2910 # The module re-uses another's PyModuleDef, with a different name.
2911 assert orig is not loaded.module
2912 assert orig.__name__ != loaded.name
2913 self.assertNotEqual(loaded.module.__name__, loaded.name)
2914 self.assertIs(loaded.snapshot.lookedup, loaded.module)
2915
2916 def check_basic(self, loaded, expected_init_count):
2917 # m_size == -1
2918 # The module loads fresh the first time and copies m_copy after.
2919 snap = loaded.snapshot
2920 self.assertIsNot(snap.state_initialized, None)
2921 self.assertIsInstance(snap.init_count, int)
2922 self.assertGreater(snap.init_count, 0)
2923 self.assertEqual(snap.init_count, expected_init_count)
2924
2925 def check_with_reinit(self, loaded):
2926 # m_size >= 0
2927 # The module loads fresh every time.
2928 pass
2929
2930 def check_fresh(self, loaded):
2931 """
2932 The module had not been loaded before (at least since fully reset).
2933 """
2934 snap = loaded.snapshot
2935 # The module's init func was run.
2936 # A copy of the module's __dict__ was stored in def->m_base.m_copy.
2937 # The previous m_copy was deleted first.
2938 # _PyRuntime.imports.extensions was set.
2939 self.assertEqual(snap.init_count, 1)
2940 # The global state was initialized.
2941 # The module attrs were initialized from that state.
2942 self.assertEqual(snap.module._module_initialized,
2943 snap.state_initialized)
2944
2945 def check_semi_fresh(self, loaded, base, prev):
2946 """
2947 The module had been loaded before and then reset
2948 (but the module global state wasn't).
2949 """
2950 snap = loaded.snapshot
2951 # The module's init func was run again.
2952 # A copy of the module's __dict__ was stored in def->m_base.m_copy.
2953 # The previous m_copy was deleted first.
2954 # The module globals did not get reset.
2955 self.assertNotEqual(snap.id, base.snapshot.id)
2956 self.assertNotEqual(snap.id, prev.snapshot.id)
2957 self.assertEqual(snap.init_count, prev.snapshot.init_count + 1)
2958 # The global state was updated.
2959 # The module attrs were initialized from that state.
2960 self.assertEqual(snap.module._module_initialized,
2961 snap.state_initialized)
2962 self.assertNotEqual(snap.state_initialized,
2963 base.snapshot.state_initialized)
2964 self.assertNotEqual(snap.state_initialized,
2965 prev.snapshot.state_initialized)
2966
2967 def check_copied(self, loaded, base):
2968 """
2969 The module had been loaded before and never reset.
2970 """
2971 snap = loaded.snapshot
2972 # The module's init func was not run again.
2973 # The interpreter copied m_copy, as set by the other interpreter,
2974 # with objects owned by the other interpreter.
2975 # The module globals did not get reset.
2976 self.assertNotEqual(snap.id, base.snapshot.id)
2977 self.assertEqual(snap.init_count, base.snapshot.init_count)
2978 # The global state was not updated since the init func did not run.
2979 # The module attrs were not directly initialized from that state.
2980 # The state and module attrs still match the previous loading.
2981 self.assertEqual(snap.module._module_initialized,
2982 snap.state_initialized)
2983 self.assertEqual(snap.state_initialized,
2984 base.snapshot.state_initialized)
2985
2986 #########################
2987 # the tests
2988
2989 def test_cleared_globals(self):
2990 loaded = self.load(self.NAME)
2991 _testsinglephase = loaded.module
2992 init_before = _testsinglephase.state_initialized()
2993
2994 _testsinglephase._clear_globals()
2995 init_after = _testsinglephase.state_initialized()
2996 init_count = _testsinglephase.initialized_count()
2997
2998 self.assertGreater(init_before, 0)
2999 self.assertEqual(init_after, 0)
3000 self.assertEqual(init_count, -1)
3001
3002 def test_variants(self):
3003 # Exercise the most meaningful variants described in Python/import.c.
3004 self.maxDiff = None
3005
3006 # Check the "basic" module.
3007
3008 name = self.NAME
3009 expected_init_count = 1
3010 with self.subTest(name):
3011 loaded = self.load(name)
3012
3013 self.check_common(loaded)
3014 self.check_direct(loaded)
3015 self.check_basic(loaded, expected_init_count)
3016 basic = loaded.module
3017
3018 # Check its indirect variants.
3019
3020 name = f'{self.NAME}_basic_wrapper'
3021 self.add_module_cleanup(name)
3022 expected_init_count += 1
3023 with self.subTest(name):
3024 loaded = self.load(name)
3025
3026 self.check_common(loaded)
3027 self.check_indirect(loaded, basic)
3028 self.check_basic(loaded, expected_init_count)
3029
3030 # Currently PyState_AddModule() always replaces the cached module.
3031 self.assertIs(basic.look_up_self(), loaded.module)
3032 self.assertEqual(basic.initialized_count(), expected_init_count)
3033
3034 # The cached module shouldn't change after this point.
3035 basic_lookedup = loaded.module
3036
3037 # Check its direct variant.
3038
3039 name = f'{self.NAME}_basic_copy'
3040 self.add_module_cleanup(name)
3041 expected_init_count += 1
3042 with self.subTest(name):
3043 loaded = self.load(name)
3044
3045 self.check_common(loaded)
3046 self.check_direct(loaded)
3047 self.check_basic(loaded, expected_init_count)
3048
3049 # This should change the cached module for _testsinglephase.
3050 self.assertIs(basic.look_up_self(), basic_lookedup)
3051 self.assertEqual(basic.initialized_count(), expected_init_count)
3052
3053 # Check the non-basic variant that has no state.
3054
3055 name = f'{self.NAME}_with_reinit'
3056 self.add_module_cleanup(name)
3057 with self.subTest(name):
3058 loaded = self.load(name)
3059
3060 self.check_common(loaded)
3061 self.assertIs(loaded.snapshot.state_initialized, None)
3062 self.check_direct(loaded)
3063 self.check_with_reinit(loaded)
3064
3065 # This should change the cached module for _testsinglephase.
3066 self.assertIs(basic.look_up_self(), basic_lookedup)
3067 self.assertEqual(basic.initialized_count(), expected_init_count)
3068
3069 # Check the basic variant that has state.
3070
3071 name = f'{self.NAME}_with_state'
3072 self.add_module_cleanup(name)
3073 with self.subTest(name):
3074 loaded = self.load(name)
3075 self.addCleanup(loaded.module._clear_module_state)
3076
3077 self.check_common(loaded)
3078 self.assertIsNot(loaded.snapshot.state_initialized, None)
3079 self.check_direct(loaded)
3080 self.check_with_reinit(loaded)
3081
3082 # This should change the cached module for _testsinglephase.
3083 self.assertIs(basic.look_up_self(), basic_lookedup)
3084 self.assertEqual(basic.initialized_count(), expected_init_count)
3085
3086 def test_basic_reloaded(self):
3087 # m_copy is copied into the existing module object.
3088 # Global state is not changed.
3089 self.maxDiff = None
3090
3091 for name in [
3092 self.NAME, # the "basic" module
3093 f'{self.NAME}_basic_wrapper', # the indirect variant
3094 f'{self.NAME}_basic_copy', # the direct variant
3095 ]:
3096 self.add_module_cleanup(name)
3097 with self.subTest(name):
3098 loaded = self.load(name)
3099 reloaded = self.re_load(name, loaded.module)
3100
3101 self.check_common(loaded)
3102 self.check_common(reloaded)
3103
3104 # Make sure the original __dict__ did not get replaced.
3105 self.assertEqual(id(loaded.module.__dict__),
3106 loaded.snapshot.ns_id)
3107 self.assertEqual(loaded.snapshot.ns.__dict__,
3108 loaded.module.__dict__)
3109
3110 self.assertEqual(reloaded.module.__spec__.name, reloaded.name)
3111 self.assertEqual(reloaded.module.__name__,
3112 reloaded.snapshot.ns.__name__)
3113
3114 self.assertIs(reloaded.module, loaded.module)
3115 self.assertIs(reloaded.module.__dict__, loaded.module.__dict__)
3116 # It only happens to be the same but that's good enough here.
3117 # We really just want to verify that the re-loaded attrs
3118 # didn't change.
3119 self.assertIs(reloaded.snapshot.lookedup,
3120 loaded.snapshot.lookedup)
3121 self.assertEqual(reloaded.snapshot.state_initialized,
3122 loaded.snapshot.state_initialized)
3123 self.assertEqual(reloaded.snapshot.init_count,
3124 loaded.snapshot.init_count)
3125
3126 self.assertIs(reloaded.snapshot.cached, reloaded.module)
3127
3128 def test_with_reinit_reloaded(self):
3129 # The module's m_init func is run again.
3130 self.maxDiff = None
3131
3132 # Keep a reference around.
3133 basic = self.load(self.NAME)
3134
3135 for name, has_state in [
3136 (f'{self.NAME}_with_reinit', False), # m_size == 0
3137 (f'{self.NAME}_with_state', True), # m_size > 0
3138 ]:
3139 self.add_module_cleanup(name)
3140 with self.subTest(name=name, has_state=has_state):
3141 loaded = self.load(name)
3142 if has_state:
3143 self.addCleanup(loaded.module._clear_module_state)
3144
3145 reloaded = self.re_load(name, loaded.module)
3146 if has_state:
3147 self.addCleanup(reloaded.module._clear_module_state)
3148
3149 self.check_common(loaded)
3150 self.check_common(reloaded)
3151
3152 # Make sure the original __dict__ did not get replaced.
3153 self.assertEqual(id(loaded.module.__dict__),
3154 loaded.snapshot.ns_id)
3155 self.assertEqual(loaded.snapshot.ns.__dict__,
3156 loaded.module.__dict__)
3157
3158 self.assertEqual(reloaded.module.__spec__.name, reloaded.name)
3159 self.assertEqual(reloaded.module.__name__,
3160 reloaded.snapshot.ns.__name__)
3161
3162 self.assertIsNot(reloaded.module, loaded.module)
3163 self.assertNotEqual(reloaded.module.__dict__,
3164 loaded.module.__dict__)
3165 self.assertIs(reloaded.snapshot.lookedup, reloaded.module)
3166 if loaded.snapshot.state_initialized is None:
3167 self.assertIs(reloaded.snapshot.state_initialized, None)
3168 else:
3169 self.assertGreater(reloaded.snapshot.state_initialized,
3170 loaded.snapshot.state_initialized)
3171
3172 self.assertIs(reloaded.snapshot.cached, reloaded.module)
3173
3174 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
3175 def test_check_state_first(self):
3176 for variant in ['', '_with_reinit', '_with_state']:
3177 name = f'{self.NAME}{variant}_check_cache_first'
3178 with self.subTest(name):
3179 mod = self._load_dynamic(name, self.ORIGIN)
3180 self.assertEqual(mod.__name__, name)
3181 sys.modules.pop(name, None)
3182 _testinternalcapi.clear_extension(name, self.ORIGIN)
3183
3184 # Currently, for every single-phrase init module loaded
3185 # in multiple interpreters, those interpreters share a
3186 # PyModuleDef for that object, which can be a problem.
3187 # Also, we test with a single-phase module that has global state,
3188 # which is shared by all interpreters.
3189
3190 @no_rerun(reason="module state is not cleared (see gh-140657)")
3191 @requires_subinterpreters
3192 def test_basic_multiple_interpreters_main_no_reset(self):
3193 # without resetting; already loaded in main interpreter
3194
3195 # At this point:
3196 # * alive in 0 interpreters
3197 # * module def may or may not be loaded already
3198 # * module def not in _PyRuntime.imports.extensions
3199 # * mod init func has not run yet (since reset, at least)
3200 # * m_copy not set (hasn't been loaded yet or already cleared)
3201 # * module's global state has not been initialized yet
3202 # (or already cleared)
3203
3204 main_loaded = self.load(self.NAME)
3205 _testsinglephase = main_loaded.module
3206 # Attrs set after loading are not in m_copy.
3207 _testsinglephase.spam = 'spam, spam, spam, spam, eggs, and spam'
3208
3209 self.check_common(main_loaded)
3210 self.check_fresh(main_loaded)
3211
3212 interpid1 = self.add_subinterpreter()
3213 interpid2 = self.add_subinterpreter()
3214
3215 # At this point:
3216 # * alive in 1 interpreter (main)
3217 # * module def in _PyRuntime.imports.extensions
3218 # * mod init func ran for the first time (since reset, at least)
3219 # * m_copy was copied from the main interpreter (was NULL)
3220 # * module's global state was initialized
3221
3222 # Use an interpreter that gets destroyed right away.
3223 loaded = self.import_in_subinterp()
3224 self.check_common(loaded)
3225 self.check_copied(loaded, main_loaded)
3226
3227 # At this point:
3228 # * alive in 1 interpreter (main)
3229 # * module def still in _PyRuntime.imports.extensions
3230 # * mod init func ran again
3231 # * m_copy is NULL (cleared when the interpreter was destroyed)
3232 # (was from main interpreter)
3233 # * module's global state was updated, not reset
3234
3235 # Use a subinterpreter that sticks around.
3236 loaded = self.import_in_subinterp(interpid1)
3237 self.check_common(loaded)
3238 self.check_copied(loaded, main_loaded)
3239
3240 # At this point:
3241 # * alive in 2 interpreters (main, interp1)
3242 # * module def still in _PyRuntime.imports.extensions
3243 # * mod init func ran again
3244 # * m_copy was copied from interp1
3245 # * module's global state was updated, not reset
3246
3247 # Use a subinterpreter while the previous one is still alive.
3248 loaded = self.import_in_subinterp(interpid2)
3249 self.check_common(loaded)
3250 self.check_copied(loaded, main_loaded)
3251
3252 # At this point:
3253 # * alive in 3 interpreters (main, interp1, interp2)
3254 # * module def still in _PyRuntime.imports.extensions
3255 # * mod init func ran again
3256 # * m_copy was copied from interp2 (was from interp1)
3257 # * module's global state was updated, not reset
3258
3259 @no_rerun(reason="rerun not possible; module state is never cleared (see gh-102251)")
3260 @requires_subinterpreters
3261 def test_basic_multiple_interpreters_deleted_no_reset(self):
3262 # without resetting; already loaded in a deleted interpreter
3263
3264 if Py_TRACE_REFS:
3265 # It's a Py_TRACE_REFS build.
3266 # This test breaks interpreter isolation a little,
3267 # which causes problems on Py_TRACE_REF builds.
3268 raise unittest.SkipTest('crashes on Py_TRACE_REFS builds')
3269
3270 # At this point:
3271 # * alive in 0 interpreters
3272 # * module def may or may not be loaded already
3273 # * module def not in _PyRuntime.imports.extensions
3274 # * mod init func has not run yet (since reset, at least)
3275 # * m_copy not set (hasn't been loaded yet or already cleared)
3276 # * module's global state has not been initialized yet
3277 # (or already cleared)
3278
3279 interpid1 = self.add_subinterpreter()
3280 interpid2 = self.add_subinterpreter()
3281
3282 # First, load in the main interpreter but then completely clear it.
3283 loaded_main = self.load(self.NAME)
3284 loaded_main.module._clear_globals()
3285 _testinternalcapi.clear_extension(self.NAME, self.ORIGIN)
3286
3287 # At this point:
3288 # * alive in 0 interpreters
3289 # * module def loaded already
3290 # * module def was in _PyRuntime.imports.extensions, but cleared
3291 # * mod init func ran for the first time (since reset, at least)
3292 # * m_copy was set, but cleared (was NULL)
3293 # * module's global state was initialized but cleared
3294
3295 # Start with an interpreter that gets destroyed right away.
3296 base = self.import_in_subinterp(
3297 postscript='''
3298 # Attrs set after loading are not in m_copy.
3299 mod.spam = 'spam, spam, mash, spam, eggs, and spam'
3300 ''')
3301 self.check_common(base)
3302 self.check_fresh(base)
3303
3304 # At this point:
3305 # * alive in 0 interpreters
3306 # * module def in _PyRuntime.imports.extensions
3307 # * mod init func ran for the first time (since reset)
3308 # * m_copy is still set (owned by main interpreter)
3309 # * module's global state was initialized, not reset
3310
3311 # Use a subinterpreter that sticks around.
3312 loaded_interp1 = self.import_in_subinterp(interpid1)
3313 self.check_common(loaded_interp1)
3314 self.check_copied(loaded_interp1, base)
3315
3316 # At this point:
3317 # * alive in 1 interpreter (interp1)
3318 # * module def still in _PyRuntime.imports.extensions
3319 # * mod init func did not run again
3320 # * m_copy was not changed
3321 # * module's global state was not touched
3322
3323 # Use a subinterpreter while the previous one is still alive.
3324 loaded_interp2 = self.import_in_subinterp(interpid2)
3325 self.check_common(loaded_interp2)
3326 self.check_copied(loaded_interp2, loaded_interp1)
3327
3328 # At this point:
3329 # * alive in 2 interpreters (interp1, interp2)
3330 # * module def still in _PyRuntime.imports.extensions
3331 # * mod init func did not run again
3332 # * m_copy was not changed
3333 # * module's global state was not touched
3334
3335 @requires_subinterpreters
3336 def test_basic_multiple_interpreters_reset_each(self):
3337 # resetting between each interpreter
3338
3339 # At this point:
3340 # * alive in 0 interpreters
3341 # * module def may or may not be loaded already
3342 # * module def not in _PyRuntime.imports.extensions
3343 # * mod init func has not run yet (since reset, at least)
3344 # * m_copy not set (hasn't been loaded yet or already cleared)
3345 # * module's global state has not been initialized yet
3346 # (or already cleared)
3347
3348 interpid1 = self.add_subinterpreter()
3349 interpid2 = self.add_subinterpreter()
3350
3351 # Use an interpreter that gets destroyed right away.
3352 loaded = self.import_in_subinterp(
3353 postscript='''
3354 # Attrs set after loading are not in m_copy.
3355 mod.spam = 'spam, spam, mash, spam, eggs, and spam'
3356 ''',
3357 postcleanup=True,
3358 )
3359 self.check_common(loaded)
3360 self.check_fresh(loaded)
3361
3362 # At this point:
3363 # * alive in 0 interpreters
3364 # * module def in _PyRuntime.imports.extensions
3365 # * mod init func ran for the first time (since reset, at least)
3366 # * m_copy is NULL (cleared when the interpreter was destroyed)
3367 # * module's global state was initialized, not reset
3368
3369 # Use a subinterpreter that sticks around.
3370 loaded = self.import_in_subinterp(interpid1, postcleanup=True)
3371 self.check_common(loaded)
3372 self.check_fresh(loaded)
3373
3374 # At this point:
3375 # * alive in 1 interpreter (interp1)
3376 # * module def still in _PyRuntime.imports.extensions
3377 # * mod init func ran again
3378 # * m_copy was copied from interp1 (was NULL)
3379 # * module's global state was initialized, not reset
3380
3381 # Use a subinterpreter while the previous one is still alive.
3382 loaded = self.import_in_subinterp(interpid2, postcleanup=True)
3383 self.check_common(loaded)
3384 self.check_fresh(loaded)
3385
3386 # At this point:
3387 # * alive in 2 interpreters (interp2, interp2)
3388 # * module def still in _PyRuntime.imports.extensions
3389 # * mod init func ran again
3390 # * m_copy was copied from interp2 (was from interp1)
3391 # * module's global state was initialized, not reset
3392
3393
3394 @cpython_only
3395 class TestMagicNumber(unittest.TestCase):
3396 def test_magic_number_endianness(self):
3397 magic_number_bytes = _imp.pyc_magic_number_token.to_bytes(4, 'little')
3398 self.assertEqual(magic_number_bytes[2:], b'\r\n')
3399 # Starting with Python 3.11, Python 3.n starts with magic number 2900+50n.
3400 magic_number = int.from_bytes(magic_number_bytes[:2], 'little')
3401 start = 2900 + sys.version_info.minor * 50
3402 self.assertIn(magic_number, range(start, start + 50))
3403
3404
3405 if __name__ == '__main__':
3406 # Test needs to be a package, so we can do relative imports.
3407 unittest.main()