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().
This commit is contained in:
Victor Stinner 2023-06-22 21:56:44 +02:00 committed by GitHub
parent d8f87cdf94
commit c38da1e3e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 14 deletions

View file

@ -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