Debian Codegraph @

mypyc/test/test_lowering.py

raw
1 """Runner for lowering transform tests."""
2
3 from __future__ import annotations
4
5 import os.path
6
7 from mypy.errors import CompileError
8 from mypy.test.config import test_temp_dir
9 from mypy.test.data import DataDrivenTestCase
10 from mypyc.common import IS_FREE_THREADED, TOP_LEVEL_NAME
11 from mypyc.ir.pprint import format_func
12 from mypyc.options import CompilerOptions
13 from mypyc.test.testutil import (
14 ICODE_GEN_BUILTINS,
15 MypycDataSuite,
16 assert_test_output,
17 build_ir_for_single_file,
18 infer_ir_build_options_from_test_name,
19 remove_comment_lines,
20 replace_word_size,
21 use_custom_builtins,
22 )
23 from mypyc.transform.exceptions import insert_exception_handling
24 from mypyc.transform.flag_elimination import do_flag_elimination
25 from mypyc.transform.lower import lower_ir
26 from mypyc.transform.refcount import insert_ref_count_opcodes
27 from mypyc.transform.uninit import insert_uninit_checks
28
29
30 class TestLowering(MypycDataSuite):
31 files = ["lowering-int.test", "lowering-list.test", "lowering-vec.test"]
32 base_path = test_temp_dir
33
34 def run_case(self, testcase: DataDrivenTestCase) -> None:
35 options = infer_ir_build_options_from_test_name(testcase.name)
36 if options is None:
37 # Skipped test case
38 return
39 if "_withgil" in testcase.name and IS_FREE_THREADED:
40 # Test case should only run on a non-free-threaded build.
41 return
42 if "_nogil" in testcase.name and not IS_FREE_THREADED:
43 # Test case should only run on a free-threaded build.
44 return
45 with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
46 expected_output = remove_comment_lines(testcase.output)
47 expected_output = replace_word_size(expected_output)
48 try:
49 ir = build_ir_for_single_file(testcase.input, options)
50 except CompileError as e:
51 actual = e.messages
52 else:
53 actual = []
54 for fn in ir:
55 if fn.name == TOP_LEVEL_NAME and not testcase.name.endswith("_toplevel"):
56 continue
57 options = CompilerOptions(strict_traceback_checks=True)
58 # Lowering happens after exception handling and ref count opcodes have
59 # been added. Any changes must maintain reference counting semantics.
60 insert_uninit_checks(fn, True)
61 insert_exception_handling(fn, True)
62 insert_ref_count_opcodes(fn)
63 lower_ir(fn, options)
64 do_flag_elimination(fn, options)
65 actual.extend(format_func(fn))
66
67 assert_test_output(testcase, actual, "Invalid source code output", expected_output)