@@ -104,7 +104,12 @@ cursor_clear(pysqlite_Cursor *self)
104
104
Py_CLEAR (self -> row_cast_map );
105
105
Py_CLEAR (self -> lastrowid );
106
106
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
+
108
113
return 0 ;
109
114
}
110
115
@@ -116,14 +121,6 @@ cursor_dealloc(pysqlite_Cursor *self)
116
121
if (self -> in_weakreflist != NULL ) {
117
122
PyObject_ClearWeakRefs ((PyObject * )self );
118
123
}
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
- }
127
124
tp -> tp_clear ((PyObject * )self );
128
125
tp -> tp_free (self );
129
126
Py_DECREF (tp );
@@ -518,19 +515,18 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
518
515
}
519
516
}
520
517
518
+ if (self -> statement != NULL ) {
519
+ /* There is an active statement */
520
+ pysqlite_statement_reset (self -> statement );
521
+ }
522
+
521
523
/* reset description and rowcount */
522
524
Py_INCREF (Py_None );
523
525
Py_SETREF (self -> description , Py_None );
524
526
self -> rowcount = 0L ;
525
527
526
528
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 );
534
530
}
535
531
536
532
PyObject * stmt = get_statement_from_cache (self , operation );
@@ -553,6 +549,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
553
549
goto error ;
554
550
}
555
551
}
552
+
553
+ pysqlite_statement_reset (self -> statement );
556
554
pysqlite_statement_mark_dirty (self -> statement );
557
555
558
556
/* We start a transaction implicitly before a DML statement.
@@ -572,7 +570,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
572
570
break ;
573
571
}
574
572
575
- pysqlite_statement_reset (self -> statement );
576
573
pysqlite_statement_mark_dirty (self -> statement );
577
574
578
575
pysqlite_statement_bind_parameters (state , self -> statement , parameters );
@@ -590,6 +587,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
590
587
PyErr_Clear ();
591
588
}
592
589
}
590
+ (void )pysqlite_statement_reset (self -> statement );
593
591
_pysqlite_seterror (state , self -> connection -> db );
594
592
goto error ;
595
593
}
@@ -648,9 +646,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
648
646
}
649
647
650
648
if (rc == SQLITE_DONE && !multiple ) {
649
+ pysqlite_statement_reset (self -> statement );
651
650
Py_CLEAR (self -> statement );
652
651
}
653
652
653
+ if (multiple ) {
654
+ pysqlite_statement_reset (self -> statement );
655
+ }
654
656
Py_XDECREF (parameters );
655
657
}
656
658
@@ -802,6 +804,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
802
804
sqlite3_stmt * stmt = self -> statement -> st ;
803
805
assert (stmt != NULL );
804
806
if (sqlite3_data_count (stmt ) == 0 ) {
807
+ (void )pysqlite_statement_reset (self -> statement );
805
808
Py_CLEAR (self -> statement );
806
809
return NULL ;
807
810
}
@@ -812,7 +815,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
812
815
}
813
816
int rc = pysqlite_step (stmt );
814
817
if (rc == SQLITE_DONE ) {
815
- Py_CLEAR (self -> statement );
818
+ ( void ) pysqlite_statement_reset (self -> statement );
816
819
}
817
820
else if (rc != SQLITE_ROW ) {
818
821
(void )_pysqlite_seterror (self -> connection -> state ,
@@ -982,7 +985,11 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
982
985
return NULL ;
983
986
}
984
987
985
- Py_CLEAR (self -> statement );
988
+ if (self -> statement ) {
989
+ (void )pysqlite_statement_reset (self -> statement );
990
+ Py_CLEAR (self -> statement );
991
+ }
992
+
986
993
self -> closed = 1 ;
987
994
988
995
Py_RETURN_NONE ;
0 commit comments