From d60210d318f3ed5686da39882bd867f8685bb0db Mon Sep 17 00:00:00 2001 From: bswck Date: Wed, 15 Oct 2025 17:43:50 +0200 Subject: [PATCH 1/2] Mention exception chaining in `contextmanager.__exit__` --- Doc/library/stdtypes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 2c5b721093d5c0..552e008f58891d 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5357,8 +5357,8 @@ before the statement body is executed and exited when the statement ends: to suppress the exception and continue execution with the statement immediately following the :keyword:`!with` statement. Otherwise the exception continues propagating after this method has finished executing. Exceptions that occur - during execution of this method will replace any exception that occurred in the - body of the :keyword:`!with` statement. + during execution of this method will be propagated on top of any exception that + occurred in the body of the :keyword:`!with` statement. The exception passed in should never be reraised explicitly - instead, this method should return a false value to indicate that the method completed From c9ac23c7dbd7c79838b95ac3356363874bceb71e Mon Sep 17 00:00:00 2001 From: johnslavik Date: Mon, 15 Dec 2025 22:51:06 +0100 Subject: [PATCH 2/2] Reword to contain a link to `__context__` --- Doc/library/stdtypes.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 552e008f58891d..a0a4b11a2c6ac9 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5356,9 +5356,11 @@ before the statement body is executed and exited when the statement ends: Returning a true value from this method will cause the :keyword:`with` statement to suppress the exception and continue execution with the statement immediately following the :keyword:`!with` statement. Otherwise the exception continues - propagating after this method has finished executing. Exceptions that occur - during execution of this method will be propagated on top of any exception that - occurred in the body of the :keyword:`!with` statement. + propagating after this method has finished executing. + + If this method raises an exception while handling an earlier exception from the + :keyword:`with` block, the new exception is raised, and the original exception + is stored in its :attr:`~BaseException.__context__` attribute. The exception passed in should never be reraised explicitly - instead, this method should return a false value to indicate that the method completed