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 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"]
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 with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
40 expected_output = remove_comment_lines(testcase.output)
41 expected_output = replace_word_size(expected_output)
42 try:
43 ir = build_ir_for_single_file(testcase.input, options)
44 except CompileError as e:
45 actual = e.messages
46 else:
47 actual = []
48 for fn in ir:
49 if fn.name == TOP_LEVEL_NAME and not testcase.name.endswith("_toplevel"):
50 continue
51 options = CompilerOptions(strict_traceback_checks=True)
52 # Lowering happens after exception handling and ref count opcodes have
53 # been added. Any changes must maintain reference counting semantics.
54 insert_uninit_checks(fn, True)
55 insert_exception_handling(fn, True)
56 insert_ref_count_opcodes(fn)
57 lower_ir(fn, options)
58 do_flag_elimination(fn, options)
59 actual.extend(format_func(fn))
60
61 assert_test_output(testcase, actual, "Invalid source code output", expected_output)