diff options
author | Brett Cannon <bcannon@gmail.com> | 2007-01-23 22:41:20 +0000 |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2007-01-23 22:41:20 +0000 |
commit | 75ba075110bbb3dcac6e975540bf8eda65774ec1 (patch) | |
tree | 012de9cedbf5036184d8bbbea0ec4da61ea1f00f /Objects | |
parent | 601d03a5be6a6952ecb57750f0365d6ef2abd4eb (diff) | |
download | cpython-git-75ba075110bbb3dcac6e975540bf8eda65774ec1.tar.gz |
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.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/typeobject.c | 11 | ||||
-rw-r--r-- | Objects/weakrefobject.c | 3 |
2 files changed, 14 insertions, 0 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c index d4a46c37f0..a8395ef9b2 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -666,6 +666,17 @@ subtype_dealloc(PyObject *self) 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 */ diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 826f5710dc..a404f29ade 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -57,6 +57,9 @@ clear_weakref(PyWeakReference *self) PyWeakref_GET_OBJECT(self)); if (*list == self) + /* If 'self' is the end of the list (and thus self->wr_next == NULL) + then the weakref list itself (and thus the value of *list) will + end up being set to NULL. */ *list = self->wr_next; self->wr_object = Py_None; if (self->wr_prev != NULL) |