Skip to content
Open
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
30 changes: 18 additions & 12 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,12 @@ def __init__(
uri=True,
check_same_thread=False,
)
self._validate_autocommit()
self.memory = True
self.memory_name = memory_name
elif memory or filename_or_conn == ":memory:":
self.conn = sqlite3.connect(":memory:")
self._validate_autocommit()
self.memory = True
elif isinstance(filename_or_conn, (str, pathlib.Path)):
if recreate and os.path.exists(filename_or_conn):
Expand All @@ -609,22 +611,12 @@ def __init__(
self.conn = sqlite3.connect(":memory:")
raise
self.conn = sqlite3.connect(str(filename_or_conn))
self._validate_autocommit()
else:
if recreate:
raise ValueError("recreate cannot be used with connections, only paths")
self.conn = cast(sqlite3.Connection, filename_or_conn)
# Python 3.12+ autocommit=True/False connections make commit()
# and rollback() behave differently, silently breaking the
# transaction handling used by every write method
autocommit = getattr(self.conn, "autocommit", None)
if autocommit is not None and autocommit != getattr(
sqlite3, "LEGACY_TRANSACTION_CONTROL", -1
):
raise TransactionError(
"sqlite-utils requires a connection that uses the default "
"transaction handling - connections created with "
"autocommit=True or autocommit=False are not supported"
)
self._validate_autocommit()
self._tracer: Tracer | None = tracer
if recursive_triggers:
self.execute("PRAGMA recursive_triggers=on;")
Expand All @@ -635,6 +627,20 @@ def __init__(
pm.hook.prepare_connection(conn=self.conn)
self.strict = strict

def _validate_autocommit(self) -> None:
# Python 3.12+ autocommit=True/False connections make commit()
# and rollback() behave differently, silently breaking the
# transaction handling used by every write method
autocommit = getattr(self.conn, "autocommit", None)
if autocommit is not None and autocommit != getattr(
sqlite3, "LEGACY_TRANSACTION_CONTROL", -1
):
raise TransactionError(
"sqlite-utils requires a connection that uses the default "
"transaction handling - connections created with "
"autocommit=True or autocommit=False are not supported"
)

def __enter__(self):
return self

Expand Down
14 changes: 14 additions & 0 deletions tests/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ def test_autocommit_connections_are_rejected(tmpdir, autocommit):
conn.close()


def test_autocommit_connection_from_path_is_rejected(tmpdir, monkeypatch):
# A patched sqlite3.connect can hand back an autocommit connection even
# when the caller only supplied a path - that branch must reject it too
class AutocommitConnection:
autocommit = True

monkeypatch.setattr(
sqlite3, "connect", lambda *args, **kwargs: AutocommitConnection()
)
with pytest.raises(TransactionError) as excinfo:
Database(str(tmpdir / "test.db"))
assert "autocommit=True or autocommit=False are not supported" in str(excinfo.value)


@pytest.mark.skipif(
sys.version_info < (3, 12),
reason="sqlite3.LEGACY_TRANSACTION_CONTROL requires Python 3.12",
Expand Down
Loading