8000 Do not clear globals or builtins when calling clear() on a frame obje… · python/cpython@ba2f32a · GitHub
[go: up one dir, main page]

Skip to content

Commit ba2f32a

Browse files
authored
Do not clear globals or builtins when calling clear() on a frame object. Reverts behavior to that of 3.10 and earlier. (GH-26768)
1 parent 00710e6 commit ba2f32a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Lib/test/test_frame.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ class C:
4545
# The reference was released by .clear()
4646
self.assertIs(None, wr())
4747

48+
def test_clear_does_not_clear_specials(self):
49+
class C:
50+
pass
51+
c = C()
52+
exc = self.outer(c=c)
53+
del c
54+
f = exc.__traceback__.tb_frame
55+
f.clear()
56+
self.assertIsNot(f.f_code, None)
57+
self.assertIsNot(f.f_locals, None)
58+
self.assertIsNot(f.f_builtins, None)
59+
self.assertIsNot(f.f_globals, None)
60+
4861
def test_clear_generator(self):
4962
endly = False
5063
def g():

Objects/frameobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ frame_dealloc(PyFrameObject *f)
612612
Py_TRASHCAN_SAFE_BEGIN(f)
613613
PyCodeObject *co = f->f_code;
614614

615-
/* Kill all local variables */
615+
/* Kill all local variables including specials. */
616616
if (f->f_localsptr) {
617617
for (int i = 0; i < co->co_nlocalsplus+FRAME_SPECIALS_SIZE; i++) {
618618
Py_CLEAR(f->f_localsptr[i]);
@@ -683,11 +683,10 @@ frame_tp_clear(PyFrameObject *f)
683683
f->f_state = FRAME_CLEARED;
684684

685685
Py_CLEAR(f->f_trace);
686-
686+
PyCodeObject *co = f->f_code;
687687
/* locals */
688-
PyObject **localsplus = f->f_localsptr;
689-
for (Py_ssize_t i = frame_nslots(f); --i >= 0; ++localsplus) {
690-
Py_CLEAR(*localsplus);
688+
for (int i = 0; i < co->co_nlocalsplus; i++) {
689+
Py_CLEAR(f->f_localsptr[i]);
691690
}
692691

693692
/* stack */

0 commit comments

Comments
 (0)
0