diff options
author | John Belmonte <john@neggie.net> | 2021-10-05 06:37:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-05 06:37:24 (GMT) |
commit | 7c2a040a10654d67ff543a55858ba2d7a9f7eea8 (patch) | |
tree | 30889e3341e6ce955b661a87fdc00d6bcede54d3 /Lib/test/test_contextlib_async.py | |
parent | e9ce081ec7fe6f45059e1de93952ad53e9c3aa74 (diff) | |
download | cpython-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/test/test_contextlib_async.py')
-rw-r--r-- | Lib/test/test_contextlib_async.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index 9d6854c..43aaf04 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -463,6 +463,41 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase): self.assertIsInstance(inner_exc, ValueError) self.assertIsInstance(inner_exc.__context__, ZeroDivisionError) + @_async_test + async def test_async_exit_exception_explicit_none_context(self): + # Ensure AsyncExitStack chaining matches actual nested `with` statements + # regarding explicit __context__ = None. + + class MyException(Exception): + pass + + @asynccontextmanager + async def my_cm(): + try: + yield + except BaseException: + exc = MyException() + try: + raise exc + finally: + exc.__context__ = None + + @asynccontextmanager + async def my_cm_with_exit_stack(): + async with self.exit_stack() as stack: + await stack.enter_async_context(my_cm()) + yield stack + + for cm in (my_cm, my_cm_with_exit_stack): + with self.subTest(): + try: + async with cm(): + raise IndexError() + except MyException as exc: + self.assertIsNone(exc.__context__) + else: + self.fail("Expected IndexError, but no exception was raised") + if __name__ == '__main__': unittest.main() |