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