8000 gh-105775: Convert LOAD_CLOSURE to a pseudo-op by polynomialherder · Pull Request #106059 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105775: Convert LOAD_CLOSURE to a pseudo-op #106059

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

Merged
merged 14 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gh-105775: Add unit test, correct NEWS/docs
This change adds a unit test for assembly
of code objects containing the LOAD_CLOSURE
pseud-instruction

We also correct the documentation and NEWS
descriptions to more accurately reflect the
effects of converting LOAD_CLOSURE to a pseudo-op
and the reason for its existence in the first place
  • Loading branch information
polynomialherder committed Jun 27, 2023
commit 93b3138aaaf3ed844814917c027a6f53731b3900
1 change: 0 additions & 1 deletion Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,6 @@ but are replaced by real opcodes or removed before bytecode is generated.
storage.

Note that ``LOAD_CLOSURE`` is replaced with ``LOAD_FAST`` in the assembler.
It exists to keep bytecode a little more readable.

.. versionchanged:: 3.13
This opcode was demoted from full opcode to pseudo-instruction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This opcode was demoted from full opcode to pseudo-instruction
This opcode is now a pseudo-instruction.

Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_compiler_assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,40 @@ def test_simple_expr(self):
]
expected = {(3, 4) : 3.5, (-100, 200) : 50, (10, 18) : 14}
self.assemble_test(insts, metadata, expected)


def test_expression_with_pseudo_instruction_load_closure(self):

def mod_two(x):
def inner():
return x
return inner() % 2

inner_code = mod_two.__code__.co_consts[1]

metadata = {
'filename' : 'mod_two.py',
'name' : 'mod_two',
'qualname' : 'nested.mod_two',
'cellvars' : {'x' : 0},
'consts': {None: 0, inner_code: 1, 2: 2},
'argcount' : 1,
'varnames' : {'x' : 0},
}

instructions = [
('RESUME', 0,),
('PUSH_NULL', 0, 1),
('LOAD_CLOSURE', 0, 1),
('BUILD_TUPLE', 1, 1),
('LOAD_CONST', 1, 1),
('MAKE_FUNCTION', 0, 2),
('SET_FUNCTION_ATTRIBUTE', 8, 2),
('CALL', 0, 2), # (lambda: x)()
('LOAD_CONST', 2, 2), # 2
('BINARY_OP', 6, 2), # %
('RETURN_VALUE', 0, 2)
]

expected = {(0,): 0, (1,): 1, (2,): 0, (120,): 0, (121,): 1}
self.assemble_test(instructions, metadata, expected)
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
Remove the LOAD_CLOSURE opcode and make it a pseudo-op instead. This enables

* Superinstruction formation
* Removal of checks for uninitialized variables
Remove the LOAD_CLOSURE opcode and make it a pseudo-op instead.
0