diff Objects/typeobject.c @ 40705:293311e63a73 2.5

If you created a weakref in an object's __del__ method to itself it would segfault the interpreter during weakref clean up. Now any new weakrefs created after __del__ is run are removed silently. Fixes bug #1377858 and the weakref_in_del crasher for new-style classes. Classic classes are still affected.
author Brett Cannon <bcannon@gmail.com>
date Tue, 23 Jan 2007 22:41:20 +0000
parents 6ba402b60041
children 83efff4d5ae9
line wrap: on
line diff
--- a/Objects/typeobject.c	Tue Jan 23 21:11:58 2007 +0000
+++ b/Objects/typeobject.c	Tue Jan 23 22:41:20 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 */