mirror of
https://github.com/python/cpython
synced 2026-09-29 10:35:30 +03:00
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.
This commit is contained in:
commit
f93ed3fa67
4 changed files with 38 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue