Skip to content

Commit 115c49a

Browse files
authored
gh-109625: Move _ready_to_import() from test_import to support.import_helper (#109626)
1 parent 712cb17 commit 115c49a

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

Lib/test/support/import_helper.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import unittest
99
import warnings
1010

11-
from .os_helper import unlink
11+
from .os_helper import unlink, temp_dir
1212

1313

1414
@contextlib.contextmanager
@@ -274,3 +274,26 @@ def mock_register_at_fork(func):
274274
# memory.
275275
from unittest import mock
276276
return mock.patch('os.register_at_fork', create=True)(func)
277+
278+
279+
@contextlib.contextmanager
280+
def ready_to_import(name=None, source=""):
281+
from test.support import script_helper
282+
283+
# 1. Sets up a temporary directory and removes it afterwards
284+
# 2. Creates the module file
285+
# 3. Temporarily clears the module from sys.modules (if any)
286+
# 4. Reverts or removes the module when cleaning up
287+
name = name or "spam"
288+
with temp_dir() as tempdir:
289+
path = script_helper.make_script(tempdir, name, source)
290+
old_module = sys.modules.pop(name, None)
291+
try:
292+
sys.path.insert(0, tempdir)
293+
yield name, path
294+
sys.path.remove(tempdir)
295+
finally:
296+
if old_module is not None:
297+
sys.modules[name] = old_module
298+
else:
299+
sys.modules.pop(name, None)

Lib/test/test_import/__init__.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten,
3131
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS)
3232
from test.support.import_helper import (
33-
forget, make_legacy_pyc, unlink, unload, DirsOnSysPath, CleanImport)
33+
forget, make_legacy_pyc, unlink, unload, ready_to_import,
34+
DirsOnSysPath, CleanImport)
3435
from test.support.os_helper import (
35-
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE, temp_dir)
36+
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
3637
from test.support import script_helper
3738
from test.support import threading_helper
3839
from test.test_importlib.util import uncache
@@ -125,27 +126,6 @@ def wrapper(self):
125126
return deco
126127

127128

128-
@contextlib.contextmanager
129-
def _ready_to_import(name=None, source=""):
130-
# sets up a temporary directory and removes it
131-
# creates the module file
132-
# temporarily clears the module from sys.modules (if any)
133-
# reverts or removes the module when cleaning up
134-
name = name or "spam"
135-
with temp_dir() as tempdir:
136-
path = script_helper.make_script(tempdir, name, source)
137-
old_module = sys.modules.pop(name, None)
138-
try:
139-
sys.path.insert(0, tempdir)
140-
yield name, path
141-
sys.path.remove(tempdir)
142-
finally:
143-
if old_module is not None:
144-
sys.modules[name] = old_module
145-
elif name in sys.modules:
146-
del sys.modules[name]
147-
148-
149129
if _testsinglephase is not None:
150130
def restore__testsinglephase(*, _orig=_testsinglephase):
151131
# We started with the module imported and want to restore
@@ -401,7 +381,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
401381

402382
def test_from_import_star_invalid_type(self):
403383
import re
404-
with _ready_to_import() as (name, path):
384+
with ready_to_import() as (name, path):
405385
with open(path, 'w', encoding='utf-8') as f:
406386
f.write("__all__ = [b'invalid_type']")
407387
globals = {}
@@ -410,7 +390,7 @@ def test_from_import_star_invalid_type(self):
410390
):
411391
exec(f"from {name} import *", globals)
412392
self.assertNotIn(b"invalid_type", globals)
413-
with _ready_to_import() as (name, path):
393+
with ready_to_import() as (name, path):
414394
with open(path, 'w', encoding='utf-8') as f:
415395
f.write("globals()[b'invalid_type'] = object()")
416396
globals = {}
@@ -818,7 +798,7 @@ class FilePermissionTests(unittest.TestCase):
818798
)
819799
def test_creation_mode(self):
820800
mask = 0o022
821-
with temp_umask(mask), _ready_to_import() as (name, path):
801+
with temp_umask(mask), ready_to_import() as (name, path):
822802
cached_path = importlib.util.cache_from_source(path)
823803
module = __import__(name)
824804
if not os.path.exists(cached_path):
@@ -837,7 +817,7 @@ def test_creation_mode(self):
837817
def test_cached_mode_issue_2051(self):
838818
# permissions of .pyc should match those of .py, regardless of mask
839819
mode = 0o600
840-
with temp_umask(0o022), _ready_to_import() as (name, path):
820+
with temp_umask(0o022), ready_to_import() as (name, path):
841821
cached_path = importlib.util.cache_from_source(path)
842822
os.chmod(path, mode)
843823
__import__(name)
@@ -853,7 +833,7 @@ def test_cached_mode_issue_2051(self):
853833
@os_helper.skip_unless_working_chmod
854834
def test_cached_readonly(self):
855835
mode = 0o400
856-
with temp_umask(0o022), _ready_to_import() as (name, path):
836+
with temp_umask(0o022), ready_to_import() as (name, path):
857837
cached_path = importlib.util.cache_from_source(path)
858838
os.chmod(path, mode)
859839
__import__(name)
@@ -868,7 +848,7 @@ def test_cached_readonly(self):
868848
def test_pyc_always_writable(self):
869849
# Initially read-only .pyc files on Windows used to cause problems
870850
# with later updates, see issue #6074 for details
871-
with _ready_to_import() as (name, path):
851+
with ready_to_import() as (name, path):
872852
# Write a Python file, make it read-only and import it
873853
with open(path, 'w', encoding='utf-8') as f:
874854
f.write("x = 'original'\n")

Lib/test/test_inspect.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from test.support import cpython_only
3535
from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ
36-
from test.support.import_helper import DirsOnSysPath
36+
from test.support.import_helper import DirsOnSysPath, ready_to_import
3737
from test.support.os_helper import TESTFN
3838
from test.support.script_helper import assert_python_ok, assert_python_failure
3939
from test import inspect_fodder as mod
@@ -43,8 +43,6 @@
4343
from test import inspect_stringized_annotations
4444
from test import inspect_stringized_annotations_2
4545

46-
from test.test_import import _ready_to_import
47-
4846

4947
# Functions tested in this suite:
5048
# ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode,
@@ -4954,7 +4952,7 @@ def assertInspectEqual(self, path, source):
49544952

49554953
def test_getsource_reload(self):
49564954
# see issue 1218234
4957-
with _ready_to_import('reload_bug', self.src_before) as (name, path):
4955+
with ready_to_import('reload_bug', self.src_before) as (name, path):
49584956
module = importlib.import_module(name)
49594957
self.assertInspectEqual(path, module)
49604958
with open(path, 'w', encoding='utf-8') as src:

0 commit comments

Comments
 (0)