diff --git a/Lib/imp.py b/Lib/imp.py index e02aaef344..fc42c15765 100644 --- a/Lib/imp.py +++ b/Lib/imp.py @@ -9,7 +9,7 @@ from _imp import (lock_held, acquire_lock, release_lock, get_frozen_object, is_frozen_package, init_frozen, is_builtin, is_frozen, - _fix_co_filename) + _fix_co_filename, _frozen_module_names) try: from _imp import create_dynamic except ImportError: @@ -226,7 +226,7 @@ def load_module(name, file, filename, details): """ suffix, mode, type_ = details - if mode and (not mode.startswith(('r', 'U')) or '+' in mode): + if mode and (not mode.startswith('r') or '+' in mode): raise ValueError('invalid file open mode {!r}'.format(mode)) elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: msg = 'file object required for import (type code {})'.format(type_) diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py index 9b602582e2..1ccd072b3f 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -1,3 +1,4 @@ +import gc import importlib import importlib.util import os @@ -8,19 +9,21 @@ from test.support import import_helper from test.support import os_helper from test.support import script_helper +from test.support import warnings_helper import unittest import warnings -with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - import imp +imp = warnings_helper.import_deprecated('imp') import _imp +OS_PATH_NAME = os.path.__name__ + + def requires_load_dynamic(meth): """Decorator to skip a test if not running under CPython or lacking imp.load_dynamic().""" meth = support.cpython_only(meth) - return unittest.skipIf(not hasattr(imp, 'load_dynamic'), + return unittest.skipIf(getattr(imp, 'load_dynamic', None) is None, 'imp.load_dynamic() required')(meth) @@ -216,15 +219,17 @@ def test_load_from_source(self): # state after reversion. Reinitialising the module contents # and just reverting os.environ to its previous state is an OK # workaround - orig_path = os.path - orig_getenv = os.getenv - with os_helper.EnvironmentVarGuard(): - x = imp.find_module("os") - self.addCleanup(x[0].close) - new_os = imp.load_module("os", *x) - self.assertIs(os, new_os) - self.assertIs(orig_path, new_os.path) - self.assertIsNot(orig_getenv, new_os.getenv) + with import_helper.CleanImport('os', 'os.path', OS_PATH_NAME): + import os + orig_path = os.path + orig_getenv = os.getenv + with os_helper.EnvironmentVarGuard(): + x = imp.find_module("os") + self.addCleanup(x[0].close) + new_os = imp.load_module("os", *x) + self.assertIs(os, new_os) + self.assertIs(orig_path, new_os.path) + self.assertIsNot(orig_getenv, new_os.getenv) @requires_load_dynamic def test_issue15828_load_extensions(self): @@ -351,8 +356,8 @@ def test_issue_35321(self): # TODO: RUSTPYTHON @unittest.expectedFailure def test_source_hash(self): - self.assertEqual(_imp.source_hash(42, b'hi'), b'\xc6\xe7Z\r\x03:}\xab') - self.assertEqual(_imp.source_hash(43, b'hi'), b'\x85\x9765\xf8\x9a\x8b9') + self.assertEqual(_imp.source_hash(42, b'hi'), b'\xfb\xd9G\x05\xaf$\x9b~') + self.assertEqual(_imp.source_hash(43, b'hi'), b'\xd0/\x87C\xccC\xff\xe2') def test_pyc_invalidation_mode_from_cmdline(self): cases = [ @@ -384,6 +389,35 @@ def test_find_and_load_checked_pyc(self): self.assertEqual(mod.x, 42) + @support.cpython_only + def test_create_builtin_subinterp(self): + # gh-99578: create_builtin() behavior changes after the creation of the + # first sub-interpreter. Test both code paths, before and after the + # creation of a sub-interpreter. Previously, create_builtin() had + # a reference leak after the creation of the first sub-interpreter. + + import builtins + create_builtin = support.get_attribute(_imp, "create_builtin") + class Spec: + name = "builtins" + spec = Spec() + + def check_get_builtins(): + refcnt = sys.getrefcount(builtins) + mod = _imp.create_builtin(spec) + self.assertIs(mod, builtins) + self.assertEqual(sys.getrefcount(builtins), refcnt + 1) + # Check that a GC collection doesn't crash + gc.collect() + + check_get_builtins() + + ret = support.run_in_subinterp("import builtins") + self.assertEqual(ret, 0) + + check_get_builtins() + + class ReloadTests(unittest.TestCase): """Very basic tests to make sure that imp.reload() operates just like diff --git a/vm/src/stdlib/imp.rs b/vm/src/stdlib/imp.rs index 773afed6db..e2e735acd1 100644 --- a/vm/src/stdlib/imp.rs +++ b/vm/src/stdlib/imp.rs @@ -154,6 +154,11 @@ mod _imp { // TODO: } + #[pyfunction] + fn _frozen_module_names(_code: PyObjectRef) { + // TODO: + } + #[allow(clippy::type_complexity)] #[pyfunction] fn find_frozen(