Skip to content
Draft
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
22 changes: 17 additions & 5 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,12 +1905,24 @@ def tearDown(self):
unlink(TESTFN)

def test_ctx_mgr_rollback_if_commit_failed(self):
# bpo-27334: ctx manager does not rollback if commit fails
# Ensure that context manager does not rollback if commit fails.
# See https://github.com/python/cpython/issues/71521.

wait_messages = ["database is locked"]
if sqlite.sqlite_version_info >= (3, 45):
# Around SQLite 3.45 or later, a read-only transaction plus
# a user-defined function do not block concurrent writers,
# so we allow the parent process to send "no error".
#
# See https://github.com/python/cpython/issues/143263.
wait_messages.append("no error")

SCRIPT = f"""if 1:
import sqlite3
def wait():
print("started")
assert "database is locked" in input()
line = input()
assert any(message in line for message in {wait_messages})

cx = sqlite3.connect("{TESTFN}", timeout={self.CONNECTION_TIMEOUT})
cx.create_function("wait", 0, wait)
Expand All @@ -1921,11 +1933,11 @@ def wait():
cx.executescript('''
-- start a transaction and wait for parent
begin transaction;
select * from t;
insert into t values("ok");
select wait();
rollback;

-- start a new transaction; would fail if parent holds lock
-- start a new transaction; may fail if parent holds lock
begin transaction;
select * from t;
rollback;
Expand All @@ -1948,7 +1960,7 @@ def wait():
self.assertEqual("started", proc.stdout.readline().strip())

cx = sqlite.connect(TESTFN, timeout=self.CONNECTION_TIMEOUT)
try: # context manager should correctly release the db lock
try: # context manager should correctly release the db lock (if any)
with cx:
cx.execute("insert into t values('test')")
except sqlite.OperationalError as exc:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Relax conditions in ``test_ctx_mgr_rollback_if_commit_failed`` to make the
test pass with SQLite 3.45 and later. Patch by Bénédikt Tran.
Loading