From 83b6a1dfa6d406eea5143c29ab90f4efab42fe59 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 20 Sep 2023 15:41:36 +0300 Subject: [PATCH 1/3] gh-108303: Move all inspect test files to `test_inspect/` --- Lib/test/libregrtest/findtests.py | 1 + Lib/test/support/import_helper.py | 26 ++++++++++++- Lib/test/test_import/__init__.py | 39 +++++-------------- Lib/test/test_inspect/__init__.py | 6 +++ Lib/test/{ => test_inspect}/inspect_fodder.py | 0 .../{ => test_inspect}/inspect_fodder2.py | 0 .../inspect_stock_annotations.py | 0 .../inspect_stringized_annotations.py | 0 .../inspect_stringized_annotations_2.py | 0 Lib/test/{ => test_inspect}/test_inspect.py | 15 ++++--- Makefile.pre.in | 1 + 11 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 Lib/test/test_inspect/__init__.py rename Lib/test/{ => test_inspect}/inspect_fodder.py (100%) rename Lib/test/{ => test_inspect}/inspect_fodder2.py (100%) rename Lib/test/{ => test_inspect}/inspect_stock_annotations.py (100%) rename Lib/test/{ => test_inspect}/inspect_stringized_annotations.py (100%) rename Lib/test/{ => test_inspect}/inspect_stringized_annotations_2.py (100%) rename Lib/test/{ => test_inspect}/test_inspect.py (99%) diff --git a/Lib/test/libregrtest/findtests.py b/Lib/test/libregrtest/findtests.py index 60f21980c10dd0..b2f5a031a01121 100644 --- a/Lib/test/libregrtest/findtests.py +++ b/Lib/test/libregrtest/findtests.py @@ -19,6 +19,7 @@ "test_asyncio", "test_concurrent_futures", "test_future_stmt", + "test_inspect", "test_multiprocessing_fork", "test_multiprocessing_forkserver", "test_multiprocessing_spawn", diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index 67f18e530edc4b..f11b51b57939c6 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -8,7 +8,7 @@ import unittest import warnings -from .os_helper import unlink +from .os_helper import unlink, temp_dir @contextlib.contextmanager @@ -274,3 +274,27 @@ 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=""): + # To fix import cycles: + from test.support import script_helper + + # 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] diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 33bce779f6cc01..caec10cac3a35a 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -1,5 +1,4 @@ import builtins -import contextlib import errno import glob import json @@ -30,9 +29,10 @@ STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten, is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS) 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 +125,6 @@ def wrapper(self): 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 +380,7 @@ def test_from_import_missing_attr_path_is_canonical(self): 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 +389,7 @@ def test_from_import_star_invalid_type(self): ): 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 +797,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 +816,7 @@ def test_creation_mode(self): 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 +832,7 @@ def test_cached_mode_issue_2051(self): @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 +847,7 @@ def test_cached_readonly(self): 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/__init__.py b/Lib/test/test_inspect/__init__.py new file mode 100644 index 00000000000000..f2a39a3fe29c7f --- /dev/null +++ b/Lib/test/test_inspect/__init__.py @@ -0,0 +1,6 @@ +import os +from test import support + + +def load_tests(*args): + return support.load_package_tests(os.path.dirname(__file__), *args) diff --git a/Lib/test/inspect_fodder.py b/Lib/test/test_inspect/inspect_fodder.py similarity index 100% rename from Lib/test/inspect_fodder.py rename to Lib/test/test_inspect/inspect_fodder.py diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/test_inspect/inspect_fodder2.py similarity index 100% rename from Lib/test/inspect_fodder2.py rename to Lib/test/test_inspect/inspect_fodder2.py diff --git a/Lib/test/inspect_stock_annotations.py b/Lib/test/test_inspect/inspect_stock_annotations.py similarity index 100% rename from Lib/test/inspect_stock_annotations.py rename to Lib/test/test_inspect/inspect_stock_annotations.py diff --git a/Lib/test/inspect_stringized_annotations.py b/Lib/test/test_inspect/inspect_stringized_annotations.py similarity index 100% rename from Lib/test/inspect_stringized_annotations.py rename to Lib/test/test_inspect/inspect_stringized_annotations.py diff --git a/Lib/test/inspect_stringized_annotations_2.py b/Lib/test/test_inspect/inspect_stringized_annotations_2.py similarity index 100% rename from Lib/test/inspect_stringized_annotations_2.py rename to Lib/test/test_inspect/inspect_stringized_annotations_2.py diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect/test_inspect.py similarity index 99% rename from Lib/test/test_inspect.py rename to Lib/test/test_inspect/test_inspect.py index 2fb356a0529ab0..ac5b099959f02d 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -33,17 +33,16 @@ 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 -from test import inspect_fodder2 as mod2 from test import support -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 +from test.test_inspect import inspect_fodder as mod +from test.test_inspect import inspect_fodder2 as mod2 +from test.test_inspect import inspect_stock_annotations +from test.test_inspect import inspect_stringized_annotations +from test.test_inspect import inspect_stringized_annotations_2 # Functions tested in this suite: @@ -4954,7 +4953,7 @@ def assertInspectEqual(self, path, source): 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: diff --git a/Makefile.pre.in b/Makefile.pre.in index 363be687d5a432..6a67c47a418585 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -2168,6 +2168,7 @@ TESTSUBDIRS= idlelib/idle_test \ test/test_email \ test/test_email/data \ test/test_future_stmt \ + test/test_inspect \ test/test_import \ test/test_import/data \ test/test_import/data/circular_imports \ From 9e35f08cfbb5ca23d46dfce1db1b9d7f22b543e1 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 20 Sep 2023 16:15:44 +0300 Subject: [PATCH 2/3] Update test_tokenize --- Lib/test/test_tokenize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py index 94fb6d933de114..8e92e00f53eba2 100644 --- a/Lib/test/test_tokenize.py +++ b/Lib/test/test_tokenize.py @@ -1862,7 +1862,7 @@ def test_random_files(self): # TODO: Remove this once we can untokenize PEP 701 syntax testfiles.remove(os.path.join(tempdir, "test_fstring.py")) - for f in ('buffer', 'builtin', 'fileio', 'inspect', 'os', 'platform', 'sys'): + for f in ('buffer', 'builtin', 'fileio', 'os', 'platform', 'sys'): testfiles.remove(os.path.join(tempdir, "test_%s.py") % f) if not support.is_resource_enabled("cpu"): From 8d791b9be95d4906fa2f95b59c4a4c6d3e692a91 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 21 Sep 2023 10:53:23 +0300 Subject: [PATCH 3/3] Address review --- Lib/test/test_inspect/test_inspect.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index ac5b099959f02d..31ac6b1070a0a2 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -38,11 +38,11 @@ from test.support.script_helper import assert_python_ok, assert_python_failure from test import support -from test.test_inspect import inspect_fodder as mod -from test.test_inspect import inspect_fodder2 as mod2 -from test.test_inspect import inspect_stock_annotations -from test.test_inspect import inspect_stringized_annotations -from test.test_inspect import inspect_stringized_annotations_2 +from . import inspect_fodder as mod +from . import inspect_fodder2 as mod2 +from . import inspect_stock_annotations +from . import inspect_stringized_annotations +from . import inspect_stringized_annotations_2 # Functions tested in this suite: