Skip to content

Commit 8e08978

Browse files
authoredJun 19, 2022
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
1 parent 6446592 commit 8e08978

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

14321432

1433+
.. _sqlite3-connection-context-manager:
1434+
14331435
Using the connection as a context manager
14341436
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14351437

1436-
Connection objects can be used as context managers
1437-
that automatically commit or rollback transactions. In the event of an
1438-
exception, the transaction is rolled back; otherwise, the transaction is
1439-
committed:
1438+
A :class:`Connection` object can be used as a context manager that
1439+
automatically commits or rolls back open transactions when leaving the body of
1440+
the context manager.
1441+
If the body of the :keyword:`with` statement finishes without exceptions,
1442+
the transaction is committed.
1443+
If this commit fails,
1444+
or if the body of the ``with`` statement raises an uncaught exception,
1445+
the transaction is rolled back.
1446+
1447+
If there is no open transaction upon leaving the body of the ``with`` statement,
1448+
the context manager is a no-op.
1449+
1450+
.. note::
1451+
1452+
The context manager neither implicitly opens a new transaction
1453+
nor closes the connection.
14401454

14411455
.. literalinclude:: ../includes/sqlite3/ctx_manager.py
14421456

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)
Please sign in to comment.