Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 41 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,47 @@ def test_as_dict(self):
with self.assertRaises(KeyError):
d['non_exist']

def test_values_reference_ownership(self):
class Value:
pass

for duplicate_names in (False, True):
with self.subTest(duplicate_names=duplicate_names):
def make_frame(first, second):
return sys._getframe()

if duplicate_names:
make_frame.__code__ = make_frame.__code__.replace(
co_varnames=('value', 'value'))

first = Value()
second = Value()
extra = Value()
refs = [weakref.ref(value) for value in (first, second, extra)]
frame = make_frame(first, second)
proxy = frame.f_locals
proxy['extra'] = extra
values = proxy.values()
if duplicate_names:
self.assertEqual(values, [first, extra])
else:
self.assertEqual(values, [first, second, extra])

frame.clear()
del first, second, extra
support.gc_collect()
self.assertIsNotNone(refs[0]())
self.assertIsNotNone(refs[2]())
if duplicate_names:
self.assertIsNone(refs[1]())
else:
self.assertIsNotNone(refs[1]())

values.clear()
support.gc_collect()
for ref in refs:
self.assertIsNone(ref())

def test_as_number(self):
x = 1
y = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid redundant reference count updates in the ``values()`` method of the
:attr:`frame.f_locals` proxy for optimized scopes.
11 changes: 6 additions & 5 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,15 @@ framelocalsproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored))
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
int first = framelocalsproxy_is_first_occurrence(seen, name);
if (first == 1) {
if (PyList_Append(values, value) < 0) {
Py_DECREF(value);
if (_PyList_AppendTakeRef((PyListObject *)values, value) < 0) {
goto error;
}
}
Py_DECREF(value);
if (first < 0) {
goto error;
else {
Py_DECREF(value);
if (first < 0) {
goto error;
}
}
}
}
Expand Down
Loading