Skip to content

Commit c38da1e

Browse files
authored
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().
1 parent d8f87cd commit c38da1e

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

Include/internal/pycore_weakref.h

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
3333
return Py_NewRef(obj);
3434
}
3535

36+
static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
37+
assert(PyWeakref_Check(ref_obj));
38+
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
39+
PyObject *obj = ref->wr_object;
40+
if (obj == Py_None) {
41+
// clear_weakref() was called
42+
return 1;
43+
}
44+
45+
// See _PyWeakref_GET_REF() for the rationale of this test
46+
return (Py_REFCNT(obj) == 0);
47+
}
48+
3649
#ifdef __cplusplus
3750
}
3851
#endif

Modules/_sqlite/connection.c

+14-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24+
#ifndef Py_BUILD_CORE_BUILTIN
25+
# define Py_BUILD_CORE_MODULE 1
26+
#endif
27+
2428
#include "module.h"
2529
#include "structmember.h" // PyMemberDef
2630
#include "connection.h"
@@ -29,6 +33,7 @@
2933
#include "blob.h"
3034
#include "prepare_protocol.h"
3135
#include "util.h"
36+
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
3237

3338
#include <stdbool.h>
3439

@@ -969,29 +974,26 @@ final_callback(sqlite3_context *context)
969974

970975
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
971976
{
972-
PyObject* new_list;
973-
PyObject* weakref;
974-
int i;
975-
976977
/* we only need to do this once in a while */
977978
if (self->created_cursors++ < 200) {
978979
return;
979980
}
980981

981982
self->created_cursors = 0;
982983

983-
new_list = PyList_New(0);
984+
PyObject* new_list = PyList_New(0);
984985
if (!new_list) {
985986
return;
986987
}
987988

988-
for (i = 0; i < PyList_Size(self->cursors); i++) {
989-
weakref = PyList_GetItem(self->cursors, i);
990-
if (PyWeakref_GetObject(weakref) != Py_None) {
991-
if (PyList_Append(new_list, weakref) != 0) {
992-
Py_DECREF(new_list);
993-
return;
994-
}
989+
for (Py_ssize_t i = 0; i < PyList_Size(self->cursors); i++) {
990+
PyObject* weakref = PyList_GetItem(self->cursors, i);
991+
if (_PyWeakref_IS_DEAD(weakref)) {
992+
continue;
993+
}
994+
if (PyList_Append(new_list, weakref) != 0) {
995+
Py_DECREF(new_list);
996+
return;
995997
}
996998
}
997999

Modules/_weakref.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Python.h"
2-
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
2+
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
3+
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
34

45

56
#define GET_WEAKREFS_LISTPTR(o) \
@@ -43,7 +44,7 @@ is_dead_weakref(PyObject *value)
4344
PyErr_SetString(PyExc_TypeError, "not a weakref");
4445
return -1;
4546
}
46-
return PyWeakref_GET_OBJECT(value) == Py_None;
47+
return _PyWeakref_IS_DEAD(value);
4748
}
4849

4950
/*[clinic input]

0 commit comments

Comments
 (0)