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 ('ANNOTATIONS_PLACEHOLDER', None),
30 ('LOAD_CONST', 0, 1),
31 ('TO_BOOL', 0, 1),
32 ('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1),
33 ('LOAD_CONST', 1, 1), # 42
34 ('JUMP_NO_INTERRUPT', exit_lbl := self.Label()),
35 false_lbl,
36 ('LOAD_CONST', 2, 1), # 24
37 exit_lbl,
38 ('POP_TOP', None),
39 ('LOAD_CONST', 1),
40 ('RETURN_VALUE', None),
41 ]
42 self.codegen_test(snippet, expected)
43
44 def test_for_loop(self):
45 snippet = "for x in l:\n\tprint(x)"
46 false_lbl = self.Label()
47 expected = [
48 ('RESUME', 0, 0),
49 ('ANNOTATIONS_PLACEHOLDER', None),
50 ('LOAD_NAME', 0, 1),
51 ('GET_ITER', None, 1),
52 loop_lbl := self.Label(),
53 ('FOR_ITER', exit_lbl := self.Label(), 1),
54 ('NOP', None, 1, 1),
55 ('STORE_NAME', 1, 1),
56 ('LOAD_NAME', 2, 2),
57 ('PUSH_NULL', None, 2),
58 ('LOAD_NAME', 1, 2),
59 ('CALL', 1, 2),
60 ('POP_TOP', None),
61 ('JUMP', loop_lbl),
62 exit_lbl,
63 ('END_FOR', None),
64 ('POP_ITER', None),
65 ('LOAD_CONST', 0),
66 ('RETURN_VALUE', None),
67 ]
68 self.codegen_test(snippet, expected)
69
70 def test_function(self):
71 snippet = textwrap.dedent("""
72 def f(x):
73 return x + 42
74 """)
75 expected = [
76 # Function definition
77 ('RESUME', 0),
78 ('ANNOTATIONS_PLACEHOLDER', None),
79 ('LOAD_CONST', 0),
80 ('MAKE_FUNCTION', None),
81 ('STORE_NAME', 0),
82 ('LOAD_CONST', 1),
83 ('RETURN_VALUE', None),
84 [
85 # Function body
86 ('RESUME', 0),
87 ('LOAD_FAST', 0),
88 ('LOAD_CONST', 42),
89 ('BINARY_OP', 0),
90 ('RETURN_VALUE', None),
91 ('LOAD_CONST', 0),
92 ('RETURN_VALUE', None),
93 ]
94 ]
95 self.codegen_test(snippet, expected)
96
97 def test_nested_functions(self):
98 snippet = textwrap.dedent("""
99 def f():
100 def h():
101 return 12
102 def g():
103 x = 1
104 y = 2
105 z = 3
106 u = 4
107 return 42
108 """)
109 expected = [
110 # Function definition
111 ('RESUME', 0),
112 ('ANNOTATIONS_PLACEHOLDER', None),
113 ('LOAD_CONST', 0),
114 ('MAKE_FUNCTION', None),
115 ('STORE_NAME', 0),
116 ('LOAD_CONST', 1),
117 ('RETURN_VALUE', None),
118 [
119 # Function body
120 ('RESUME', 0),
121 ('LOAD_CONST', 1),
122 ('MAKE_FUNCTION', None),
123 ('STORE_FAST', 0),
124 ('LOAD_CONST', 2),
125 ('MAKE_FUNCTION', None),
126 ('STORE_FAST', 1),
127 ('LOAD_CONST', 0),
128 ('RETURN_VALUE', None),
129 [
130 ('RESUME', 0),
131 ('NOP', None),
132 ('LOAD_CONST', 12),
133 ('RETURN_VALUE', None),
134 ('LOAD_CONST', 1),
135 ('RETURN_VALUE', None),
136 ],
137 [
138 ('RESUME', 0),
139 ('LOAD_CONST', 1),
140 ('STORE_FAST', 0),
141 ('LOAD_CONST', 2),
142 ('STORE_FAST', 1),
143 ('LOAD_CONST', 3),
144 ('STORE_FAST', 2),
145 ('LOAD_CONST', 4),
146 ('STORE_FAST', 3),
147 ('NOP', None),
148 ('LOAD_CONST', 42),
149 ('RETURN_VALUE', None),
150 ('LOAD_CONST', 0),
151 ('RETURN_VALUE', None),
152 ],
153 ],
154 ]
155 self.codegen_test(snippet, expected)
156
157 def test_syntax_error__return_not_in_function(self):
158 snippet = "return 42"
159 with self.assertRaisesRegex(SyntaxError, "'return' outside function") as cm:
160 self.codegen_test(snippet, None)
161 self.assertIsNone(cm.exception.text)
162 self.assertEqual(cm.exception.offset, 1)
163 self.assertEqual(cm.exception.end_offset, 10)