[Python-checkins] gh-89121: Keep the number of pending SQLite statements to a minimum (#30379)
erlend-aasland
webhook-mailer at python.org
Thu Jun 23 06:14:11 EDT 2022
https://github.com/python/cpython/commit/89285314bdbd067922d5eb02efd76da07e6fce4e
commit: 89285314bdbd067922d5eb02efd76da07e6fce4e
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at protonmail.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2022-06-23T12:14:05+02:00
summary:
gh-89121: Keep the number of pending SQLite statements to a minimum (#30379)
Make sure statements that have run to completion or errored are
reset and cleared off the cursor for all paths in execute() and
executemany().
files:
M Modules/_sqlite/cursor.c
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 3f5cfef0c3258..8d9f20da530b2 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -830,16 +830,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}
- if (self->statement != NULL) {
- /* There is an active statement */
- stmt_reset(self->statement);
- }
-
/* reset description */
Py_INCREF(Py_None);
Py_SETREF(self->description, Py_None);
if (self->statement) {
+ // Reset pending statements on this cursor.
(void)stmt_reset(self->statement);
}
@@ -879,6 +875,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}
+ assert(!sqlite3_stmt_busy(self->statement->st));
while (1) {
parameters = PyIter_Next(parameters_iter);
if (!parameters) {
@@ -902,7 +899,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
PyErr_Clear();
}
}
- (void)stmt_reset(self->statement);
_pysqlite_seterror(state, self->connection->db);
goto error;
}
@@ -944,16 +940,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}
- if (rc == SQLITE_DONE && !multiple) {
+ if (rc == SQLITE_DONE) {
if (self->statement->is_dml) {
- self->rowcount = (long)sqlite3_changes(self->connection->db);
- }
- stmt_reset(self->statement);
- Py_CLEAR(self->statement);
- }
-
- if (multiple) {
- if (self->statement->is_dml && rc == SQLITE_DONE) {
self->rowcount += (long)sqlite3_changes(self->connection->db);
}
stmt_reset(self->statement);
@@ -980,11 +968,17 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
self->locked = 0;
if (PyErr_Occurred()) {
+ if (self->statement) {
+ (void)stmt_reset(self->statement);
+ Py_CLEAR(self->statement);
+ }
self->rowcount = -1L;
return NULL;
- } else {
- return Py_NewRef((PyObject *)self);
}
+ if (self->statement && !sqlite3_stmt_busy(self->statement->st)) {
+ Py_CLEAR(self->statement);
+ }
+ return Py_NewRef((PyObject *)self);
}
/*[clinic input]
@@ -1111,11 +1105,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
sqlite3_stmt *stmt = self->statement->st;
assert(stmt != NULL);
- if (sqlite3_data_count(stmt) == 0) {
- (void)stmt_reset(self->statement);
- Py_CLEAR(self->statement);
- return NULL;
- }
+ assert(sqlite3_data_count(stmt) != 0);
self->locked = 1; // GH-80254: Prevent recursive use of cursors.
PyObject *row = _pysqlite_fetch_one_row(self);
More information about the Python-checkins
mailing list