This commit is contained in:
blueloveTH 2025-02-28 17:04:30 +08:00
parent f0b3cb102e
commit 7c1174f816
2 changed files with 3 additions and 5 deletions

View File

@ -508,14 +508,14 @@ void py_newglobals(py_Ref out) {
void py_newlocals(py_Ref out) { void py_newlocals(py_Ref out) {
Frame* frame = pk_current_vm->top_frame; Frame* frame = pk_current_vm->top_frame;
if(!frame) { if(!frame) {
py_newglobals(out); py_newdict(out);
return; return;
} }
if(frame->is_locals_special) { if(frame->is_locals_special) {
switch(frame->locals->type) { switch(frame->locals->type) {
case tp_locals: frame = frame->locals->_ptr; break; case tp_locals: frame = frame->locals->_ptr; break;
case tp_dict: *out = *frame->locals; return; case tp_dict: *out = *frame->locals; return;
case tp_nil: py_newglobals(out); return; case tp_nil: py_newdict(out); return;
default: c11__unreachable(); default: c11__unreachable();
} }
} }

View File

@ -6,7 +6,6 @@ my_locals = {"b": 2}
# With user-defined locals: # With user-defined locals:
exec(""" exec("""
import sys import sys
assert locals() != globals()
assert "sys" in locals() assert "sys" in locals()
assert "sys" not in globals() assert "sys" not in globals()
assert "a" not in locals() assert "a" not in locals()
@ -18,7 +17,6 @@ assert "b" not in globals()
# print(b) # print(b)
assert (b == 2), b assert (b == 2), b
def main(): def main():
assert locals() != globals()
assert "sys" not in locals() # not the same `locals()` as the outer scope assert "sys" not in locals() # not the same `locals()` as the outer scope
assert "sys" not in globals() # and `sys` isn't in `globals()`, same as before assert "sys" not in globals() # and `sys` isn't in `globals()`, same as before
assert "b" not in locals() # again, not the same `locals()` as the outer scope assert "b" not in locals() # again, not the same `locals()` as the outer scope
@ -32,7 +30,7 @@ assert "sys" not in globals()
# With default locals: # With default locals:
exec(""" exec("""
import sys import sys
assert "sys" in locals() assert locals() == {}
assert "sys" in globals() assert "sys" in globals()
def main(): def main():
assert "sys" not in locals() # not the same locals as the outer scope assert "sys" not in locals() # not the same locals as the outer scope