diff options
author | Erlend Egeberg Aasland <erlend.aasland@protonmail.com> | 2022-05-03 12:33:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 12:33:24 (GMT) |
commit | 1d4a9a45b7ac8c1c5fecc363c988be59500f1ed7 (patch) | |
tree | 732708c26149d1358bd8caf7c930ef5d80a98e37 | |
parent | 628d6e8270339062c591825ea467ab00bb560f6f (diff) | |
download | cpython-1d4a9a45b7ac8c1c5fecc363c988be59500f1ed7.zip cpython-1d4a9a45b7ac8c1c5fecc363c988be59500f1ed7.tar.gz cpython-1d4a9a45b7ac8c1c5fecc363c988be59500f1ed7.tar.bz2 |
gh-92206: Improve scoping of sqlite3 register cursor helper (#92212)
-rw-r--r-- | Modules/_sqlite/connection.c | 26 | ||||
-rw-r--r-- | Modules/_sqlite/connection.h | 1 | ||||
-rw-r--r-- | Modules/_sqlite/cursor.c | 24 |
3 files changed, 23 insertions, 28 deletions
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index fe244c8..75d2970 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -371,32 +371,6 @@ connection_dealloc(pysqlite_Connection *self) Py_DECREF(tp); } -/* - * Registers a cursor with the connection. - * - * 0 => error; 1 => ok - */ -int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor) -{ - PyObject* weakref; - - weakref = PyWeakref_NewRef((PyObject*)cursor, NULL); - if (!weakref) { - goto error; - } - - if (PyList_Append(connection->cursors, weakref) != 0) { - Py_CLEAR(weakref); - goto error; - } - - Py_DECREF(weakref); - - return 1; -error: - return 0; -} - /*[clinic input] _sqlite3.Connection.cursor as pysqlite_connection_cursor diff --git a/Modules/_sqlite/connection.h b/Modules/_sqlite/connection.h index 2b946ff..629fe3d 100644 --- a/Modules/_sqlite/connection.h +++ b/Modules/_sqlite/connection.h @@ -99,7 +99,6 @@ typedef struct PyObject* NotSupportedError; } pysqlite_Connection; -int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor); int pysqlite_check_thread(pysqlite_Connection* self); int pysqlite_check_connection(pysqlite_Connection* con); diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 861704f..84f4817 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -35,6 +35,28 @@ class _sqlite3.Cursor "pysqlite_Cursor *" "clinic_state()->CursorType" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c5b8115c5cf30f1]*/ +/* + * Registers a cursor with the connection. + * + * 0 => error; 1 => ok + */ +static int +register_cursor(pysqlite_Connection *connection, PyObject *cursor) +{ + PyObject *weakref = PyWeakref_NewRef((PyObject *)cursor, NULL); + if (weakref == NULL) { + return 0; + } + + if (PyList_Append(connection->cursors, weakref) < 0) { + Py_CLEAR(weakref); + return 0; + } + + Py_DECREF(weakref); + return 1; +} + /*[clinic input] _sqlite3.Cursor.__init__ as pysqlite_cursor_init @@ -70,7 +92,7 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self, return -1; } - if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) { + if (!register_cursor(connection, (PyObject *)self)) { return -1; } |