@@ -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