Skip to content

Commit

Permalink
pythongh-116303: Handle disabled test modules in test.support
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Mar 7, 2024
1 parent 5d0cdfe commit c5fa18b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 16 additions & 4 deletions Lib/test/support/__init__.py
Expand Up @@ -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)


Expand All @@ -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
Expand Down Expand Up @@ -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


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

Expand Down Expand Up @@ -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

0 comments on commit c5fa18b

Please sign in to comment.