You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AsyncExitStack.enter_async_context() is mishandling exception __context__ #88760
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Over at the Trio project, we have evidence that AsyncExitStack.enter_async_context(foo()) is not actually equivalent to async with foo() regarding raised exception context.
The symptom is a very long, unhelpful tracebacks because the __context__ of raised exceptions is not set to the expected object.
(From my understanding, Trio has valid use cases for doing this since it wants to control complex exception chaining, and this is beyond the scope of __suppress_context__.)
Neither ExitStack nor AsyncExcitStack are preserving the None context in the case above.
=== with statement ===
Traceback (most recent call last):
File "exit_stack_test.py", line 251, in<module>assertFalse
File "/.../python3.7/contextlib.py", line 130, in__exit__self.gen.throw(type, value, traceback)
File "exit_stack_test.py", line 244, in my_cm
raise exc
MyException
=== enter_context() ===
Traceback (most recent call last):
File "exit_stack_test.py", line 240, in my_cm
yield
File "exit_stack_test.py", line 259, in<module>assertFalse
AssertionError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "exit_stack_test.py", line 259, in<module>assertFalse
File "/.../python3.7/contextlib.py", line 524, in__exit__raise exc_details[1]
File "/.../python3.7/contextlib.py", line 509, in__exit__if cb(*exc_details):
File "/.../python3.7/contextlib.py", line 377, in _exit_wrapper
return cm_exit(cm, exc_type, exc, tb)
File "/.../python3.7/contextlib.py", line 130, in__exit__self.gen.throw(type, value, traceback)
File "exit_stack_test.py", line 244, in my_cm
raise exc
MyException
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Activity
belm0 commentedon Jul 9, 2021
Over at the Trio project, we have evidence that
AsyncExitStack.enter_async_context(foo())is not actually equivalent toasync with foo()regarding raised exception context.The symptom is a very long, unhelpful tracebacks because the __context__ of raised exceptions is not set to the expected object.
python-trio/trio#2001
I can't speak to this solution myself, but njsmith suggests this amendment to contextlib:
belm0 commentedon Jul 12, 2021
demonstrating the difference for async case:
main1, context: None
main2, context: Cancelled
belm0 commentedon Jul 12, 2021
To clarify the problem case, I believe the discrepancy is seen when raising exceptions as follows:
(From my understanding, Trio has valid use cases for doing this since it wants to control complex exception chaining, and this is beyond the scope of __suppress_context__.)
Neither ExitStack nor AsyncExcitStack are preserving the None context in the case above.
===
withstatement ===Traceback (most recent call last): File "exit_stack_test.py", line 251, in <module> assert False File "/.../python3.7/contextlib.py", line 130, in __exit__ self.gen.throw(type, value, traceback) File "exit_stack_test.py", line 244, in my_cm raise exc MyException=== enter_context() ===
Traceback (most recent call last): File "exit_stack_test.py", line 240, in my_cm yield File "exit_stack_test.py", line 259, in <module> assert False AssertionError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "exit_stack_test.py", line 259, in <module> assert False File "/.../python3.7/contextlib.py", line 524, in __exit__ raise exc_details[1] File "/.../python3.7/contextlib.py", line 509, in __exit__ if cb(*exc_details): File "/.../python3.7/contextlib.py", line 377, in _exit_wrapper return cm_exit(cm, exc_type, exc, tb) File "/.../python3.7/contextlib.py", line 130, in __exit__ self.gen.throw(type, value, traceback) File "exit_stack_test.py", line 244, in my_cm raise exc MyExceptionbelm0 commentedon Jul 12, 2021
[reposting the example, with source]
example:
class MyException(Exception):
pass
print('\n=== enter_context() ===')
try:
with ExitStack() as stack:
stack.enter_context(my_cm())
assert False
except BaseException as e:
traceback.print_exc()
output:
===
withstatement ===Traceback (most recent call last): File "exit_stack_test.py", line 251, in <module> assert False File "/.../python3.7/contextlib.py", line 130, in __exit__ self.gen.throw(type, value, traceback) File "exit_stack_test.py", line 244, in my_cm raise exc MyException=== enter_context() ===
Traceback (most recent call last): File "exit_stack_test.py", line 240, in my_cm yield File "exit_stack_test.py", line 259, in <module> assert False AssertionError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "exit_stack_test.py", line 259, in <module> assert False File "/.../python3.7/contextlib.py", line 524, in __exit__ raise exc_details[1] File "/.../python3.7/contextlib.py", line 509, in __exit__ if cb(*exc_details): File "/.../python3.7/contextlib.py", line 377, in _exit_wrapper return cm_exit(cm, exc_type, exc, tb) File "/.../python3.7/contextlib.py", line 130, in __exit__ self.gen.throw(type, value, traceback) File "exit_stack_test.py", line 244, in my_cm raise exc MyExceptionbelm0 commentedon Jul 12, 2021
cc: ncoghlan for help with ExitStack exception context
njsmith commentedon Oct 4, 2021
New changeset e6d1aa1 by John Belmonte in branch 'main':
bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089)
e6d1aa1
miss-islington commentedon Oct 5, 2021
New changeset 872b1e5 by John Belmonte in branch '3.10':
[3.10] bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089) (GH-28730)
872b1e5
miss-islington commentedon Oct 5, 2021
New changeset 7c2a040 by John Belmonte in branch '3.9':
[3.9] bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089) (GH-28731)
7c2a040