Skip to content

Commit fd32bc2

Browse files
committed
Add RESTORE_CELL to opcode metadata, fix locals() for inlined comprehension cells
Register RESTORE_CELL (opcode 121) in _opcode_metadata.py so test__opcode's stack_effect check passes. In locals(), skip cell values when the same name has a set fastlocal value. This fixes test_closure_with_inline_comprehension where a cell variable used as an inlined comprehension iteration target should show the comprehension's value, not the outer cell value.
1 parent d912e41 commit fd32bc2

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Lib/_opcode_metadata.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/vm/src/frame.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,19 @@ impl Frame {
906906
}
907907
}
908908
if !code.cellvars.is_empty() || !code.freevars.is_empty() {
909+
let fastlocals = unsafe { (*self.iframe.get()).localsplus.fastlocals() };
909910
for (i, &k) in code.cellvars.iter().enumerate() {
911+
// When a variable appears in both varnames and cellvars
912+
// (inlined comprehension with scope tweak), the fastlocal
913+
// value takes precedence, matching CPython FrameLocalsProxy.
914+
let has_fastlocal = code
915+
.varnames
916+
.iter()
917+
.position(|&v| v == k)
918+
.is_some_and(|idx| fastlocals.get(idx).is_some_and(|v| v.is_some()));
919+
if has_fastlocal {
920+
continue;
921+
}
910922
let cell_value = self.get_cell_contents(i);
911923
match locals_map.ass_subscript(k, cell_value, vm) {
912924
Ok(()) => {}

0 commit comments

Comments
 (0)