summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-10-02 15:35:03 (GMT)
committerGitHub <noreply@github.com>2023-10-02 15:35:03 (GMT)
commit8c5fd2105c3d592883b682b5788355aed3f785d7 (patch)
tree09465e71cf139f12fd07a01efb76e49322773730
parent150bd302bb78ad3ed0524447cfa25805b1e22763 (diff)
downloadcpython-8c5fd2105c3d592883b682b5788355aed3f785d7.zip
cpython-8c5fd2105c3d592883b682b5788355aed3f785d7.tar.gz
cpython-8c5fd2105c3d592883b682b5788355aed3f785d7.tar.bz2
[3.12] gh-109625: Move _ready_to_import() from test_import to support.import_helper (GH-109626) (#109640)
gh-109625: Move _ready_to_import() from test_import to support.import_helper (GH-109626) (cherry picked from commit 115c49ad5a5ccfb628fef3ae06a566f7a0197f97) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
-rw-r--r--Lib/test/support/import_helper.py25
-rw-r--r--Lib/test/test_import/__init__.py38
-rw-r--r--Lib/test/test_inspect.py6
3 files changed, 35 insertions, 34 deletions
diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py
index 67f18e5..3d804f2 100644
--- a/Lib/test/support/import_helper.py
+++ b/Lib/test/support/import_helper.py
@@ -8,7 +8,7 @@ import sys
import unittest
import warnings
-from .os_helper import unlink
+from .os_helper import unlink, temp_dir
@contextlib.contextmanager
@@ -274,3 +274,26 @@ def mock_register_at_fork(func):
# memory.
from unittest import mock
return mock.patch('os.register_at_fork', create=True)(func)
+
+
+@contextlib.contextmanager
+def ready_to_import(name=None, source=""):
+ from test.support import script_helper
+
+ # 1. Sets up a temporary directory and removes it afterwards
+ # 2. Creates the module file
+ # 3. Temporarily clears the module from sys.modules (if any)
+ # 4. Reverts or removes the module when cleaning up
+ name = name or "spam"
+ with temp_dir() as tempdir:
+ path = script_helper.make_script(tempdir, name, source)
+ old_module = sys.modules.pop(name, None)
+ try:
+ sys.path.insert(0, tempdir)
+ yield name, path
+ sys.path.remove(tempdir)
+ finally:
+ if old_module is not None:
+ sys.modules[name] = old_module
+ else:
+ sys.modules.pop(name, None)
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 62585b2..8994f14 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -30,9 +30,10 @@ from test.support import (
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten,
is_wasi, run_in_subinterp, run_in_subinterp_with_config)
from test.support.import_helper import (
- forget, make_legacy_pyc, unlink, unload, DirsOnSysPath, CleanImport)
+ forget, make_legacy_pyc, unlink, unload, ready_to_import,
+ DirsOnSysPath, CleanImport)
from test.support.os_helper import (
- TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE, temp_dir)
+ TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
from test.support import script_helper
from test.support import threading_helper
from test.test_importlib.util import uncache
@@ -125,27 +126,6 @@ def no_rerun(reason):
return deco
-@contextlib.contextmanager
-def _ready_to_import(name=None, source=""):
- # sets up a temporary directory and removes it
- # creates the module file
- # temporarily clears the module from sys.modules (if any)
- # reverts or removes the module when cleaning up
- name = name or "spam"
- with temp_dir() as tempdir:
- path = script_helper.make_script(tempdir, name, source)
- old_module = sys.modules.pop(name, None)
- try:
- sys.path.insert(0, tempdir)
- yield name, path
- sys.path.remove(tempdir)
- finally:
- if old_module is not None:
- sys.modules[name] = old_module
- elif name in sys.modules:
- del sys.modules[name]
-
-
if _testsinglephase is not None:
def restore__testsinglephase(*, _orig=_testsinglephase):
# We started with the module imported and want to restore
@@ -401,7 +381,7 @@ class ImportTests(unittest.TestCase):
def test_from_import_star_invalid_type(self):
import re
- with _ready_to_import() as (name, path):
+ with ready_to_import() as (name, path):
with open(path, 'w', encoding='utf-8') as f:
f.write("__all__ = [b'invalid_type']")
globals = {}
@@ -410,7 +390,7 @@ class ImportTests(unittest.TestCase):
):
exec(f"from {name} import *", globals)
self.assertNotIn(b"invalid_type", globals)
- with _ready_to_import() as (name, path):
+ with ready_to_import() as (name, path):
with open(path, 'w', encoding='utf-8') as f:
f.write("globals()[b'invalid_type'] = object()")
globals = {}
@@ -818,7 +798,7 @@ class FilePermissionTests(unittest.TestCase):
)
def test_creation_mode(self):
mask = 0o022
- with temp_umask(mask), _ready_to_import() as (name, path):
+ with temp_umask(mask), ready_to_import() as (name, path):
cached_path = importlib.util.cache_from_source(path)
module = __import__(name)
if not os.path.exists(cached_path):
@@ -837,7 +817,7 @@ class FilePermissionTests(unittest.TestCase):
def test_cached_mode_issue_2051(self):
# permissions of .pyc should match those of .py, regardless of mask
mode = 0o600
- with temp_umask(0o022), _ready_to_import() as (name, path):
+ with temp_umask(0o022), ready_to_import() as (name, path):
cached_path = importlib.util.cache_from_source(path)
os.chmod(path, mode)
__import__(name)
@@ -853,7 +833,7 @@ class FilePermissionTests(unittest.TestCase):
@os_helper.skip_unless_working_chmod
def test_cached_readonly(self):
mode = 0o400
- with temp_umask(0o022), _ready_to_import() as (name, path):
+ with temp_umask(0o022), ready_to_import() as (name, path):
cached_path = importlib.util.cache_from_source(path)
os.chmod(path, mode)
__import__(name)
@@ -868,7 +848,7 @@ class FilePermissionTests(unittest.TestCase):
def test_pyc_always_writable(self):
# Initially read-only .pyc files on Windows used to cause problems
# with later updates, see issue #6074 for details
- with _ready_to_import() as (name, path):
+ with ready_to_import() as (name, path):
# Write a Python file, make it read-only and import it
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'original'\n")
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index f0ee831..6f260c9 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -28,7 +28,7 @@ except ImportError:
from test.support import cpython_only
from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ
-from test.support.import_helper import DirsOnSysPath
+from test.support.import_helper import DirsOnSysPath, ready_to_import
from test.support.os_helper import TESTFN
from test.support.script_helper import assert_python_ok, assert_python_failure
from test import inspect_fodder as mod
@@ -38,8 +38,6 @@ from test import inspect_stock_annotations
from test import inspect_stringized_annotations
from test import inspect_stringized_annotations_2
-from test.test_import import _ready_to_import
-
# Functions tested in this suite:
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
@@ -4739,7 +4737,7 @@ def foo():
def test_getsource_reload(self):
# see issue 1218234
- with _ready_to_import('reload_bug', self.src_before) as (name, path):
+ with ready_to_import('reload_bug', self.src_before) as (name, path):
module = importlib.import_module(name)
self.assertInspectEqual(path, module)
with open(path, 'w', encoding='utf-8') as src: