@@ -4071,8 +4071,6 @@ type_dealloc_common(PyTypeObject *type)
40714071 remove_all_subclasses (type , type -> tp_bases );
40724072 PyErr_Restore (tp , val , tb );
40734073 }
4074-
4075- PyObject_ClearWeakRefs ((PyObject * )type );
40764074}
40774075
40784076
@@ -4094,19 +4092,29 @@ _PyStaticType_Dealloc(PyTypeObject *type)
40944092 Py_CLEAR (type -> tp_cache );
40954093 // type->tp_subclasses is NULL
40964094
4095+ // PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0
4096+ if (Py_REFCNT (type ) == 0 ) {
4097+ PyObject_ClearWeakRefs ((PyObject * )type );
4098+ }
4099+
40974100 type -> tp_flags &= ~Py_TPFLAGS_READY ;
40984101}
40994102
41004103
41014104static void
41024105type_dealloc (PyTypeObject * type )
41034106{
4104- /* Assert this is a heap-allocated type object */
4107+ // Assert this is a heap-allocated type object
41054108 _PyObject_ASSERT ((PyObject * )type , type -> tp_flags & Py_TPFLAGS_HEAPTYPE );
4109+
41064110 _PyObject_GC_UNTRACK (type );
41074111
41084112 type_dealloc_common (type );
41094113
4114+ // PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0
4115+ assert (Py_REFCNT (type ) == 0 );
4116+ PyObject_ClearWeakRefs ((PyObject * )type );
4117+
41104118 Py_XDECREF (type -> tp_base );
41114119 Py_XDECREF (type -> tp_dict );
41124120 Py_XDECREF (type -> tp_bases );
0 commit comments