Skip to content

Commit 64c20ff

Browse files
authored
gh-148722: Make code objects support cyclic GC (#151155)
1 parent 2424c1d commit 64c20ff

4 files changed

Lines changed: 19 additions & 22 deletions

File tree

‎Lib/test/test_code.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,19 @@ def callback(code):
12871287
self.assertFalse(bool(coderef()))
12881288
self.assertTrue(self.called)
12891289

1290+
def test_code_const_reference_cycle_collected(self):
1291+
def f():
1292+
return 1234.5
1293+
1294+
a = []
1295+
code = f.__code__.replace(co_consts=f.__code__.co_consts + (a,))
1296+
coderef = weakref.ref(code)
1297+
a.append(code)
1298+
1299+
del code, a
1300+
gc_collect()
1301+
self.assertIsNone(coderef())
1302+
12901303
# Python implementation of location table parsing algorithm
12911304
def read(it):
12921305
return next(it)

‎Lib/test/test_marshal.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,7 @@ def f():
395395
code = f.__code__
396396
a = []
397397
code = code.replace(co_consts=code.co_consts + (a,))
398-
# This test creates a reference loop which leads to reference leaks,
399-
# so we need to break the loop manually. See gh-148722.
398+
# Break the cycle without relying on cyclic GC after the test.
400399
self.addCleanup(a.clear)
401400
a.append(code)
402401
self.check_reference_loop(code, 'code', 3)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a reference leak when a code object's constants contain a reference cycle
2+
back to the code object.

‎Objects/codeobject.c‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,7 @@ _PyCode_New(struct _PyCodeConstructor *con)
735735
}
736736

737737
Py_ssize_t size = PyBytes_GET_SIZE(con->code) / sizeof(_Py_CODEUNIT);
738-
PyCodeObject *co;
739-
#ifdef Py_GIL_DISABLED
740-
co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size);
741-
#else
742-
co = PyObject_NewVar(PyCodeObject, &PyCode_Type, size);
743-
#endif
738+
PyCodeObject *co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size);
744739
if (co == NULL) {
745740
Py_XDECREF(replacement_locations);
746741
PyErr_NoMemory();
@@ -759,8 +754,8 @@ _PyCode_New(struct _PyCodeConstructor *con)
759754

760755
#ifdef Py_GIL_DISABLED
761756
co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co);
762-
_PyObject_GC_TRACK(co);
763757
#endif
758+
_PyObject_GC_TRACK(co);
764759
Py_XDECREF(replacement_locations);
765760
return co;
766761
}
@@ -2411,9 +2406,7 @@ code_dealloc(PyObject *self)
24112406
return;
24122407
}
24132408

2414-
#ifdef Py_GIL_DISABLED
24152409
PyObject_GC_UnTrack(co);
2416-
#endif
24172410

24182411
_PyFunction_ClearCodeByVersion(co->co_version);
24192412
if (co->co_extra != NULL) {
@@ -2470,18 +2463,16 @@ code_dealloc(PyObject *self)
24702463
PyMem_Free(co->co_tlbc);
24712464
}
24722465
#endif
2473-
PyObject_Free(co);
2466+
PyObject_GC_Del(co);
24742467
}
24752468

2476-
#ifdef Py_GIL_DISABLED
24772469
static int
24782470
code_traverse(PyObject *self, visitproc visit, void *arg)
24792471
{
24802472
PyCodeObject *co = _PyCodeObject_CAST(self);
24812473
Py_VISIT(co->co_consts);
24822474
return 0;
24832475
}
2484-
#endif
24852476

24862477
static PyObject *
24872478
code_repr(PyObject *self)
@@ -2901,17 +2892,9 @@ PyTypeObject PyCode_Type = {
29012892
PyObject_GenericGetAttr, /* tp_getattro */
29022893
0, /* tp_setattro */
29032894
0, /* tp_as_buffer */
2904-
#ifdef Py_GIL_DISABLED
29052895
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2906-
#else
2907-
Py_TPFLAGS_DEFAULT, /* tp_flags */
2908-
#endif
29092896
code_new__doc__, /* tp_doc */
2910-
#ifdef Py_GIL_DISABLED
29112897
code_traverse, /* tp_traverse */
2912-
#else
2913-
0, /* tp_traverse */
2914-
#endif
29152898
0, /* tp_clear */
29162899
code_richcompare, /* tp_richcompare */
29172900
offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */

0 commit comments

Comments
 (0)