Skip to content

Commit fe47619

Browse files
erlend-aaslandmiss-islington
authored andcommitted
pythongh-61162: Clarify sqlite3 connection context manager docs (pythonGH-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 <[email protected]> Co-authored-by: Stanley <[email protected]> Automerge-Triggered-By: GH:erlend-aasland (cherry picked from commit 8e08978) Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 05f3367 commit fe47619

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
@@ -1420,13 +1420,27 @@ case-insensitively by name:
14201420
.. literalinclude:: ../includes/sqlite3/rowclass.py
14211421

14221422

1423+
.. _sqlite3-connection-context-manager:
1424+
14231425
Using the connection as a context manager
14241426
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14251427

1426-
Connection objects can be used as context managers
1427-
that automatically commit or rollback transactions. In the event of an
1428-
exception, the transaction is rolled back; otherwise, the transaction is
1429-
committed:
1428+
A :class:`Connection` object can be used as a context manager that
1429+
automatically commits or rolls back open transactions when leaving the body of
1430+
the context manager.
1431+
If the body of the :keyword:`with` statement finishes without exceptions,
1432+
the transaction is committed.
1433+
If this commit fails,
1434+
or if the body of the ``with`` statement raises an uncaught exception,
1435+
the transaction is rolled back.
1436+
1437+
If there is no open transaction upon leaving the body of the ``with`` statement,
1438+
the context manager is a no-op.
1439+
1440+
.. note::
1441+
1442+
The context manager neither implicitly opens a new transaction
1443+
nor closes the connection.
14301444

14311445
.. literalinclude:: ../includes/sqlite3/ctx_manager.py
14321446

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)