From 43cbf83d8c6c25a88bd2981361b3fd48899e6af6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 26 Jun 2023 12:37:02 +0200 Subject: [PATCH] gh-106104: Use public weakref APIs in sqlite3 The sqlite3 extension module should be built using the public API, so we're removing the Py_BUILD_CORE_MODULE define that was introduced as part of the gh-105927 changes. --- Modules/_sqlite/blob.c | 19 ++++++++++--------- Modules/_sqlite/connection.c | 24 ++++++++++++++---------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c index 989d9a83b590ca..08a27eff88732a 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) { + PyObject *blob; + if (!PyWeakref_Check(weakref)) { continue; } - close_blob((pysqlite_Blob *)blob); - Py_DECREF(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..220ec0d44a0bb6 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); + PyObject *item; + if (!PyWeakref_Check(weakref)) { continue; } - if (PyList_Append(new_list, weakref) != 0) { - Py_DECREF(new_list); - return; + 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; + } } }