@@ -104,7 +104,12 @@ cursor_clear(pysqlite_Cursor *self)
104104 Py_CLEAR (self -> row_cast_map );
105105 Py_CLEAR (self -> lastrowid );
106106 Py_CLEAR (self -> row_factory );
107- Py_CLEAR (self -> statement );
107+ if (self -> statement ) {
108+ /* Reset the statement if the user has not closed the cursor */
109+ pysqlite_statement_reset (self -> statement );
110+ Py_CLEAR (self -> statement );
111+ }
112+
108113 return 0 ;
109114}
110115
@@ -116,14 +121,6 @@ cursor_dealloc(pysqlite_Cursor *self)
116121 if (self -> in_weakreflist != NULL ) {
117122 PyObject_ClearWeakRefs ((PyObject * )self );
118123 }
119- if (self -> statement ) {
120- /* A SELECT query will lock the affected database table(s), so we need
121- * to reset the statement to unlock the database before disappearing */
122- sqlite3_stmt * stmt = self -> statement -> st ;
123- if (sqlite3_stmt_readonly (stmt )) {
124- pysqlite_statement_reset (self -> statement );
125- }
126- }
127124 tp -> tp_clear ((PyObject * )self );
128125 tp -> tp_free (self );
129126 Py_DECREF (tp );
@@ -518,19 +515,18 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
518515 }
519516 }
520517
518+ if (self -> statement != NULL ) {
519+ /* There is an active statement */
520+ pysqlite_statement_reset (self -> statement );
521+ }
522+
521523 /* reset description and rowcount */
522524 Py_INCREF (Py_None );
523525 Py_SETREF (self -> description , Py_None );
524526 self -> rowcount = 0L ;
525527
526528 if (self -> statement ) {
527- /* A SELECT query will lock the affected database table(s), so we need
528- * to reset the statement to unlock the database before switching
529- * statements */
530- sqlite3_stmt * stmt = self -> statement -> st ;
531- if (sqlite3_stmt_readonly (stmt )) {
532- pysqlite_statement_reset (self -> statement );
533- }
529+ (void )pysqlite_statement_reset (self -> statement );
534530 }
535531
536532 PyObject * stmt = get_statement_from_cache (self , operation );
@@ -553,6 +549,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
553549 goto error ;
554550 }
555551 }
552+
553+ pysqlite_statement_reset (self -> statement );
556554 pysqlite_statement_mark_dirty (self -> statement );
557555
558556 /* We start a transaction implicitly before a DML statement.
@@ -572,7 +570,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
572570 break ;
573571 }
574572
575- pysqlite_statement_reset (self -> statement );
576573 pysqlite_statement_mark_dirty (self -> statement );
577574
578575 pysqlite_statement_bind_parameters (state , self -> statement , parameters );
@@ -590,6 +587,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
590587 PyErr_Clear ();
591588 }
592589 }
590+ (void )pysqlite_statement_reset (self -> statement );
593591 _pysqlite_seterror (state , self -> connection -> db );
594592 goto error ;
595593 }
@@ -648,9 +646,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
648646 }
649647
650648 if (rc == SQLITE_DONE && !multiple ) {
649+ pysqlite_statement_reset (self -> statement );
651650 Py_CLEAR (self -> statement );
652651 }
653652
653+ if (multiple ) {
654+ pysqlite_statement_reset (self -> statement );
655+ }
654656 Py_XDECREF (parameters );
655657 }
656658
@@ -802,6 +804,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
802804 sqlite3_stmt * stmt = self -> statement -> st ;
803805 assert (stmt != NULL );
804806 if (sqlite3_data_count (stmt ) == 0 ) {
807+ (void )pysqlite_statement_reset (self -> statement );
805808 Py_CLEAR (self -> statement );
806809 return NULL ;
807810 }
@@ -812,7 +815,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
812815 }
813816 int rc = pysqlite_step (stmt );
814817 if (rc == SQLITE_DONE ) {
815- Py_CLEAR (self -> statement );
818+ ( void ) pysqlite_statement_reset (self -> statement );
816819 }
817820 else if (rc != SQLITE_ROW ) {
818821 (void )_pysqlite_seterror (self -> connection -> state ,
@@ -982,7 +985,11 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
982985 return NULL ;
983986 }
984987
985- Py_CLEAR (self -> statement );
988+ if (self -> statement ) {
989+ (void )pysqlite_statement_reset (self -> statement );
990+ Py_CLEAR (self -> statement );
991+ }
992+
986993 self -> closed = 1 ;
987994
988995 Py_RETURN_NONE ;
0 commit comments