summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-12-08 20:15:26 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-12-08 20:15:26 (GMT)
commitd38c990bb73f47a6293e5f783131b709e7ef0982 (patch)
tree5dab3e90b67c7aa1e5b8a8ba3e10338bd7dfe188 /Include
parentbd5279ea247e46d4bf88d1b1b306060e479e227f (diff)
downloadcpython-d38c990bb73f47a6293e5f783131b709e7ef0982.zip
cpython-d38c990bb73f47a6293e5f783131b709e7ef0982.tar.gz
cpython-d38c990bb73f47a6293e5f783131b709e7ef0982.tar.bz2
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Thanks to Eugene Toder for diagnosing and reporting the issue.
Diffstat (limited to 'Include')
-rw-r--r--Include/weakrefobject.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/Include/weakrefobject.h b/Include/weakrefobject.h
index f15c9d9c..e46aecf 100644
--- a/Include/weakrefobject.h
+++ b/Include/weakrefobject.h
@@ -66,7 +66,17 @@ PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
-#define PyWeakref_GET_OBJECT(ref) (((PyWeakReference *)(ref))->wr_object)
+/* Explanation for the Py_REFCNT() check: when a weakref's target is part
+ of a long chain of deallocations which triggers the trashcan mechanism,
+ clearing the weakrefs can be delayed long after the target's refcount
+ has dropped to zero. In the meantime, code accessing the weakref will
+ be able to "see" the target object even though it is supposed to be
+ unreachable. See issue #16602. */
+
+#define PyWeakref_GET_OBJECT(ref) \
+ (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \
+ ? ((PyWeakReference *)(ref))->wr_object \
+ : Py_None)
#ifdef __cplusplus