]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-116303: Handle disabled test modules in test.support helpers (#116482)
authorErlend E. Aasland <erlend@python.org>
Thu, 28 Mar 2024 08:40:37 +0000 (09:40 +0100)
committerGitHub <noreply@github.com>
Thu, 28 Mar 2024 08:40:37 +0000 (08:40 +0000)
Make sure test.support helpers skip iso. failing if test extension
modules are disabled. Also log TEST_MODULES in test.pythoninfo.

Lib/test/pythoninfo.py
Lib/test/support/__init__.py
Lib/test/support/bytecode_helper.py

index c8bf16dd4d0d49e9d840b8c85b553c9c653042f4..0cfd033bb637a7e47301fcc25b4b7cbea6f86796 100644 (file)
@@ -524,6 +524,7 @@ def collect_sysconfig(info_add):
         'Py_GIL_DISABLED',
         'SHELL',
         'SOABI',
+        'TEST_MODULES',
         'abs_builddir',
         'abs_srcdir',
         'prefix',
index 3d7868768231f504ea943dc2ae199f3760d0912e..92e3174407f133df2eb4e8dbc2a170f9458ee45f 100644 (file)
@@ -1715,7 +1715,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)
 
 
@@ -1725,7 +1728,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
@@ -1887,12 +1893,18 @@ class SaveSignals:
 
 
 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
 
 
index a4845065a5322e37b686fbb427f6d507d3df2a89..7a0e884ccc122aef0994c1a1ed3c20e71ba9d5f0 100644 (file)
@@ -3,7 +3,10 @@
 import unittest
 import dis
 import io
-from _testinternalcapi import compiler_codegen, optimize_cfg, assemble_code_object
+try:
+    import _testinternalcapi
+except ImportError:
+    _testinternalcapi = None
 
 _UNSPECIFIED = object()
 
@@ -133,23 +136,26 @@ class CompilationStepTestCase(unittest.TestCase):
         return res
 
 
+@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
 class CodegenTestCase(CompilationStepTestCase):
 
     def generate_code(self, ast):
-        insts, _ = compiler_codegen(ast, "my_file.py", 0)
+        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):
         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
 
+@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
 class AssemblerTestCase(CompilationStepTestCase):
 
     def get_code_object(self, filename, insts, metadata):
-        co = assemble_code_object(filename, insts, metadata)
+        co = _testinternalcapi.assemble_code_object(filename, insts, metadata)
         return co