Skip to content

Commit 99adc05

Browse files
author
Erlend E. Aasland
committed
bpo-44958: Only reset sqlite3 statements when needed
1 parent 3db42fc commit 99adc05

File tree

1 file changed

+7
-33
lines changed

1 file changed

+7
-33
lines changed

Modules/_sqlite/cursor.c

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,7 @@ cursor_clear(pysqlite_Cursor *self)
106106
Py_CLEAR(self->row_cast_map);
107107
Py_CLEAR(self->lastrowid);
108108
Py_CLEAR(self->row_factory);
109-
if (self->statement) {
110-
/* Reset the statement if the user has not closed the cursor */
111-
pysqlite_statement_reset(self->statement);
112-
Py_CLEAR(self->statement);
113-
}
109+
Py_CLEAR(self->statement);
114110
Py_CLEAR(self->next_row);
115111

116112
return 0;
@@ -124,6 +120,9 @@ cursor_dealloc(pysqlite_Cursor *self)
124120
if (self->in_weakreflist != NULL) {
125121
PyObject_ClearWeakRefs((PyObject*)self);
126122
}
123+
if (self->statement) {
124+
pysqlite_statement_reset(self->statement);
125+
}
127126
tp->tp_clear((PyObject *)self);
128127
tp->tp_free(self);
129128
Py_DECREF(tp);
@@ -528,20 +527,11 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
528527
}
529528
}
530529

531-
if (self->statement != NULL) {
532-
/* There is an active statement */
533-
pysqlite_statement_reset(self->statement);
534-
}
535-
536530
/* reset description and rowcount */
537531
Py_INCREF(Py_None);
538532
Py_SETREF(self->description, Py_None);
539533
self->rowcount = 0L;
540534

541-
if (self->statement) {
542-
(void)pysqlite_statement_reset(self->statement);
543-
}
544-
545535
PyObject *stmt = get_statement_from_cache(self, operation);
546536
Py_XSETREF(self->statement, (pysqlite_Statement *)stmt);
547537
if (!self->statement) {
@@ -555,8 +545,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
555545
goto error;
556546
}
557547
}
558-
559-
pysqlite_statement_reset(self->statement);
560548
pysqlite_statement_mark_dirty(self->statement);
561549

562550
/* We start a transaction implicitly before a DML statement.
@@ -577,6 +565,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
577565
break;
578566
}
579567

568+
pysqlite_statement_reset(self->statement);
580569
pysqlite_statement_mark_dirty(self->statement);
581570

582571
pysqlite_statement_bind_parameters(state, self->statement, parameters);
@@ -594,7 +583,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
594583
PyErr_Clear();
595584
}
596585
}
597-
(void)pysqlite_statement_reset(self->statement);
598586
_pysqlite_seterror(state, self->connection->db);
599587
goto error;
600588
}
@@ -664,13 +652,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
664652
if (self->next_row == NULL)
665653
goto error;
666654
} else if (rc == SQLITE_DONE && !multiple) {
667-
pysqlite_statement_reset(self->statement);
668655
Py_CLEAR(self->statement);
669656
}
670657

671-
if (multiple) {
672-
pysqlite_statement_reset(self->statement);
673-
}
674658
Py_XDECREF(parameters);
675659
}
676660

@@ -837,10 +821,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
837821
}
838822

839823
if (!self->next_row) {
840-
if (self->statement) {
841-
(void)pysqlite_statement_reset(self->statement);
842-
Py_CLEAR(self->statement);
843-
}
824+
Py_CLEAR(self->statement);
844825
return NULL;
845826
}
846827

@@ -862,12 +843,10 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
862843
if (self->statement) {
863844
rc = pysqlite_step(self->statement->st);
864845
if (PyErr_Occurred()) {
865-
(void)pysqlite_statement_reset(self->statement);
866846
Py_DECREF(next_row);
867847
return NULL;
868848
}
869849
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
870-
(void)pysqlite_statement_reset(self->statement);
871850
Py_DECREF(next_row);
872851
_pysqlite_seterror(self->connection->state, self->connection->db);
873852
return NULL;
@@ -876,7 +855,6 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
876855
if (rc == SQLITE_ROW) {
877856
self->next_row = _pysqlite_fetch_one_row(self);
878857
if (self->next_row == NULL) {
879-
(void)pysqlite_statement_reset(self->statement);
880858
return NULL;
881859
}
882860
}
@@ -1037,11 +1015,7 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
10371015
return NULL;
10381016
}
10391017

1040-
if (self->statement) {
1041-
(void)pysqlite_statement_reset(self->statement);
1042-
Py_CLEAR(self->statement);
1043-
}
1044-
1018+
Py_CLEAR(self->statement);
10451019
self->closed = 1;
10461020

10471021
Py_RETURN_NONE;

0 commit comments

Comments
 (0)