diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index dcd46feff0cc48..347ded80b7c18f 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -2347,14 +2347,18 @@ PyObject_GC_Del(void *op) size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type); PyGC_Head *g = AS_GC(op); if (_PyObject_GC_IS_TRACKED(op)) { + gc_list_remove(g); #ifdef Py_DEBUG + PyObject *exc, *exc_value, *exc_tb; + PyErr_Fetch(&exc, &exc_value, &exc_tb); if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0, "gc", NULL, "Object of type %s is not untracked before destruction", ((PyObject*)op)->ob_type->tp_name)) { PyErr_WriteUnraisable(NULL); } + if (exc != NULL) + PyErr_Restore(exc, exc_value, exc_tb); #endif - gc_list_remove(g); } GCState *gcstate = get_gc_state(); if (gcstate->generations[0].count > 0) {