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