Index: Objects/typeobject.c =================================================================== --- Objects/typeobject.c (revision 47131) +++ Objects/typeobject.c (working copy) @@ -666,6 +666,18 @@ goto endlabel; /* resurrected */ else _PyObject_GC_UNTRACK(self); + /* New weakrefs could be created during the finalizer call. + If this occurs, clear them out without calling their + finalizers since they might rely on state of the object + that has already been destroyed. */ + if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { + /* Modeled after GET_WEAKREFS_LISTPTR() */ + PyWeakReference **list = + (PyWeakReference **)PyObject_GET_WEAKREFS_LISTPTR(self); + + if (*list) + _PyWeakref_ClearRef(*list); + } } /* Clear slots up to the nearest base with a different tp_dealloc */