From c5fa18b3a667f3866b155c2d06a5fccffd5feb5e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 8 Mar 2024 00:08:11 +0100 Subject: [PATCH 1/7] gh-116303: Handle disabled test modules in test.support --- Lib/test/support/__init__.py | 20 ++++++++++++++++---- Lib/test/support/bytecode_helper.py | 11 +++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 505d4bea83fe8d..33b5700ad0d69d 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1705,7 +1705,10 @@ def run_in_subinterp(code): module is enabled. """ _check_tracemalloc() - import _testcapi + try: + import _testcapi + except ImportError: + raise unittest.SkipTest("requires _testcapi") return _testcapi.run_in_subinterp(code) @@ -1715,7 +1718,10 @@ def run_in_subinterp_with_config(code, *, own_gil=None, **config): module is enabled. """ _check_tracemalloc() - import _testinternalcapi + try: + import _testinternalcapi + except ImportError: + raise unittest.SkipTest("requires _testinternalcapi") if own_gil is not None: assert 'gil' not in config, (own_gil, config) config['gil'] = 2 if own_gil else 1 @@ -1877,12 +1883,18 @@ def restore(self): def with_pymalloc(): - import _testcapi + try: + import _testcapi + except ImportError: + raise unittest.SkipTest("requires _testcapi") return _testcapi.WITH_PYMALLOC and not Py_GIL_DISABLED def with_mimalloc(): - import _testcapi + try: + import _testcapi + except ImportError: + raise unittest.SkipTest("requires _testcapi") return _testcapi.WITH_MIMALLOC diff --git a/Lib/test/support/bytecode_helper.py b/Lib/test/support/bytecode_helper.py index a4845065a5322e..32d4ec06609177 100644 --- a/Lib/test/support/bytecode_helper.py +++ b/Lib/test/support/bytecode_helper.py @@ -3,7 +3,7 @@ import unittest import dis import io -from _testinternalcapi import compiler_codegen, optimize_cfg, assemble_code_object +from test.support import import_helper _UNSPECIFIED = object() @@ -136,20 +136,23 @@ def complete_insts_info(self, insts): class CodegenTestCase(CompilationStepTestCase): def generate_code(self, ast): - insts, _ = compiler_codegen(ast, "my_file.py", 0) + _testinternalcapi = import_helper.import_module("_testinternalcapi") + insts, _ = _testinternalcapi.compiler_codegen(ast, "my_file.py", 0) return insts class CfgOptimizationTestCase(CompilationStepTestCase): def get_optimized(self, insts, consts, nlocals=0): + _testinternalcapi = import_helper.import_module("_testinternalcapi") insts = self.normalize_insts(insts) insts = self.complete_insts_info(insts) - insts = optimize_cfg(insts, consts, nlocals) + insts = _testinternalcapi.optimize_cfg(insts, consts, nlocals) return insts, consts class AssemblerTestCase(CompilationStepTestCase): def get_code_object(self, filename, insts, metadata): - co = assemble_code_object(filename, insts, metadata) + _testinternalcapi = import_helper.import_module("_testinternalcapi") + co = _testinternalcapi.assemble_code_object(filename, insts, metadata) return co From 1169a9536a1e5b8489bbda630dba96180cdc9517 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 10 Mar 2024 11:54:50 +0100 Subject: [PATCH 2/7] Address review: use sysconfig.get_config_var in with_*malloc() helpers --- Lib/test/support/__init__.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 33b5700ad0d69d..89bec77ee176cf 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1883,19 +1883,12 @@ def restore(self): def with_pymalloc(): - try: - import _testcapi - except ImportError: - raise unittest.SkipTest("requires _testcapi") - return _testcapi.WITH_PYMALLOC and not Py_GIL_DISABLED + WITH_PYMALLOC = bool(sysconfig.get_config_var("WITH_PYMALLOC")) + return WITH_PYMALLOC and not Py_GIL_DISABLED def with_mimalloc(): - try: - import _testcapi - except ImportError: - raise unittest.SkipTest("requires _testcapi") - return _testcapi.WITH_MIMALLOC + return bool(sysconfig.get_config_var("WITH_MIMALLOC")) class _ALWAYS_EQ: From 23c448bdbcb5c1ca3e8d05055425f040ea705447 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 10 Mar 2024 12:00:02 +0100 Subject: [PATCH 3/7] Add test.support.requires_test_modules --- Lib/test/support/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 89bec77ee176cf..1d7b71ab580df6 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -40,7 +40,7 @@ "anticipate_failure", "load_package_tests", "detect_api_mismatch", "check__all__", "skip_if_buggy_ucrt_strfptime", "check_disallow_instantiation", "check_sanitizer", "skip_if_sanitizer", - "requires_limited_api", "requires_specialization", + "requires_test_modules", "requires_limited_api", "requires_specialization", # sys "MS_WINDOWS", "is_jython", "is_android", "is_emscripten", "is_wasi", "is_apple_mobile", "check_impl_detail", "unix_shell", "setswitchinterval", @@ -1150,6 +1150,12 @@ def refcount_test(test): return no_tracing(cpython_only(test)) +def requires_test_modules(test): + if sysconfig.get_config_var("TEST_MODULES") != "yes": + return unittest.skip("needs test extension modules")(test) + return test + + def requires_limited_api(test): try: import _testcapi From b8af54a78bf55720f90eed6a589b63e67379b67b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 27 Mar 2024 21:42:51 +0100 Subject: [PATCH 4/7] Remove requires_test_modules() for now --- Lib/test/support/__init__.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 332870999adff3..591e05a3c50822 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -40,7 +40,7 @@ "anticipate_failure", "load_package_tests", "detect_api_mismatch", "check__all__", "skip_if_buggy_ucrt_strfptime", "check_disallow_instantiation", "check_sanitizer", "skip_if_sanitizer", - "requires_test_modules", "requires_limited_api", "requires_specialization", + "requires_limited_api", "requires_specialization", # sys "MS_WINDOWS", "is_jython", "is_android", "is_emscripten", "is_wasi", "is_apple_mobile", "check_impl_detail", "unix_shell", "setswitchinterval", @@ -1160,12 +1160,6 @@ def refcount_test(test): return no_tracing(cpython_only(test)) -def requires_test_modules(test): - if sysconfig.get_config_var("TEST_MODULES") != "yes": - return unittest.skip("needs test extension modules")(test) - return test - - def requires_limited_api(test): try: import _testcapi From 10fa3a806f732949da32a139f65001dd14c7dd63 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 28 Mar 2024 01:17:11 +0100 Subject: [PATCH 5/7] Revert 1169a9536a1e5b8489bbda630dba96180cdc9517 --- Lib/test/support/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 591e05a3c50822..92e3174407f133 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1893,12 +1893,19 @@ def restore(self): def with_pymalloc(): - WITH_PYMALLOC = bool(sysconfig.get_config_var("WITH_PYMALLOC")) - return WITH_PYMALLOC and not Py_GIL_DISABLED + try: + import _testcapi + except ImportError: + raise unittest.SkipTest("requires _testcapi") + return _testcapi.WITH_PYMALLOC and not Py_GIL_DISABLED def with_mimalloc(): - return bool(sysconfig.get_config_var("WITH_MIMALLOC")) + try: + import _testcapi + except ImportError: + raise unittest.SkipTest("requires _testcapi") + return _testcapi.WITH_MIMALLOC class _ALWAYS_EQ: From a01ef4247e8e4ff50cee9237f3f079665b56b760 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 28 Mar 2024 01:54:09 +0100 Subject: [PATCH 6/7] Skip CodegenTestCase, CfgOptimizationTestCase, and AssemblerTestCase if _testinternalcapi is missing --- Lib/test/support/bytecode_helper.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/test/support/bytecode_helper.py b/Lib/test/support/bytecode_helper.py index 32d4ec06609177..7a0e884ccc122a 100644 --- a/Lib/test/support/bytecode_helper.py +++ b/Lib/test/support/bytecode_helper.py @@ -3,7 +3,10 @@ import unittest import dis import io -from test.support import import_helper +try: + import _testinternalcapi +except ImportError: + _testinternalcapi = None _UNSPECIFIED = object() @@ -133,26 +136,26 @@ def complete_insts_info(self, insts): return res +@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") class CodegenTestCase(CompilationStepTestCase): def generate_code(self, ast): - _testinternalcapi = import_helper.import_module("_testinternalcapi") insts, _ = _testinternalcapi.compiler_codegen(ast, "my_file.py", 0) return insts +@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") class CfgOptimizationTestCase(CompilationStepTestCase): def get_optimized(self, insts, consts, nlocals=0): - _testinternalcapi = import_helper.import_module("_testinternalcapi") insts = self.normalize_insts(insts) insts = self.complete_insts_info(insts) insts = _testinternalcapi.optimize_cfg(insts, consts, nlocals) return insts, consts +@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") class AssemblerTestCase(CompilationStepTestCase): def get_code_object(self, filename, insts, metadata): - _testinternalcapi = import_helper.import_module("_testinternalcapi") co = _testinternalcapi.assemble_code_object(filename, insts, metadata) return co From 71968fccb09f52aa2413c40a9154ad88a6c933b5 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 28 Mar 2024 09:10:43 +0100 Subject: [PATCH 7/7] Address review: log TEST_MODULES in test.pythoninfo --- Lib/test/pythoninfo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index c8bf16dd4d0d49..0cfd033bb637a7 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -524,6 +524,7 @@ def collect_sysconfig(info_add): 'Py_GIL_DISABLED', 'SHELL', 'SOABI', + 'TEST_MODULES', 'abs_builddir', 'abs_srcdir', 'prefix',