[3.10] gh-93421: Update sqlite3 cursor.rowcount only after SQLITE_DON… · python/cpython@9cc0afc

GitHub

@@ -492,10 +492,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

492492pysqlite_statement_reset(self->statement);

493493 }

494494495-/* reset description and rowcount */

495+/* reset description */

496496Py_INCREF(Py_None);

497497Py_SETREF(self->description, Py_None);

498-self->rowcount=0L;

499498500499func_args=PyTuple_New(1);

501500if (!func_args) {

@@ -527,6 +526,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

527526528527pysqlite_statement_reset(self->statement);

529528pysqlite_statement_mark_dirty(self->statement);

529+self->rowcount=self->statement->is_dml ? 0L : -1L;

530530531531/* We start a transaction implicitly before a DML statement.

532532 SELECT is the only exception. See #9924. */

@@ -604,12 +604,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

604604 }

605605 }

606606607-if (self->statement->is_dml) {

608-self->rowcount+= (long)sqlite3_changes(self->connection->db);

609- } else {

610-self->rowcount=-1L;

611- }

612-613607if (!multiple) {

614608Py_BEGIN_ALLOW_THREADS

615609lastrowid=sqlite3_last_insert_rowid(self->connection->db);

@@ -630,11 +624,17 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

630624if (self->next_row==NULL)

631625 goto error;

632626 } elseif (rc==SQLITE_DONE&& !multiple) {

627+if (self->statement->is_dml) {

628+self->rowcount= (long)sqlite3_changes(self->connection->db);

629+ }

633630pysqlite_statement_reset(self->statement);

634631Py_CLEAR(self->statement);

635632 }

636633637634if (multiple) {

635+if (self->statement->is_dml&&rc==SQLITE_DONE) {

636+self->rowcount+= (long)sqlite3_changes(self->connection->db);

637+ }

638638pysqlite_statement_reset(self->statement);

639639 }

640640Py_XDECREF(parameters);

@@ -824,7 +824,12 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)

824824if (PyErr_Occurred()) {

825825 goto error;

826826 }

827-if (rc!=SQLITE_DONE&&rc!=SQLITE_ROW) {

827+if (rc==SQLITE_DONE) {

828+if (self->statement->is_dml) {

829+self->rowcount= (long)sqlite3_changes(self->connection->db);

830+ }

831+ }

832+elseif (rc!=SQLITE_ROW) {

828833_pysqlite_seterror(self->connection->db, NULL);

829834 goto error;

830835 }