Skip to content

Commit

Permalink
00384: pythongh-94028: Clear and reset sqlite3 statements properly in…
Browse files Browse the repository at this point in the history
… cursor iternext (pythonGH-94042)
  • Loading branch information
erlend-aasland authored and hrnciar committed Jun 24, 2022
1 parent e6a1ac7 commit a3b9665
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Lib/test/test_sqlite3/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,45 @@ def test_rollback_cursor_consistency(self):
con.rollback()
self.assertEqual(cur.fetchall(), [(1,), (2,), (3,)])

def test_multiple_cursors_and_iternext(self):
# gh-94028: statements are cleared and reset in cursor iternext.

# Provoke the gh-94028 by using a cursor cache.
CURSORS = {}
def sql(cx, sql, *args):
cu = cx.cursor()
cu.execute(sql, args)
CURSORS[id(sql)] = cu
return cu

self.con1.execute("create table t(t)")
sql(self.con1, "insert into t values (?), (?), (?)", "u1", "u2", "u3")
self.con1.commit()

# On second connection, verify rows are visible, then delete them.
count = sql(self.con2, "select count(*) from t").fetchone()[0]
self.assertEqual(count, 3)
changes = sql(self.con2, "delete from t").rowcount
self.assertEqual(changes, 3)
self.con2.commit()

# Back in original connection, create 2 new users.
sql(self.con1, "insert into t values (?)", "u4")
sql(self.con1, "insert into t values (?)", "u5")

# The second connection cannot see uncommitted changes.
count = sql(self.con2, "select count(*) from t").fetchone()[0]
self.assertEqual(count, 0)

# First connection can see its own changes.
count = sql(self.con1, "select count(*) from t").fetchone()[0]
self.assertEqual(count, 2)

# The second connection can now see the changes.
self.con1.commit()
count = sql(self.con2, "select count(*) from t").fetchone()[0]
self.assertEqual(count, 2)


class RollbackTests(unittest.TestCase):
"""bpo-44092: sqlite3 now leaves it to SQLite to resolve rollback issues"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a regression in the :mod:`sqlite3` where statement objects were not
properly cleared and reset after use in cursor iters. The regression was
introduced by PR 27884 in Python 3.11a1. Patch by Erlend E. Aasland.
3 changes: 3 additions & 0 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,10 +1126,13 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
int rc = stmt_step(stmt);
if (rc == SQLITE_DONE) {
(void)stmt_reset(self->statement);
Py_CLEAR(self->statement);
}
else if (rc != SQLITE_ROW) {
(void)_pysqlite_seterror(self->connection->state,
self->connection->db);
(void)stmt_reset(self->statement);
Py_CLEAR(self->statement);
Py_DECREF(row);
return NULL;
}
Expand Down

0 comments on commit a3b9665

Please sign in to comment.