Skip to content

[3.12] gh-130809: Fix PyFrame_LocalsToFast copying the wrong value #130816

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 10 commits into from
Mar 11, 2025
17 changes: 17 additions & 0 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,23 @@ def test_multiple_comprehension_name_reuse(self):
self._check_in_scopes(code, {"x": 2, "y": [3]}, ns={"x": 3}, scopes=["class"])
self._check_in_scopes(code, {"x": 2, "y": [2]}, ns={"x": 3}, scopes=["function", "module"])

def test_name_collision_locals(self):
# GH-130809: The existence of a hidden fast from list comprehension
# should not cause frame.f_locals on module level to return a new dict
# every time it is accessed.

code = """
import sys
frame = sys._getframe()
f_locals = frame.f_locals
foo = 1
[foo for foo in [0]]
# calls _PyFrame_LocalsToFast which triggers the issue
from abc import *
same_f_locals = frame.f_locals is f_locals
"""
self._check_in_scopes(code, {"foo": 1, "same_f_locals": True}, scopes=["module"])

def test_exception_locations(self):
# The location of an exception raised from __init__ or
# __next__ should should be the iterator expression
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue where ``_PyFrame_LocalsToFast`` tries to write module level
values to hidden fasts.
3 changes: 3 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,9 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
continue;
}
if (kind & CO_FAST_HIDDEN) {
continue;
}
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
PyObject *value = PyObject_GetItem(locals, name);
/* We only care about NULLs if clear is true. */
Expand Down
Loading