|
1 | 1 |
|
| 2 | +import textwrap |
2 | 3 | from test.support.bytecode_helper import CodegenTestCase
|
3 | 4 |
|
4 | 5 | # Tests for the code-generation stage of the compiler.
|
5 | 6 | # Examine the un-optimized code generated from the AST.
|
6 | 7 |
|
7 | 8 | class IsolatedCodeGenTests(CodegenTestCase):
|
8 | 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 | + |
9 | 18 | def codegen_test(self, snippet, expected_insts):
|
10 | 19 | import ast
|
11 | 20 | a = ast.parse(snippet, "my_file.py", "exec")
|
12 | 21 | insts = self.generate_code(a)
|
13 |
| - self.assertInstructionsMatch(insts, expected_insts) |
| 22 | + self.assertInstructionsMatch_recursive(insts, expected_insts) |
14 | 23 |
|
15 | 24 | def test_if_expression(self):
|
16 | 25 | snippet = "42 if True else 24"
|
@@ -55,6 +64,91 @@ def test_for_loop(self):
|
55 | 64 | ]
|
56 | 65 | self.codegen_test(snippet, expected)
|
57 | 66 |
|
| 67 | + def test_function(self): |
| 68 | + snippet = textwrap.dedent(""" |
| 69 | + def f(x): |
| 70 | + return x + 42 |
| 71 | + """) |
| 72 | + expected = [ |
| 73 | + # Function definition |
| 74 | + ('RESUME', 0), |
| 75 | + ('LOAD_CONST', 0), |
| 76 | + ('MAKE_FUNCTION', None), |
| 77 | + ('STORE_NAME', 0), |
| 78 | + ('LOAD_CONST', 1), |
| 79 | + ('RETURN_VALUE', None), |
| 80 | + [ |
| 81 | + # Function body |
| 82 | + ('RESUME', 0), |
| 83 | + ('LOAD_FAST', 0), |
| 84 | + ('LOAD_CONST', 1), |
| 85 | + ('BINARY_OP', 0), |
| 86 | + ('RETURN_VALUE', None), |
| 87 | + ('LOAD_CONST', 0), |
| 88 | + ('RETURN_VALUE', None), |
| 89 | + ] |
| 90 | + ] |
| 91 | + self.codegen_test(snippet, expected) |
| 92 | + |
| 93 | + def test_nested_functions(self): |
| 94 | + snippet = textwrap.dedent(""" |
| 95 | + def f(): |
| 96 | + def h(): |
| 97 | + return 12 |
| 98 | + def g(): |
| 99 | + x = 1 |
| 100 | + y = 2 |
| 101 | + z = 3 |
| 102 | + u = 4 |
| 103 | + return 42 |
| 104 | + """) |
| 105 | + expected = [ |
| 106 | + # Function definition |
| 107 | + ('RESUME', 0), |
| 108 | + ('LOAD_CONST', 0), |
| 109 | + ('MAKE_FUNCTION', None), |
| 110 | + ('STORE_NAME', 0), |
| 111 | + ('LOAD_CONST', 1), |
| 112 | + ('RETURN_VALUE', None), |
| 113 | + [ |
| 114 | + # Function body |
| 115 | + ('RESUME', 0), |
| 116 | + ('LOAD_CONST', 1), |
| 117 | + ('MAKE_FUNCTION', None), |
| 118 | + ('STORE_FAST', 0), |
| 119 | + ('LOAD_CONST', 2), |
| 120 | + ('MAKE_FUNCTION', None), |
| 121 | + ('STORE_FAST', 1), |
| 122 | + ('LOAD_CONST', 0), |
| 123 | + ('RETURN_VALUE', None), |
| 124 | + [ |
| 125 | + ('RESUME', 0), |
| 126 | + ('NOP', None), |
| 127 | + ('LOAD_CONST', 1), |
| 128 | + ('RETURN_VALUE', None), |
| 129 | + ('LOAD_CONST', 0), |
| 130 | + ('RETURN_VALUE', None), |
| 131 | + ], |
| 132 | + [ |
| 133 | + ('RESUME', 0), |
| 134 | + ('LOAD_CONST', 1), |
| 135 | + ('STORE_FAST', 0), |
| 136 | + ('LOAD_CONST', 2), |
| 137 | + ('STORE_FAST', 1), |
| 138 | + ('LOAD_CONST', 3), |
| 139 | + ('STORE_FAST', 2), |
| 140 | + ('LOAD_CONST', 4), |
| 141 | + ('STORE_FAST', 3), |
| 142 | + ('NOP', None), |
| 143 | + ('LOAD_CONST', 5), |
| 144 | + ('RETURN_VALUE', None), |
| 145 | + ('LOAD_CONST', 0), |
| 146 | + ('RETURN_VALUE', None), |
| 147 | + ], |
| 148 | + ], |
| 149 | + ] |
| 150 | + self.codegen_test(snippet, expected) |
| 151 | + |
58 | 152 | def test_syntax_error__return_not_in_function(self):
|
59 | 153 | snippet = "return 42"
|
60 | 154 | with self.assertRaisesRegex(SyntaxError, "'return' outside function"):
|
|
0 commit comments