FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-102960: Make frames weak-referenceable by ambv · Pull Request #102962 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions Include/internal/pycore_frame.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL