update imp.py from CPython 3.11 (#5054)

This commit is contained in:
Reid00
2023-08-31 16:44:31 +08:00
committed by GitHub
parent 64c66e00d6
commit 3900a086b8
3 changed files with 56 additions and 17 deletions

4
Lib/imp.py vendored
View File

@@ -9,7 +9,7 @@ functionality over this module.
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_)

64
Lib/test/test_imp.py vendored
View File

@@ -1,3 +1,4 @@
import gc
import importlib
import importlib.util
import os
@@ -8,19 +9,21 @@ from test import support
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 @@ class ImportTests(unittest.TestCase):
# 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 @@ class ImportTests(unittest.TestCase):
# 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 @@ class ImportTests(unittest.TestCase):
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

View File

@@ -154,6 +154,11 @@ mod _imp {
// TODO:
}
#[pyfunction]
fn _frozen_module_names(_code: PyObjectRef) {
// TODO:
}
#[allow(clippy::type_complexity)]
#[pyfunction]
fn find_frozen(