gh-92118: fix traceback of exceptions propagated from inside a contextlib.contextmanager (GH-92202)

This commit is contained in:
Irit Katriel 2022-05-04 14:40:47 -04:00 committed by GitHub
parent f8a2fab212
commit e61330b44f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View file

@ -161,6 +161,7 @@ class _GeneratorContextManager(
except RuntimeError as exc:
# Don't re-raise the passed in exception. (issue27122)
if exc is value:
exc.__traceback__ = traceback
return False
# Avoid suppressing if a StopIteration exception
# was passed to throw() and later wrapped into a RuntimeError
@ -172,6 +173,7 @@ class _GeneratorContextManager(
isinstance(value, StopIteration)
and exc.__cause__ is value
):
exc.__traceback__ = traceback
return False
raise
except BaseException as exc:
@ -183,6 +185,7 @@ class _GeneratorContextManager(
# and the __exit__() protocol.
if exc is not value:
raise
exc.__traceback__ = traceback
return False
raise RuntimeError("generator didn't stop after throw()")