| 1 |
"""Test runner for reference count opcode insertion transform test cases. |
| 2 |
|
| 3 |
The transform inserts needed reference count increment/decrement |
| 4 |
operations to IR. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import os.path |
| 10 |
|
| 11 |
from mypy.errors import CompileError |
| 12 |
from mypy.test.config import test_temp_dir |
| 13 |
from mypy.test.data import DataDrivenTestCase |
| 14 |
from mypyc.common import IS_FREE_THREADED, TOP_LEVEL_NAME |
| 15 |
from mypyc.ir.pprint import format_func |
| 16 |
from mypyc.test.testutil import ( |
| 17 |
ICODE_GEN_BUILTINS, |
| 18 |
MypycDataSuite, |
| 19 |
assert_test_output, |
| 20 |
build_ir_for_single_file, |
| 21 |
infer_ir_build_options_from_test_name, |
| 22 |
remove_comment_lines, |
| 23 |
replace_word_size, |
| 24 |
use_custom_builtins, |
| 25 |
) |
| 26 |
from mypyc.transform.refcount import insert_ref_count_opcodes |
| 27 |
from mypyc.transform.uninit import insert_uninit_checks |
| 28 |
|
| 29 |
files = ["refcount.test"] |
| 30 |
|
| 31 |
|
| 32 |
class TestRefCountTransform(MypycDataSuite): |
| 33 |
files = files |
| 34 |
base_path = test_temp_dir |
| 35 |
optional_out = True |
| 36 |
|
| 37 |
def run_case(self, testcase: DataDrivenTestCase) -> None: |
| 38 |
"""Perform a runtime checking transformation test case.""" |
| 39 |
options = infer_ir_build_options_from_test_name(testcase.name) |
| 40 |
if options is None: |
| 41 |
# Skipped test case |
| 42 |
return |
| 43 |
if "_withgil" in testcase.name and IS_FREE_THREADED: |
| 44 |
# Test case should only run on a non-free-threaded build. |
| 45 |
return |
| 46 |
if "_nogil" in testcase.name and not IS_FREE_THREADED: |
| 47 |
# Test case should only run on a free-threaded build. |
| 48 |
return |
| 49 |
with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase): |
| 50 |
expected_output = remove_comment_lines(testcase.output) |
| 51 |
expected_output = replace_word_size(expected_output) |
| 52 |
try: |
| 53 |
ir = build_ir_for_single_file(testcase.input, options) |
| 54 |
except CompileError as e: |
| 55 |
actual = e.messages |
| 56 |
else: |
| 57 |
actual = [] |
| 58 |
for fn in ir: |
| 59 |
if fn.name == TOP_LEVEL_NAME and not testcase.name.endswith("_toplevel"): |
| 60 |
continue |
| 61 |
insert_uninit_checks(fn, True) |
| 62 |
insert_ref_count_opcodes(fn) |
| 63 |
actual.extend(format_func(fn)) |
| 64 |
|
| 65 |
assert_test_output(testcase, actual, "Invalid source code output", expected_output) |