Skip to content

gh-102960: Make frames weak-referenceable #102962

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct _frame {
char f_trace_lines; /* Emit per-line trace events? */
char f_trace_opcodes; /* Emit per-opcode trace events? */
char f_fast_as_locals; /* Have the fast locals of this frame been converted to a dict? */
PyObject* f_weakreflist; /* Weak ref support */
/* The frame data, if this frame object owns the frame */
PyObject *_f_frame_data[1];
};
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ def test_f_lineno_del_segfault(self):
with self.assertRaises(AttributeError):
del f.f_lineno

def test_weakref_support(self):
wkd = weakref.WeakKeyDictionary()
def _test():
f, outer, inner = self.make_frames()
for frame in (f, outer, inner):
wkd[frame] = True
for frame in (f, outer, inner):
self.assertEqual(wkd[frame], True)
_test()
gc.collect()
self.assertEqual(len(wkd), 0)


class ReprTest(unittest.TestCase):
"""
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ class C(object): pass
def func():
return sys._getframe()
x = func()
check(x, size('3Pi3c7P2ic??2P'))
check(x, size('3Pi3c7P2ic??P2P'))
# function
def func(): pass
check(func, size('14Pi'))
Expand Down
7 changes: 6 additions & 1 deletion Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ frame_dealloc(PyFrameObject *f)
_PyObject_GC_UNTRACK(f);
}

if (f->f_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *)f);
}

Py_TRASHCAN_BEGIN(f, frame_dealloc);
PyCodeObject *co = NULL;

Expand Down Expand Up @@ -997,7 +1001,7 @@ PyTypeObject PyFrame_Type = {
(traverseproc)frame_traverse, /* tp_traverse */
(inquiry)frame_tp_clear, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
OFF(f_weakreflist), /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
frame_methods, /* tp_methods */
Expand Down Expand Up @@ -1031,6 +1035,7 @@ _PyFrame_New_NoTrack(PyCodeObject *code)
f->f_trace_opcodes = 0;
f->f_fast_as_locals = 0;
f->f_lineno = 0;
f->f_weakreflist = NULL;
return f;
}

Expand Down