Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d5f14fc89b8357ddac06ab07b9c6a2a1c173c1c6479bafd3aae1e146ce45de8d98f15000aea6450c0cc261edee2ab3f67c4453129ad1ef544f513073657eca9cf20efFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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_BUILTINThere 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.