Skip to content

Commit 050d103

Browse files
author
Erlend Egeberg Aasland
authored
bpo-44958: Only reset sqlite3 statements when needed (GH-27844)
1 parent debd804 commit 050d103

File tree

4 files changed

+82
-46
lines changed

4 files changed

+82
-46
lines changed

Lib/sqlite3/test/test_regression.py

+42-7
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ def test_type_map_usage(self):
124124
"""
125125
SELECT = "select * from foo"
126126
con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES)
127-
con.execute("create table foo(bar timestamp)")
128-
con.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),))
129-
con.execute(SELECT).close()
130-
con.execute("drop table foo")
131-
con.execute("create table foo(bar integer)")
132-
con.execute("insert into foo(bar) values (5)")
133-
con.execute(SELECT).close()
127+
cur = con.cursor()
128+
cur.execute("create table foo(bar timestamp)")
129+
cur.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),))
130+
cur.execute(SELECT)
131+
cur.execute("drop table foo")
132+
cur.execute("create table foo(bar integer)")
133+
cur.execute("insert into foo(bar) values (5)")
134+
cur.execute(SELECT)
134135

135136
def test_bind_mutating_list(self):
136137
# Issue41662: Crash when mutate a list of parameters during iteration.
@@ -435,6 +436,40 @@ def test_return_empty_bytestring(self):
435436
val = cur.fetchone()[0]
436437
self.assertEqual(val, b'')
437438

439+
def test_table_lock_cursor_replace_stmt(self):
440+
con = sqlite.connect(":memory:")
441+
cur = con.cursor()
442+
cur.execute("create table t(t)")
443+
cur.executemany("insert into t values(?)", ((v,) for v in range(5)))
444+
con.commit()
445+
cur.execute("select t from t")
446+
cur.execute("drop table t")
447+
con.commit()
448+
449+
def test_table_lock_cursor_dealloc(self):
450+
con = sqlite.connect(":memory:")
451+
con.execute("create table t(t)")
452+
con.executemany("insert into t values(?)", ((v,) for v in range(5)))
453+
con.commit()
454+
cur = con.execute("select t from t")
455+
del cur
456+
con.execute("drop table t")
457+
con.commit()
458+
459+
def test_table_lock_cursor_non_readonly_select(self):
460+
con = sqlite.connect(":memory:")
461+
con.execute("create table t(t)")
462+
con.executemany("insert into t values(?)", ((v,) for v in range(5)))
463+
con.commit()
464+
def dup(v):
465+
con.execute("insert into t values(?)", (v,))
466+
return
467+
con.create_function("dup", 1, dup)
468+
cur = con.execute("select dup(t) from t")
469+
del cur
470+
con.execute("drop table t")
471+
con.commit()
472+
438473

439474
if __name__ == "__main__":
440475
unittest.main()

Modules/_sqlite/cursor.c

+19-26
Original file line numberDiff line numberDiff line change
@@ -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;

Modules/_sqlite/statement.c

+20-12
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,31 @@ pysqlite_statement_bind_parameters(pysqlite_state *state,
360360
}
361361
}
362362

363-
int pysqlite_statement_reset(pysqlite_Statement* self)
363+
void
364+
pysqlite_statement_reset(pysqlite_Statement *self)
364365
{
365-
int rc;
366+
sqlite3_stmt *stmt = self->st;
367+
if (stmt == NULL || self->in_use == 0) {
368+
return;
369+
}
366370

367-
rc = SQLITE_OK;
371+
#if SQLITE_VERSION_NUMBER >= 3020000
372+
/* Check if the statement has been run (that is, sqlite3_step() has been
373+
* called at least once). Third parameter is non-zero in order to reset the
374+
* run count. */
375+
if (sqlite3_stmt_status(stmt, SQLITE_STMTSTATUS_RUN, 1) == 0) {
376+
return;
377+
}
378+
#endif
368379

369-
if (self->in_use && self->st) {
370-
Py_BEGIN_ALLOW_THREADS
371-
rc = sqlite3_reset(self->st);
372-
Py_END_ALLOW_THREADS
380+
int rc;
381+
Py_BEGIN_ALLOW_THREADS
382+
rc = sqlite3_reset(stmt);
383+
Py_END_ALLOW_THREADS
373384

374-
if (rc == SQLITE_OK) {
375-
self->in_use = 0;
376-
}
385+
if (rc == SQLITE_OK) {
386+
self->in_use = 0;
377387
}
378-
379-
return rc;
380388
}
381389

382390
void pysqlite_statement_mark_dirty(pysqlite_Statement* self)

Modules/_sqlite/statement.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void pysqlite_statement_bind_parameters(pysqlite_state *state,
4444
pysqlite_Statement *self,
4545
PyObject *parameters);
4646

47-
int pysqlite_statement_reset(pysqlite_Statement* self);
47+
void pysqlite_statement_reset(pysqlite_Statement *self);
4848
void pysqlite_statement_mark_dirty(pysqlite_Statement* self);
4949

5050
int pysqlite_statement_setup_types(PyObject *module);

0 commit comments

Comments
 (0)