diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c index 989d9a83b590ca..aafd334510ddc7 100644 --- a/Modules/_sqlite/blob.c +++ b/Modules/_sqlite/blob.c @@ -1,10 +1,5 @@ -#ifndef Py_BUILD_CORE_BUILTIN -# define Py_BUILD_CORE_MODULE 1 -#endif - #include "blob.h" #include "util.h" -#include "pycore_weakref.h" // _PyWeakref_GET_REF() #define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self))) #include "clinic/blob.c.h" @@ -102,12 +97,18 @@ pysqlite_close_all_blobs(pysqlite_Connection *self) { for (int i = 0; i < PyList_GET_SIZE(self->blobs); i++) { PyObject *weakref = PyList_GET_ITEM(self->blobs, i); - PyObject *blob = _PyWeakref_GET_REF(weakref); - if (blob == NULL) { + if (!PyWeakref_Check(weakref)) { continue; } - close_blob((pysqlite_Blob *)blob); - Py_DECREF(blob); + PyObject *blob; + if (PyWeakref_GetRef(weakref, &blob) < 0) { + PyErr_WriteUnraisable((PyObject *)self); + continue; + } + if (blob) { + close_blob((pysqlite_Blob *)blob); + Py_DECREF(blob); + } } } diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 967ba2812080e5..20931e57f84563 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -21,10 +21,6 @@ * 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" @@ -33,7 +29,6 @@ #include "blob.h" #include "prepare_protocol.h" #include "util.h" -#include "pycore_weakref.h" // _PyWeakref_IS_DEAD() #include @@ -987,13 +982,22 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) } for (Py_ssize_t i = 0; i < PyList_Size(self->cursors); i++) { - PyObject* weakref = PyList_GetItem(self->cursors, i); - if (_PyWeakref_IS_DEAD(weakref)) { + PyObject *weakref = PyList_GetItem(self->cursors, i); + if (!PyWeakref_Check(weakref)) { continue; } - if (PyList_Append(new_list, weakref) != 0) { - Py_DECREF(new_list); - return; + PyObject *item; + if (PyWeakref_GetRef(weakref, &item) < 0) { + PyErr_WriteUnraisable((PyObject *)self); + continue; + } + if (item) { + int rc = PyList_Append(new_list, item); + Py_DECREF(item); + if (rc < 0) { + Py_DECREF(new_list); + return; + } } }