@@ -1123,11 +1123,6 @@ subtype_dealloc(PyObject *self)
1123
1123
Py_TRASHCAN_SAFE_BEGIN (self );
1124
1124
-- _PyTrash_delete_nesting ;
1125
1125
-- tstate -> trash_delete_nesting ;
1126
- /* DO NOT restore GC tracking at this point. weakref callbacks
1127
- * (if any, and whether directly here or indirectly in something we
1128
- * call) may trigger GC, and if self is tracked at that point, it
1129
- * will look like trash to GC and GC will try to delete self again.
1130
- */
1131
1126
1132
1127
/* Find the nearest base with a different tp_dealloc */
1133
1128
base = type ;
@@ -1138,30 +1133,36 @@ subtype_dealloc(PyObject *self)
1138
1133
1139
1134
has_finalizer = type -> tp_finalize || type -> tp_del ;
1140
1135
1141
- /* Maybe call finalizer; exit early if resurrected */
1142
- if (has_finalizer )
1143
- _PyObject_GC_TRACK (self );
1144
-
1145
1136
if (type -> tp_finalize ) {
1137
+ _PyObject_GC_TRACK (self );
1146
1138
if (PyObject_CallFinalizerFromDealloc (self ) < 0 ) {
1147
1139
/* Resurrected */
1148
1140
goto endlabel ;
1149
1141
}
1142
+ _PyObject_GC_UNTRACK (self );
1150
1143
}
1151
- /* If we added a weaklist, we clear it. Do this *before* calling
1152
- tp_del, clearing slots, or clearing the instance dict. */
1144
+ /*
1145
+ If we added a weaklist, we clear it. Do this *before* calling tp_del,
1146
+ clearing slots, or clearing the instance dict.
1147
+
1148
+ GC tracking must be off at this point. weakref callbacks (if any, and
1149
+ whether directly here or indirectly in something we call) may trigger GC,
1150
+ and if self is tracked at that point, it will look like trash to GC and GC
1151
+ will try to delete self again.
1152
+ */
1153
1153
if (type -> tp_weaklistoffset && !base -> tp_weaklistoffset )
1154
1154
PyObject_ClearWeakRefs (self );
1155
1155
1156
1156
if (type -> tp_del ) {
1157
+ _PyObject_GC_TRACK (self );
1157
1158
type -> tp_del (self );
1158
1159
if (self -> ob_refcnt > 0 ) {
1159
1160
/* Resurrected */
1160
1161
goto endlabel ;
1161
1162
}
1163
+ _PyObject_GC_UNTRACK (self );
1162
1164
}
1163
1165
if (has_finalizer ) {
1164
- _PyObject_GC_UNTRACK (self );
1165
1166
/* New weakrefs could be created during the finalizer call.
1166
1167
If this occurs, clear them out without calling their
1167
1168
finalizers since they might rely on part of the object
0 commit comments