]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46249: Move set lastrowid out of the sqlite3 query loop (GH-30489)
authorErlend Egeberg Aasland <erlend.aasland@innova.no>
Sat, 22 Jan 2022 09:40:22 +0000 (10:40 +0100)
committerGitHub <noreply@github.com>
Sat, 22 Jan 2022 09:40:22 +0000 (18:40 +0900)
Modules/_sqlite/cursor.c

index 2729a85f3195d69ef85ea90b5050acd963593b3d..4700afbbf11881b340bab5a90b23f63f118434e5 100644 (file)
@@ -465,7 +465,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
     int rc;
     int numcols;
     PyObject* column_name;
-    sqlite_int64 lastrowid;
 
     if (!check_cursor(self)) {
         goto error;
@@ -630,16 +629,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
             self->rowcount= -1L;
         }
 
-        if (!multiple) {
-            Py_BEGIN_ALLOW_THREADS
-            lastrowid = sqlite3_last_insert_rowid(self->connection->db);
-            Py_END_ALLOW_THREADS
-            Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
-            if (self->lastrowid == NULL) {
-                goto error;
-            }
-        }
-
         if (rc == SQLITE_DONE && !multiple) {
             pysqlite_statement_reset(self->statement);
             Py_CLEAR(self->statement);
@@ -651,6 +640,17 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
         Py_XDECREF(parameters);
     }
 
+    if (!multiple) {
+        sqlite_int64 lastrowid;
+
+        Py_BEGIN_ALLOW_THREADS
+        lastrowid = sqlite3_last_insert_rowid(self->connection->db);
+        Py_END_ALLOW_THREADS
+
+        Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
+        // Fall through on error.
+    }
+
 error:
     Py_XDECREF(parameters);
     Py_XDECREF(parameters_iter);