diff options
author | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2008-06-16 19:12:42 +0000 |
---|---|---|
committer | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2008-06-16 19:12:42 +0000 |
commit | e03d9e030371f91b66728823a641028a41a4affc (patch) | |
tree | 1b516e348b2974d24543cb884ed69d37811b5c4f /Objects/weakrefobject.c | |
parent | fe094457b0901d4c121ac3b694512ee0e94b183f (diff) | |
download | cpython-e03d9e030371f91b66728823a641028a41a4affc.tar.gz |
Issue 3110: Crash with weakref subclass,
seen after a "import multiprocessing.reduction"
An instance of a weakref subclass can have attributes.
If such a weakref holds the only strong reference to the object,
deleting the weakref will delete the object. In this case,
the callback must not be called, because the ref object is being deleted!
Diffstat (limited to 'Objects/weakrefobject.c')
-rw-r--r-- | Objects/weakrefobject.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 1aee5a5530..5cd91734e4 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -907,7 +907,8 @@ PyObject_ClearWeakRefs(PyObject *object) current->wr_callback = NULL; clear_weakref(current); if (callback != NULL) { - handle_callback(current, callback); + if (current->ob_refcnt > 0) + handle_callback(current, callback); Py_DECREF(callback); } } @@ -925,9 +926,15 @@ PyObject_ClearWeakRefs(PyObject *object) for (i = 0; i < count; ++i) { PyWeakReference *next = current->wr_next; - Py_INCREF(current); - PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current); - PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback); + if (current->ob_refcnt > 0) + { + Py_INCREF(current); + PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current); + PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback); + } + else { + Py_DECREF(current->wr_callback); + } current->wr_callback = NULL; clear_weakref(current); current = next; @@ -935,6 +942,7 @@ PyObject_ClearWeakRefs(PyObject *object) for (i = 0; i < count; ++i) { PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1); + /* The tuple may have slots left to NULL */ if (callback != NULL) { PyObject *item = PyTuple_GET_ITEM(tuple, i * 2); handle_callback((PyWeakReference *)item, callback); |