Skip to content

Commit c13b847

Browse files
authored
bpo-41984: GC track all user classes (GH-22701)
1 parent 302b616 commit c13b847

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

Lib/test/test_finalization.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def __new__(cls, *args, **kwargs):
1616
raise TypeError('requires _testcapi.with_tp_del')
1717
return C
1818

19+
try:
20+
from _testcapi import without_gc
21+
except ImportError:
22+
def without_gc(cls):
23+
class C:
24+
def __new__(cls, *args, **kwargs):
25+
raise TypeError('requires _testcapi.without_gc')
26+
return C
27+
1928
from test import support
2029

2130

@@ -94,9 +103,11 @@ def check_sanity(self):
94103
assert self.id_ == id(self)
95104

96105

106+
@without_gc
97107
class NonGC(NonGCSimpleBase):
98108
__slots__ = ()
99109

110+
@without_gc
100111
class NonGCResurrector(NonGCSimpleBase):
101112
__slots__ = ()
102113

@@ -109,8 +120,14 @@ def side_effect(self):
109120
class Simple(SimpleBase):
110121
pass
111122

112-
class SimpleResurrector(NonGCResurrector, SimpleBase):
113-
pass
123+
# Can't inherit from NonGCResurrector, in case importing without_gc fails.
124+
class SimpleResurrector(SimpleBase):
125+
126+
def side_effect(self):
127+
"""
128+
Resurrect self by storing self in a class-wide list.
129+
"""
130+
self.survivors.append(self)
114131

115132

116133
class TestBase:
@@ -178,6 +195,7 @@ def test_simple_resurrect(self):
178195
self.assert_survivors([])
179196
self.assertIs(wr(), None)
180197

198+
@support.cpython_only
181199
def test_non_gc(self):
182200
with SimpleBase.test():
183201
s = NonGC()
@@ -191,6 +209,7 @@ def test_non_gc(self):
191209
self.assert_del_calls(ids)
192210
self.assert_survivors([])
193211

212+
@support.cpython_only
194213
def test_non_gc_resurrect(self):
195214
with SimpleBase.test():
196215
s = NonGCResurrector()

Lib/test/test_gc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@ class UserIntSlots(int):
582582
self.assertTrue(gc.is_tracked(UserInt()))
583583
self.assertTrue(gc.is_tracked([]))
584584
self.assertTrue(gc.is_tracked(set()))
585-
self.assertFalse(gc.is_tracked(UserClassSlots()))
586-
self.assertFalse(gc.is_tracked(UserFloatSlots()))
587-
self.assertFalse(gc.is_tracked(UserIntSlots()))
585+
self.assertTrue(gc.is_tracked(UserClassSlots()))
586+
self.assertTrue(gc.is_tracked(UserFloatSlots()))
587+
self.assertTrue(gc.is_tracked(UserIntSlots()))
588588

589589
def test_is_finalized(self):
590590
# Objects not tracked by the always gc return false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The garbage collector now tracks all user-defined classes. Patch by Brandt
2+
Bucher.

Modules/_testcapimodule.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,25 @@ with_tp_del(PyObject *self, PyObject *args)
38883888
return obj;
38893889
}
38903890

3891+
static PyObject *
3892+
without_gc(PyObject *Py_UNUSED(self), PyObject *obj)
3893+
{
3894+
PyTypeObject *tp = (PyTypeObject*)obj;
3895+
if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3896+
return PyErr_Format(PyExc_TypeError, "heap type expected, got %R", obj);
3897+
}
3898+
if (PyType_IS_GC(tp)) {
3899+
// Don't try this at home, kids:
3900+
tp->tp_flags -= Py_TPFLAGS_HAVE_GC;
3901+
tp->tp_free = PyObject_Del;
3902+
tp->tp_traverse = NULL;
3903+
tp->tp_clear = NULL;
3904+
}
3905+
assert(!PyType_IS_GC(tp));
3906+
Py_INCREF(obj);
3907+
return obj;
3908+
}
3909+
38913910
static PyMethodDef ml;
38923911

38933912
static PyObject *
@@ -5805,6 +5824,7 @@ static PyMethodDef TestMethods[] = {
58055824
{"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL},
58065825
{"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS},
58075826
{"pynumber_tobase", pynumber_tobase, METH_VARARGS},
5827+
{"without_gc", without_gc, METH_O},
58085828
{NULL, NULL} /* sentinel */
58095829
};
58105830

Objects/typeobject.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,10 +2612,10 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
26122612
slots = NULL;
26132613

26142614
/* Initialize tp_flags */
2615+
// All heap types need GC, since we can create a reference cycle by storing
2616+
// an instance on one of its parents:
26152617
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2616-
Py_TPFLAGS_BASETYPE;
2617-
if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2618-
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2618+
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC;
26192619

26202620
/* Initialize essential fields */
26212621
type->tp_as_async = &et->as_async;
@@ -2815,21 +2815,11 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
28152815
}
28162816
type->tp_dealloc = subtype_dealloc;
28172817

2818-
/* Enable GC unless this class is not adding new instance variables and
2819-
the base class did not use GC. */
2820-
if ((base->tp_flags & Py_TPFLAGS_HAVE_GC) ||
2821-
type->tp_basicsize > base->tp_basicsize)
2822-
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2823-
28242818
/* Always override allocation strategy to use regular heap */
28252819
type->tp_alloc = PyType_GenericAlloc;
2826-
if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2827-
type->tp_free = PyObject_GC_Del;
2828-
type->tp_traverse = subtype_traverse;
2829-
type->tp_clear = subtype_clear;
2830-
}
2831-
else
2832-
type->tp_free = PyObject_Del;
2820+
type->tp_free = PyObject_GC_Del;
2821+
type->tp_traverse = subtype_traverse;
2822+
type->tp_clear = subtype_clear;
28332823

28342824
/* store type in class' cell if one is supplied */
28352825
cell = _PyDict_GetItemIdWithError(dict, &PyId___classcell__);

0 commit comments

Comments
 (0)