diff options
author | Victor Stinner <vstinner@python.org> | 2023-06-22 19:56:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-22 19:56:44 (GMT) |
commit | c38da1e3e19a7bf1053c6d52e730e970efeceff6 (patch) | |
tree | f66d68229c8c1c4667a08b29793d8b66480ca774 /Include | |
parent | d8f87cdf94a6533c5cf2d25e09e6fa3eb06720b9 (diff) | |
download | cpython-c38da1e3e19a7bf1053c6d52e730e970efeceff6.zip cpython-c38da1e3e19a7bf1053c6d52e730e970efeceff6.tar.gz cpython-c38da1e3e19a7bf1053c6d52e730e970efeceff6.tar.bz2 |
gh-105927: Add _PyWeakref_IS_DEAD() function (#105992)
* Add _PyWeakref_IS_DEAD() internal function.
* Modify is_dead_weakref() of Modules/_weakref.c and
_pysqlite_drop_unused_cursor_references() to replace
PyWeakref_GET_OBJECT() with _PyWeakref_IS_DEAD().
* Replace "int i" with "Py_ssize_t i" to iterate on cursors
in _pysqlite_drop_unused_cursor_references().
Diffstat (limited to 'Include')
-rw-r--r-- | Include/internal/pycore_weakref.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Include/internal/pycore_weakref.h b/Include/internal/pycore_weakref.h index 4e2b979..f31b124 100644 --- a/Include/internal/pycore_weakref.h +++ b/Include/internal/pycore_weakref.h @@ -33,6 +33,19 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) { return Py_NewRef(obj); } +static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) { + assert(PyWeakref_Check(ref_obj)); + PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj); + PyObject *obj = ref->wr_object; + if (obj == Py_None) { + // clear_weakref() was called + return 1; + } + + // See _PyWeakref_GET_REF() for the rationale of this test + return (Py_REFCNT(obj) == 0); +} + #ifdef __cplusplus } #endif |