Skip to content

Commit cdf3689

Browse files
gh-61162: Clarify sqlite3 connection context manager docs (GH-93890)
Explicitly note that transactions are only closed if there is an open transation at `__exit__`, and that transactions are not implicitly opened during `__enter__`. Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM> Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com> Automerge-Triggered-By: GH:erlend-aasland (cherry picked from commit 8e08978) Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@protonmail.com>
1 parent c4440e6 commit cdf3689

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Doc/library/sqlite3.rst

+18-4
Original file line numberDiff line numberDiff line change
@@ -1188,13 +1188,27 @@ case-insensitively by name:
11881188
.. literalinclude:: ../includes/sqlite3/rowclass.py
11891189

11901190

1191+
.. _sqlite3-connection-context-manager:
1192+
11911193
Using the connection as a context manager
11921194
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11931195

1194-
Connection objects can be used as context managers
1195-
that automatically commit or rollback transactions. In the event of an
1196-
exception, the transaction is rolled back; otherwise, the transaction is
1197-
committed:
1196+
A :class:`Connection` object can be used as a context manager that
1197+
automatically commits or rolls back open transactions when leaving the body of
1198+
the context manager.
1199+
If the body of the :keyword:`with` statement finishes without exceptions,
1200+
the transaction is committed.
1201+
If this commit fails,
1202+
or if the body of the ``with`` statement raises an uncaught exception,
1203+
the transaction is rolled back.
1204+
1205+
If there is no open transaction upon leaving the body of the ``with`` statement,
1206+
the context manager is a no-op.
1207+
1208+
.. note::
1209+
1210+
The context manager neither implicitly opens a new transaction
1211+
nor closes the connection.
11981212

11991213
.. literalinclude:: ../includes/sqlite3/ctx_manager.py
12001214

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify :mod:`sqlite3` behavior when :ref:`sqlite3-connection-context-manager`.

0 commit comments

Comments
 (0)