diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c011d9bb0..845323644 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -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): @@ -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;") @@ -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 diff --git a/tests/test_constructor.py b/tests/test_constructor.py index 412b66c2c..2fc4dc62a 100644 --- a/tests/test_constructor.py +++ b/tests/test_constructor.py @@ -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",