@@ -104,12 +104,7 @@ cursor_clear(pysqlite_Cursor *self)
104104 Py_CLEAR (self -> row_cast_map );
105105 Py_CLEAR (self -> lastrowid );
106106 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 );
113108 return 0 ;
114109}
115110
@@ -121,6 +116,14 @@ cursor_dealloc(pysqlite_Cursor *self)
121116 if (self -> in_weakreflist != NULL ) {
122117 PyObject_ClearWeakRefs ((PyObject * )self );
123118 }
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+ }
124127 tp -> tp_clear ((PyObject * )self );
125128 tp -> tp_free (self );
126129 Py_DECREF (tp );
@@ -515,18 +518,19 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
515518 }
516519 }
517520
518- if (self -> statement != NULL ) {
519- /* There is an active statement */
520- pysqlite_statement_reset (self -> statement );
521- }
522-
523521 /* reset description and rowcount */
524522 Py_INCREF (Py_None );
525523 Py_SETREF (self -> description , Py_None );
526524 self -> rowcount = 0L ;
527525
528526 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+ }
530534 }
531535
532536 PyObject * stmt = get_statement_from_cache (self , operation );
@@ -549,8 +553,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
549553 goto error ;
550554 }
551555 }
552-
553- pysqlite_statement_reset (self -> statement );
554556 pysqlite_statement_mark_dirty (self -> statement );
555557
556558 /* We start a transaction implicitly before a DML statement.
@@ -570,6 +572,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
570572 break ;
571573 }
572574
575+ pysqlite_statement_reset (self -> statement );
573576 pysqlite_statement_mark_dirty (self -> statement );
574577
575578 pysqlite_statement_bind_parameters (state , self -> statement , parameters );
@@ -587,7 +590,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
587590 PyErr_Clear ();
588591 }
589592 }
590- (void )pysqlite_statement_reset (self -> statement );
591593 _pysqlite_seterror (state , self -> connection -> db );
592594 goto error ;
593595 }
@@ -646,13 +648,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
646648 }
647649
648650 if (rc == SQLITE_DONE && !multiple ) {
649- pysqlite_statement_reset (self -> statement );
650651 Py_CLEAR (self -> statement );
651652 }
652653
653- if (multiple ) {
654- pysqlite_statement_reset (self -> statement );
655- }
656654 Py_XDECREF (parameters );
657655 }
658656
@@ -804,7 +802,6 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
804802 sqlite3_stmt * stmt = self -> statement -> st ;
805803 assert (stmt != NULL );
806804 if (sqlite3_data_count (stmt ) == 0 ) {
807- (void )pysqlite_statement_reset (self -> statement );
808805 Py_CLEAR (self -> statement );
809806 return NULL ;
810807 }
@@ -815,7 +812,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
815812 }
816813 int rc = pysqlite_step (stmt );
817814 if (rc == SQLITE_DONE ) {
818- ( void ) pysqlite_statement_reset (self -> statement );
815+ Py_CLEAR (self -> statement );
819816 }
820817 else if (rc != SQLITE_ROW ) {
821818 (void )_pysqlite_seterror (self -> connection -> state ,
@@ -985,11 +982,7 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
985982 return NULL ;
986983 }
987984
988- if (self -> statement ) {
989- (void )pysqlite_statement_reset (self -> statement );
990- Py_CLEAR (self -> statement );
991- }
992-
985+ Py_CLEAR (self -> statement );
993986 self -> closed = 1 ;
994987
995988 Py_RETURN_NONE ;
0 commit comments