@@ -521,17 +521,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
521521 }
522522 }
523523
524- if (self -> statement != NULL ) {
525- /* There is an active statement */
526- pysqlite_statement_reset (self -> statement );
527- }
528-
529524 /* reset description and rowcount */
530525 Py_INCREF (Py_None );
531526 Py_SETREF (self -> description , Py_None );
532527 self -> rowcount = 0L ;
533528
534529 if (self -> statement ) {
530+ // Reset pending statements on this cursor.
535531 (void )pysqlite_statement_reset (self -> statement );
536532 }
537533
@@ -570,6 +566,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
570566 }
571567 }
572568
569+ assert (!sqlite3_stmt_busy (self -> statement -> st ));
573570 while (1 ) {
574571 parameters = PyIter_Next (parameters_iter );
575572 if (!parameters ) {
@@ -593,7 +590,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
593590 PyErr_Clear ();
594591 }
595592 }
596- (void )pysqlite_statement_reset (self -> statement );
597593 _pysqlite_seterror (state , self -> connection -> db );
598594 goto error ;
599595 }
@@ -651,13 +647,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
651647 }
652648 }
653649
654- if (rc == SQLITE_DONE && !multiple ) {
655- pysqlite_statement_reset (self -> statement );
656- Py_CLEAR (self -> statement );
657- }
658-
659- if (multiple ) {
660- pysqlite_statement_reset (self -> statement );
650+ if (rc == SQLITE_DONE ) {
651+ (void )pysqlite_statement_reset (self -> statement );
661652 }
662653 Py_XDECREF (parameters );
663654 }
@@ -670,11 +661,17 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
670661 self -> locked = 0 ;
671662
672663 if (PyErr_Occurred ()) {
664+ if (self -> statement ) {
665+ (void )pysqlite_statement_reset (self -> statement );
666+ Py_CLEAR (self -> statement );
667+ }
673668 self -> rowcount = -1L ;
674669 return NULL ;
675- } else {
676- return Py_NewRef ((PyObject * )self );
677670 }
671+ if (self -> statement && !sqlite3_stmt_busy (self -> statement -> st )) {
672+ Py_CLEAR (self -> statement );
673+ }
674+ return Py_NewRef ((PyObject * )self );
678675}
679676
680677/*[clinic input]
@@ -809,25 +806,23 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
809806
810807 sqlite3_stmt * stmt = self -> statement -> st ;
811808 assert (stmt != NULL );
812- if (sqlite3_data_count (stmt ) == 0 ) {
813- (void )pysqlite_statement_reset (self -> statement );
814- Py_CLEAR (self -> statement );
815- return NULL ;
816- }
809+ assert (sqlite3_data_count (stmt ) != 0 );
817810
818811 PyObject * row = _pysqlite_fetch_one_row (self );
819812 if (row == NULL ) {
820813 return NULL ;
821814 }
822815 int rc = pysqlite_step (stmt );
823- if (rc == SQLITE_DONE ) {
816+ if (rc != SQLITE_ROW ) {
824817 (void )pysqlite_statement_reset (self -> statement );
825- }
826- else if (rc != SQLITE_ROW ) {
827- (void )_pysqlite_seterror (self -> connection -> state ,
828- self -> connection -> db );
829- Py_DECREF (row );
830- return NULL ;
818+ Py_CLEAR (self -> statement );
819+
820+ if (rc != SQLITE_DONE ) {
821+ (void )_pysqlite_seterror (self -> connection -> state ,
822+ self -> connection -> db );
823+ Py_DECREF (row );
824+ return NULL ;
825+ }
831826 }
832827 if (!Py_IsNone (self -> row_factory )) {
833828 PyObject * factory = self -> row_factory ;
0 commit comments