diff options
author | Victor Stinner <vstinner@python.org> | 2023-06-22 19:56:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-22 19:56:44 (GMT) |
commit | c38da1e3e19a7bf1053c6d52e730e970efeceff6 (patch) | |
tree | f66d68229c8c1c4667a08b29793d8b66480ca774 /Modules/_sqlite/connection.c | |
parent | d8f87cdf94a6533c5cf2d25e09e6fa3eb06720b9 (diff) | |
download | cpython-c38da1e3e19a7bf1053c6d52e730e970efeceff6.zip cpython-c38da1e3e19a7bf1053c6d52e730e970efeceff6.tar.gz cpython-c38da1e3e19a7bf1053c6d52e730e970efeceff6.tar.bz2 |
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().
Diffstat (limited to 'Modules/_sqlite/connection.c')
-rw-r--r-- | Modules/_sqlite/connection.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index d12d655..967ba28 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -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" @@ -29,6 +33,7 @@ #include "blob.h" #include "prepare_protocol.h" #include "util.h" +#include "pycore_weakref.h" // _PyWeakref_IS_DEAD() #include <stdbool.h> @@ -969,10 +974,6 @@ error: 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; @@ -980,18 +981,19 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) 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; } } |