File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33
44import asyncio
55import contextvars
6-
6+ import contextlib
77from asyncio import taskgroups
88import unittest
99
@@ -741,6 +741,35 @@ async def coro2(g):
741741
742742 self .assertEqual (get_error_types (cm .exception ), {ZeroDivisionError })
743743
744+ async def test_taskgroup_context_manager_exit_raises (self ):
745+ # See https://github.com/python/cpython/issues/95289
746+ class CustomException (Exception ):
747+ pass
748+
749+ async def raise_exc ():
750+ raise CustomException
751+
752+ @contextlib .asynccontextmanager
753+ async def database ():
754+ try :
755+ yield
756+ finally :
757+ raise CustomException
758+
759+ async def main ():
760+ task = asyncio .current_task ()
761+ try :
762+ async with taskgroups .TaskGroup () as tg :
763+ async with database ():
764+ tg .create_task (raise_exc ())
765+ await asyncio .sleep (1 )
766+ except* CustomException :
767+ self .assertEqual (task .cancelling (), 0 )
768+ else :
769+ self .fail ('CustomException not raised' )
770+
771+ await asyncio .create_task (main ())
772+
744773
745774if __name__ == "__main__" :
746775 unittest .main ()
You can’t perform that action at this time.
0 commit comments