Skip to content

gh-92206: Improve scoping of sqlite3 register cursor helper #92212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 23 additions & 1 deletion Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}

Expand Down