Debian Codegraph @

Lib/test/test_compiler_codegen.py

raw
1
2 import textwrap
3 from test.support.bytecode_helper import CodegenTestCase
4
5 # Tests for the code-generation stage of the compiler.
6 # Examine the un-optimized code generated from the AST.
7
8 class IsolatedCodeGenTests(CodegenTestCase):
9
10 def assertInstructionsMatch_recursive(self, insts, expected_insts):
11 expected_nested = [i for i in expected_insts if isinstance(i, list)]
12 expected_insts = [i for i in expected_insts if not isinstance(i, list)]
13 self.assertInstructionsMatch(insts, expected_insts)
14 self.assertEqual(len(insts.get_nested()), len(expected_nested))
15 for n_insts, n_expected in zip(insts.get_nested(), expected_nested):
16 self.assertInstructionsMatch_recursive(n_insts, n_expected)
17
18 def codegen_test(self, snippet, expected_insts):
19 import ast
20 a = ast.parse(snippet, "my_file.py", "exec")
21 insts = self.generate_code(a)
22 self.assertInstructionsMatch_recursive(insts, expected_insts)
23
24 def test_if_expression(self):
25 snippet = "42 if True else 24"
26 false_lbl = self.Label()
27 expected = [
28 ('RESUME', 0, 0),
29 ('LOAD_CONST', 0, 1),
30 ('TO_BOOL', 0, 1),
31 ('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1),
32 ('LOAD_CONST', 1, 1),
33 ('JUMP_NO_INTERRUPT', exit_lbl := self.Label()),
34 false_lbl,
35 ('LOAD_CONST', 2, 1),
36 exit_lbl,
37 ('POP_TOP', None),
38 ('LOAD_CONST', 3),
39 ('RETURN_VALUE', None),
40 ]
41 self.codegen_test(snippet, expected)
42
43 def test_for_loop(self):
44 snippet = "for x in l:\n\tprint(x)"
45 false_lbl = self.Label()
46 expected = [
47 ('RESUME', 0, 0),
48 ('LOAD_NAME', 0, 1),
49 ('GET_ITER', None, 1),
50 loop_lbl := self.Label(),
51 ('FOR_ITER', exit_lbl := self.Label(), 1),
52 ('NOP', None, 1, 1),
53 ('STORE_NAME', 1, 1),
54 ('LOAD_NAME', 2, 2),
55 ('PUSH_NULL', None, 2),
56 ('LOAD_NAME', 1, 2),
57 ('CALL', 1, 2),
58 ('POP_TOP', None),
59 ('JUMP', loop_lbl),
60 exit_lbl,
61 ('END_FOR', None),
62 ('POP_TOP', None),
63 ('LOAD_CONST', 0),
64 ('RETURN_VALUE', None),
65 ]
66 self.codegen_test(snippet, expected)
67
68 def test_function(self):
69 snippet = textwrap.dedent("""
70 def f(x):
71 return x + 42
72 """)
73 expected = [
74 # Function definition
75 ('RESUME', 0),
76 ('LOAD_CONST', 0),
77 ('MAKE_FUNCTION', None),
78 ('STORE_NAME', 0),
79 ('LOAD_CONST', 1),
80 ('RETURN_VALUE', None),
81 [
82 # Function body
83 ('RESUME', 0),
84 ('LOAD_FAST', 0),
85 ('LOAD_CONST', 1),
86 ('BINARY_OP', 0),
87 ('RETURN_VALUE', None),
88 ('LOAD_CONST', 0),
89 ('RETURN_VALUE', None),
90 ]
91 ]
92 self.codegen_test(snippet, expected)
93
94 def test_nested_functions(self):
95 snippet = textwrap.dedent("""
96 def f():
97 def h():
98 return 12
99 def g():
100 x = 1
101 y = 2
102 z = 3
103 u = 4
104 return 42
105 """)
106 expected = [
107 # Function definition
108 ('RESUME', 0),
109 ('LOAD_CONST', 0),
110 ('MAKE_FUNCTION', None),
111 ('STORE_NAME', 0),
112 ('LOAD_CONST', 1),
113 ('RETURN_VALUE', None),
114 [
115 # Function body
116 ('RESUME', 0),
117 ('LOAD_CONST', 1),
118 ('MAKE_FUNCTION', None),
119 ('STORE_FAST', 0),
120 ('LOAD_CONST', 2),
121 ('MAKE_FUNCTION', None),
122 ('STORE_FAST', 1),
123 ('LOAD_CONST', 0),
124 ('RETURN_VALUE', None),
125 [
126 ('RESUME', 0),
127 ('NOP', None),
128 ('LOAD_CONST', 1),
129 ('RETURN_VALUE', None),
130 ('LOAD_CONST', 0),
131 ('RETURN_VALUE', None),
132 ],
133 [
134 ('RESUME', 0),
135 ('LOAD_CONST', 1),
136 ('STORE_FAST', 0),
137 ('LOAD_CONST', 2),
138 ('STORE_FAST', 1),
139 ('LOAD_CONST', 3),
140 ('STORE_FAST', 2),
141 ('LOAD_CONST', 4),
142 ('STORE_FAST', 3),
143 ('NOP', None),
144 ('LOAD_CONST', 5),
145 ('RETURN_VALUE', None),
146 ('LOAD_CONST', 0),
147 ('RETURN_VALUE', None),
148 ],
149 ],
150 ]
151 self.codegen_test(snippet, expected)
152
153 def test_syntax_error__return_not_in_function(self):
154 snippet = "return 42"
155 with self.assertRaisesRegex(SyntaxError, "'return' outside function") as cm:
156 self.codegen_test(snippet, None)
157 self.assertIsNone(cm.exception.text)
158 self.assertEqual(cm.exception.offset, 1)
159 self.assertEqual(cm.exception.end_offset, 10)