summaryrefslogtreecommitdiffstats
path: root/Lib/contextlib.py
diff options
context:
space:
mode:
authorJohn Belmonte <john@neggie.net>2021-10-05 06:37:24 (GMT)
committerGitHub <noreply@github.com>2021-10-05 06:37:24 (GMT)
commit7c2a040a10654d67ff543a55858ba2d7a9f7eea8 (patch)
tree30889e3341e6ce955b661a87fdc00d6bcede54d3 /Lib/contextlib.py
parente9ce081ec7fe6f45059e1de93952ad53e9c3aa74 (diff)
downloadcpython-7c2a040a10654d67ff543a55858ba2d7a9f7eea8.zip
cpython-7c2a040a10654d67ff543a55858ba2d7a9f7eea8.tar.gz
cpython-7c2a040a10654d67ff543a55858ba2d7a9f7eea8.tar.bz2
[3.9] bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089) (GH-28731)
Make enter_context(foo()) / enter_async_context(foo()) equivalent to `[async] with foo()` regarding __context__ when an exception is raised. Previously exceptions would be caught and re-raised with the wrong context when explicitly overriding __context__ with None.. (cherry picked from commit e6d1aa1ac65b6908fdea2c70ec3aa8c4f1dffcb5) Co-authored-by: John Belmonte <john@neggie.net> Automerge-Triggered-By: GH:njsmith
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r--Lib/contextlib.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index f60f0c2..4e8f5f7 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -496,10 +496,10 @@ class ExitStack(_BaseExitStack, AbstractContextManager):
# Context may not be correct, so find the end of the chain
while 1:
exc_context = new_exc.__context__
- if exc_context is old_exc:
+ if exc_context is None or exc_context is old_exc:
# Context is already set correctly (see issue 20317)
return
- if exc_context is None or exc_context is frame_exc:
+ if exc_context is frame_exc:
break
new_exc = exc_context
# Change the end of the chain to point to the exception
@@ -630,10 +630,10 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
# Context may not be correct, so find the end of the chain
while 1:
exc_context = new_exc.__context__
- if exc_context is old_exc:
+ if exc_context is None or exc_context is old_exc:
# Context is already set correctly (see issue 20317)
return
- if exc_context is None or exc_context is frame_exc:
+ if exc_context is frame_exc:
break
new_exc = exc_context
# Change the end of the chain to point to the exception