summaryrefslogtreecommitdiff
path: root/Include/weakrefobject.h
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-12-08 21:15:26 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2012-12-08 21:15:26 +0100
commit62a0d6ea402d18f528629c6e84ed6f46e7c0912a (patch)
tree72f36fc075688276626f59fb7138baf52d4077ed /Include/weakrefobject.h
parent09974b4e9e8da3c9ba0469f4812e68cf9f700ffd (diff)
downloadcpython-git-62a0d6ea402d18f528629c6e84ed6f46e7c0912a.tar.gz
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/weakrefobject.h')
-rw-r--r--Include/weakrefobject.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/Include/weakrefobject.h b/Include/weakrefobject.h
index 725838726d..537e7ebf28 100644
--- a/Include/weakrefobject.h
+++ b/Include/weakrefobject.h
@@ -70,7 +70,17 @@ PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
#endif
-#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