diff Objects/typeobject.c @ 40707:dbbdfc038dc9 legacy-trunk

Fix crasher for when an object's __del__ creates a new weakref to itself. Patch only fixes new-style classes; classic classes still buggy. Closes bug #1377858. Already backported.
author Brett Cannon <bcannon@gmail.com>
date Tue, 23 Jan 2007 23:21:22 +0000
parents c3877b6430b5
children f6f391d1d050
line wrap: on
line diff
--- a/Objects/typeobject.c	Tue Jan 23 21:11:47 2007 +0000
+++ b/Objects/typeobject.c	Tue Jan 23 23:21:22 2007 +0000
@@ -666,6 +666,17 @@
 			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 part of the object
+		    being finalized 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);
+			while (*list)
+				_PyWeakref_ClearRef(*list);
+		}
 	}
 
 	/*  Clear slots up to the nearest base with a different tp_dealloc */