Skip to content

Commit 9d8fa7e

Browse files
Test that GC frame is not cleared
1 parent 024b6bc commit 9d8fa7e

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lib/test/test_gc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,24 @@ def test_heap_size(self):
12891289
del l
12901290
self.assertEqual(count, _testinternalcapi.get_tracked_heap_size())
12911291

1292+
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
1293+
def test_clear_frame_on_early_return(self):
1294+
# __del__ methods can trigger collection, make this to happen
1295+
thresholds = gc.get_threshold()
1296+
gc.enable()
1297+
gc.set_threshold(1)
1298+
1299+
class A:
1300+
def __del__(self):
1301+
dir(self)
1302+
1303+
x = [A() for _ in range(10)]
1304+
del x
1305+
self.assertTrue(_testinternalcapi.is_gc_frame_clear())
1306+
1307+
gc.disable()
1308+
gc.set_threshold(*thresholds)
1309+
12921310

12931311
class GCCallbackTests(unittest.TestCase):
12941312
def setUp(self):

Modules/_testinternalcapi.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,19 @@ test_thread_state_ensure_from_view_interp_switch(PyObject *self, PyObject *unuse
32063206
Py_RETURN_NONE;
32073207
}
32083208

3209+
static PyObject *
3210+
is_gc_frame_clear(PyObject *self, PyObject *unused)
3211+
{
3212+
PyInterpreterState *interp = _PyInterpreterState_GET();
3213+
assert(interp != NULL);
3214+
3215+
if (!interp->gc.frame) {
3216+
Py_RETURN_TRUE;
3217+
}
3218+
3219+
Py_RETURN_FALSE;
3220+
}
3221+
32093222
/* Self interrupting context manager */
32103223

32113224
typedef struct {
@@ -3393,6 +3406,7 @@ static PyMethodDef module_functions[] = {
33933406
{"test_interp_guard_countdown", test_interp_guard_countdown, METH_NOARGS},
33943407
{"test_interp_view_countdown", test_interp_view_countdown, METH_NOARGS},
33953408
{"test_thread_state_ensure_from_view_interp_switch", test_thread_state_ensure_from_view_interp_switch, METH_NOARGS},
3409+
{"is_gc_frame_clear", is_gc_frame_clear, METH_NOARGS},
33963410
{NULL, NULL} /* sentinel */
33973411
};
33983412

0 commit comments

Comments
 (0)