Skip to content

Commit 1d4a9a4

Browse files
gh-92206: Improve scoping of sqlite3 register cursor helper (#92212)
1 parent 628d6e8 commit 1d4a9a4

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

Modules/_sqlite/connection.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -371,32 +371,6 @@ connection_dealloc(pysqlite_Connection *self)
371371
Py_DECREF(tp);
372372
}
373373

374-
/*
375-
* Registers a cursor with the connection.
376-
*
377-
* 0 => error; 1 => ok
378-
*/
379-
int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
380-
{
381-
PyObject* weakref;
382-
383-
weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
384-
if (!weakref) {
385-
goto error;
386-
}
387-
388-
if (PyList_Append(connection->cursors, weakref) != 0) {
389-
Py_CLEAR(weakref);
390-
goto error;
391-
}
392-
393-
Py_DECREF(weakref);
394-
395-
return 1;
396-
error:
397-
return 0;
398-
}
399-
400374
/*[clinic input]
401375
_sqlite3.Connection.cursor as pysqlite_connection_cursor
402376

Modules/_sqlite/connection.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ typedef struct
9999
PyObject* NotSupportedError;
100100
} pysqlite_Connection;
101101

102-
int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
103102
int pysqlite_check_thread(pysqlite_Connection* self);
104103
int pysqlite_check_connection(pysqlite_Connection* con);
105104

Modules/_sqlite/cursor.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ class _sqlite3.Cursor "pysqlite_Cursor *" "clinic_state()->CursorType"
3535
[clinic start generated code]*/
3636
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c5b8115c5cf30f1]*/
3737

38+
/*
39+
* Registers a cursor with the connection.
40+
*
41+
* 0 => error; 1 => ok
42+
*/
43+
static int
44+
register_cursor(pysqlite_Connection *connection, PyObject *cursor)
45+
{
46+
PyObject *weakref = PyWeakref_NewRef((PyObject *)cursor, NULL);
47+
if (weakref == NULL) {
48+
return 0;
49+
}
50+
51+
if (PyList_Append(connection->cursors, weakref) < 0) {
52+
Py_CLEAR(weakref);
53+
return 0;
54+
}
55+
56+
Py_DECREF(weakref);
57+
return 1;
58+
}
59+
3860
/*[clinic input]
3961
_sqlite3.Cursor.__init__ as pysqlite_cursor_init
4062
@@ -70,7 +92,7 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
7092
return -1;
7193
}
7294

73-
if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
95+
if (!register_cursor(connection, (PyObject *)self)) {
7496
return -1;
7597
}
7698

0 commit comments

Comments
 (0)