[Python-checkins] bpo-45041: Simplify `sqlite3.Cursor.executescript()` (GH-28020)

pablogsal webhook-mailer at python.org
Sun Sep 19 18:52:41 EDT 2021


https://github.com/python/cpython/commit/a6779715c4d0289acb59a8fd3660ab2e5d486c4b
commit: a6779715c4d0289acb59a8fd3660ab2e5d486c4b
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-09-19T23:52:36+01:00
summary:

bpo-45041: Simplify `sqlite3.Cursor.executescript()` (GH-28020)

files:
M Modules/_sqlite/cursor.c

diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index d0c9e7f265520..38ccdcf5379d0 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -721,19 +721,13 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
                                    const char *sql_script)
 /*[clinic end generated code: output=8fd726dde1c65164 input=1ac0693dc8db02a8]*/
 {
-    _Py_IDENTIFIER(commit);
-    sqlite3_stmt* statement;
-    int rc;
-    size_t sql_len;
-    PyObject* result;
-
     if (!check_cursor(self)) {
         return NULL;
     }
 
     self->reset = 0;
 
-    sql_len = strlen(sql_script);
+    size_t sql_len = strlen(sql_script);
     int max_length = sqlite3_limit(self->connection->db,
                                    SQLITE_LIMIT_LENGTH, -1);
     if (sql_len >= (unsigned)max_length) {
@@ -742,47 +736,37 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
         return NULL;
     }
 
-    /* commit first */
-    result = _PyObject_CallMethodIdNoArgs((PyObject *)self->connection, &PyId_commit);
-    if (!result) {
-        goto error;
-    }
-    Py_DECREF(result);
-
-    pysqlite_state *state = self->connection->state;
-    while (1) {
-        const char *tail;
+    // Commit if needed
+    sqlite3 *db = self->connection->db;
+    if (!sqlite3_get_autocommit(db)) {
+        int rc = SQLITE_OK;
 
         Py_BEGIN_ALLOW_THREADS
-        rc = sqlite3_prepare_v2(self->connection->db,
-                                sql_script,
-                                (int)sql_len + 1,
-                                &statement,
-                                &tail);
+        rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
         Py_END_ALLOW_THREADS
+
         if (rc != SQLITE_OK) {
-            _pysqlite_seterror(state, self->connection->db);
             goto error;
         }
+    }
 
-        /* execute statement, and ignore results of SELECT statements */
-        do {
-            rc = pysqlite_step(statement);
-            if (PyErr_Occurred()) {
-                (void)sqlite3_finalize(statement);
-                goto error;
-            }
-        } while (rc == SQLITE_ROW);
+    while (1) {
+        int rc;
+        const char *tail;
 
-        if (rc != SQLITE_DONE) {
-            (void)sqlite3_finalize(statement);
-            _pysqlite_seterror(state, self->connection->db);
-            goto error;
+        Py_BEGIN_ALLOW_THREADS
+        sqlite3_stmt *stmt;
+        rc = sqlite3_prepare_v2(db, sql_script, (int)sql_len + 1, &stmt,
+                                &tail);
+        if (rc == SQLITE_OK) {
+            do {
+                (void)sqlite3_step(stmt);
+            } while (rc == SQLITE_ROW);
+            rc = sqlite3_finalize(stmt);
         }
+        Py_END_ALLOW_THREADS
 
-        rc = sqlite3_finalize(statement);
         if (rc != SQLITE_OK) {
-            _pysqlite_seterror(state, self->connection->db);
             goto error;
         }
 
@@ -793,12 +777,11 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
         sql_script = tail;
     }
 
+    return Py_NewRef((PyObject *)self);
+
 error:
-    if (PyErr_Occurred()) {
-        return NULL;
-    } else {
-        return Py_NewRef((PyObject *)self);
-    }
+    _pysqlite_seterror(self->connection->state, db);
+    return NULL;
 }
 
 static PyObject *



More information about the Python-checkins mailing list