-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-87092: expose the compiler's codegen to python for unit tests #99111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d5f14fc
89b8357
ddac06a
b07b9c6
a2a1c17
3c1c647
9bafd3a
ae1e146
ce45de8
d98f150
00aea64
50c0cc2
61edee2
ab3f67c
4453129
ad1ef54
4f51307
3657eca
9cf20ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
from test.support.bytecode_helper import CodegenTestCase | ||
|
||
# Tests for the code-generation stage of the compiler. | ||
# Examine the un-optimized code generated from the AST. | ||
|
||
class IsolatedCodeGenTests(CodegenTestCase): | ||
|
||
def codegen_test(self, snippet, expected_insts): | ||
import ast | ||
a = ast.parse(snippet, "my_file.py", "exec"); | ||
insts = self.generate_code(a) | ||
self.assertInstructionsMatch(insts, expected_insts) | ||
|
||
def test_if_expression(self): | ||
snippet = "42 if True else 24" | ||
false_lbl = self.Label() | ||
expected = [ | ||
('RESUME', 0, 0), | ||
('LOAD_CONST', 0, 1), | ||
('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1), | ||
('LOAD_CONST', 1, 1), | ||
('JUMP', exit_lbl := self.Label()), | ||
false_lbl, | ||
('LOAD_CONST', 2, 1), | ||
exit_lbl, | ||
('POP_TOP', None), | ||
] | ||
self.codegen_test(snippet, expected) | ||
|
||
def test_for_loop(self): | ||
snippet = "for x in l:\n\tprint(x)" | ||
false_lbl = self.Label() | ||
expected = [ | ||
('RESUME', 0, 0), | ||
('LOAD_NAME', 0, 1), | ||
('GET_ITER', None, 1), | ||
loop_lbl := self.Label(), | ||
('FOR_ITER', exit_lbl := self.Label(), 1), | ||
('STORE_NAME', None, 1), | ||
('PUSH_NULL', None, 2), | ||
('LOAD_NAME', None, 2), | ||
('LOAD_NAME', None, 2), | ||
('CALL', None, 2), | ||
('POP_TOP', None), | ||
('JUMP', loop_lbl), | ||
exit_lbl, | ||
('END_FOR', None), | ||
] | ||
self.codegen_test(snippet, expected) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this (or the other functions) need to be an API function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can _testinternalcapi import them otherwise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, that why it has "internal" in the name.
It defines
Py_BUILD_CORE_BUILTIN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem to work:
[ERROR] _testinternalcapi failed to import: dlopen(/Users/iritkatriel/src/cpython-654/build/lib.macosx-13.0-x86_64-3.12-pydebug/_testinternalcapi.cpython-312d-darwin.so, 0x0002): symbol not found in flat namespace '__PyCompile_CodeGen'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge this PR now, it's getting into merge conflicts all the time. We can remove these from the API later if it's possible.