Skip to content

gh-105927: Add _PyWeakref_IS_DEAD() function #105992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Include/internal/pycore_weakref.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "module.h"
#include "structmember.h" // PyMemberDef
#include "connection.h"
Expand All @@ -29,6 +33,7 @@
#include "blob.h"
#include "prepare_protocol.h"
#include "util.h"
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()

#include <stdbool.h>

Expand Down Expand Up @@ -969,29 +974,26 @@ final_callback(sqlite3_context *context)

static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
{
PyObject* new_list;
PyObject* weakref;
int i;

/* we only need to do this once in a while */
if (self->created_cursors++ < 200) {
return;
}

self->created_cursors = 0;

new_list = PyList_New(0);
PyObject* new_list = PyList_New(0);
if (!new_list) {
return;
}

for (i = 0; i < PyList_Size(self->cursors); i++) {
weakref = PyList_GetItem(self->cursors, i);
if (PyWeakref_GetObject(weakref) != Py_None) {
if (PyList_Append(new_list, weakref) != 0) {
Py_DECREF(new_list);
return;
}
for (Py_ssize_t i = 0; i < PyList_Size(self->cursors); i++) {
PyObject* weakref = PyList_GetItem(self->cursors, i);
if (_PyWeakref_IS_DEAD(weakref)) {
continue;
}
if (PyList_Append(new_list, weakref) != 0) {
Py_DECREF(new_list);
return;
}
}

Expand Down
5 changes: 3 additions & 2 deletions Modules/_weakref.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()


#define GET_WEAKREFS_LISTPTR(o) \
Expand Down Expand Up @@ -43,7 +44,7 @@ is_dead_weakref(PyObject *value)
PyErr_SetString(PyExc_TypeError, "not a weakref");
return -1;
}
return PyWeakref_GET_OBJECT(value) == Py_None;
return _PyWeakref_IS_DEAD(value);
}

/*[clinic input]
Expand Down