Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lib/sqlite3/test/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,48 @@ def CheckCommitCursorReset(self):
counter += 1
self.assertEqual(counter, 3, "should have returned exactly three rows")

def CheckActionAfterRollbackCursorReset(self):
"""
Similar to `CheckCommitCursorReset`, `Connection::rollback()` resets
statements and cursors, which could cause statements to be reset again
when they shouldn't be.

See issue33376 for more details.
"""
con = sqlite.connect(":memory:")
con.executescript("""
create table t(c);
insert into t values(0);
insert into t values(1);
insert into t values(2);
""")

self.assertEqual(con.isolation_level, "")

curs = con.cursor()
curs.execute("BEGIN TRANSACTION")
curs.execute("select c from t")
con.rollback()

# Reusing the same statement from the statement cache, which has been
# reset by the rollback above.
gen = con.execute("select c from t")

# Would previously cause a spurious reset of the statement.
del curs

counter = 0
for i, row in enumerate(gen):
with self.subTest(i=i, row=row):
if counter == 0:
self.assertEqual(row[0], 0)
elif counter == 1:
self.assertEqual(row[0], 1)
elif counter == 2:
self.assertEqual(row[0], 2)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not self.assertEqual(row[0], counter)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the previous, very similar unit test. I don't have particularly strong opinions either way, but I generally value correctness > consistency > style.

I can change it if others feel strongly about this.

counter += 1
self.assertEqual(counter, 3, "should have returned exactly three rows")

def CheckBpo31770(self):
"""
The interpreter shouldn't crash in case Cursor.__init__() is called
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes spurious statement resets after a connection rollback in some
situations.
1 change: 1 addition & 0 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset
cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
if ((PyObject*)cursor != Py_None) {
cursor->reset = 1;
Py_CLEAR(cursor->statement);
}
}
}
Expand Down