Skip to content

Commit 4159443

Browse files
gh-92206: Improve scoping of sqlite3 reset statement helper (#92241)
1 parent d5dfcd4 commit 4159443

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

Modules/_sqlite/cursor.c

+28-10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
101101
return 0;
102102
}
103103

104+
static inline int
105+
stmt_reset(pysqlite_Statement *self)
106+
{
107+
int rc = SQLITE_OK;
108+
109+
if (self->in_use && self->st) {
110+
Py_BEGIN_ALLOW_THREADS
111+
rc = sqlite3_reset(self->st);
112+
Py_END_ALLOW_THREADS
113+
114+
if (rc == SQLITE_OK) {
115+
self->in_use = 0;
116+
}
117+
}
118+
119+
return rc;
120+
}
121+
104122
static int
105123
cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
106124
{
@@ -124,7 +142,7 @@ cursor_clear(pysqlite_Cursor *self)
124142
Py_CLEAR(self->row_factory);
125143
if (self->statement) {
126144
/* Reset the statement if the user has not closed the cursor */
127-
pysqlite_statement_reset(self->statement);
145+
stmt_reset(self->statement);
128146
Py_CLEAR(self->statement);
129147
}
130148

@@ -544,7 +562,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
544562

545563
if (self->statement != NULL) {
546564
/* There is an active statement */
547-
pysqlite_statement_reset(self->statement);
565+
stmt_reset(self->statement);
548566
}
549567

550568
/* reset description and rowcount */
@@ -553,7 +571,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
553571
self->rowcount = 0L;
554572

555573
if (self->statement) {
556-
(void)pysqlite_statement_reset(self->statement);
574+
(void)stmt_reset(self->statement);
557575
}
558576

559577
PyObject *stmt = get_statement_from_cache(self, operation);
@@ -577,7 +595,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
577595
}
578596
}
579597

580-
pysqlite_statement_reset(self->statement);
598+
stmt_reset(self->statement);
581599
pysqlite_statement_mark_dirty(self->statement);
582600

583601
/* We start a transaction implicitly before a DML statement.
@@ -614,7 +632,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
614632
PyErr_Clear();
615633
}
616634
}
617-
(void)pysqlite_statement_reset(self->statement);
635+
(void)stmt_reset(self->statement);
618636
_pysqlite_seterror(state, self->connection->db);
619637
goto error;
620638
}
@@ -663,12 +681,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
663681
}
664682

665683
if (rc == SQLITE_DONE && !multiple) {
666-
pysqlite_statement_reset(self->statement);
684+
stmt_reset(self->statement);
667685
Py_CLEAR(self->statement);
668686
}
669687

670688
if (multiple) {
671-
pysqlite_statement_reset(self->statement);
689+
stmt_reset(self->statement);
672690
}
673691
Py_XDECREF(parameters);
674692
}
@@ -824,7 +842,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
824842
sqlite3_stmt *stmt = self->statement->st;
825843
assert(stmt != NULL);
826844
if (sqlite3_data_count(stmt) == 0) {
827-
(void)pysqlite_statement_reset(self->statement);
845+
(void)stmt_reset(self->statement);
828846
Py_CLEAR(self->statement);
829847
return NULL;
830848
}
@@ -835,7 +853,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
835853
}
836854
int rc = stmt_step(stmt);
837855
if (rc == SQLITE_DONE) {
838-
(void)pysqlite_statement_reset(self->statement);
856+
(void)stmt_reset(self->statement);
839857
}
840858
else if (rc != SQLITE_ROW) {
841859
(void)_pysqlite_seterror(self->connection->state,
@@ -1005,7 +1023,7 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self)
10051023
}
10061024

10071025
if (self->statement) {
1008-
(void)pysqlite_statement_reset(self->statement);
1026+
(void)stmt_reset(self->statement);
10091027
Py_CLEAR(self->statement);
10101028
}
10111029

Modules/_sqlite/statement.c

-19
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,6 @@ pysqlite_statement_bind_parameters(pysqlite_state *state,
366366
}
367367
}
368368

369-
int pysqlite_statement_reset(pysqlite_Statement* self)
370-
{
371-
int rc;
372-
373-
rc = SQLITE_OK;
374-
375-
if (self->in_use && self->st) {
376-
Py_BEGIN_ALLOW_THREADS
377-
rc = sqlite3_reset(self->st);
378-
Py_END_ALLOW_THREADS
379-
380-
if (rc == SQLITE_OK) {
381-
self->in_use = 0;
382-
}
383-
}
384-
385-
return rc;
386-
}
387-
388369
void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
389370
{
390371
self->in_use = 1;

Modules/_sqlite/statement.h

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ void pysqlite_statement_bind_parameters(pysqlite_state *state,
4444
pysqlite_Statement *self,
4545
PyObject *parameters);
4646

47-
int pysqlite_statement_reset(pysqlite_Statement* self);
4847
void pysqlite_statement_mark_dirty(pysqlite_Statement* self);
4948

5049
int pysqlite_statement_setup_types(PyObject *module);

0 commit comments

Comments
 (0)