Skip to content

Commit

Permalink
pythongh-116303: Handle disabled test modules in test.support helpers (
Browse files Browse the repository at this point in the history
…python#116482)

Make sure test.support helpers skip iso. failing if test extension
modules are disabled. Also log TEST_MODULES in test.pythoninfo.
  • Loading branch information
erlend-aasland authored and diegorusso committed Apr 17, 2024
1 parent 8bb29e2 commit c28a6fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions Lib/test/pythoninfo.py
Expand Up @@ -524,6 +524,7 @@ def collect_sysconfig(info_add):
'Py_GIL_DISABLED',
'SHELL',
'SOABI',
'TEST_MODULES',
'abs_builddir',
'abs_srcdir',
'prefix',
Expand Down
20 changes: 16 additions & 4 deletions Lib/test/support/__init__.py
Expand Up @@ -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)


Expand All @@ -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
Expand Down Expand Up @@ -1887,12 +1893,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


Expand Down
14 changes: 10 additions & 4 deletions Lib/test/support/bytecode_helper.py
Expand Up @@ -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()

Expand Down Expand Up @@ -133,23 +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):
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

0 comments on commit c28a6fc

Please sign in to comment.