@@ -1042,20 +1042,29 @@ async def iter_one():
10421042 asyncio .create_task (iter_one ())
10431043 return status
10441044
1045- def test_shutdown_asyncgens_reports_cancelled_error (self ):
1046- # gh-150866: shutdown_asyncgens silently swallowed
1047- # CancelledError raised during aclose() because the check was
1048- # isinstance(result, Exception), but CancelledError inherits
1045+ def test_shutdown_asyncgens_reports_base_exceptions (self ):
1046+ # gh-150866: shutdown_asyncgens silently swallowed exceptions that
1047+ # don't inherit from Exception raised during aclose() because the
1048+ # check was isinstance(result, Exception), but CancelledError inherits
10491049 # from BaseException.
10501050 self .loop ._process_events = mock .Mock ()
10511051 self .loop ._write_to_self = mock .Mock ()
10521052
1053+ class MyBaseException (BaseException ):
1054+ pass
1055+
10531056 async def agen_cancel ():
10541057 try :
10551058 yield 1
10561059 finally :
10571060 raise asyncio .CancelledError ("agen got cancelled during cleanup" )
10581061
1062+ async def agen_base ():
1063+ try :
1064+ yield 1
1065+ finally :
1066+ raise MyBaseException ("base exc during cleanup" )
1067+
10591068 async def agen_value_error ():
10601069 try :
10611070 yield 1
@@ -1065,21 +1074,27 @@ async def agen_value_error():
10651074 caught = []
10661075
10671076 def handler (loop , context ):
1068- caught .append (context ['message ' ])
1077+ caught .append (context ['exception ' ])
10691078
10701079 async def main ():
10711080 loop = asyncio .get_running_loop ()
10721081 loop .set_exception_handler (handler )
10731082
10741083 g1 = agen_cancel ()
1075- g2 = agen_value_error ()
1084+ g2 = agen_base ()
1085+ g3 = agen_value_error ()
10761086 await g1 .__anext__ ()
10771087 await g2 .__anext__ ()
1088+ await g3 .__anext__ ()
10781089
10791090 await loop .shutdown_asyncgens ()
10801091
10811092 self .loop .run_until_complete (main ())
1082- self .assertEqual (len (caught ), 2 )
1093+ self .assertEqual (len (caught ), 3 )
1094+ self .assertEqual (
1095+ {type (exc ) for exc in caught },
1096+ {asyncio .CancelledError , MyBaseException , ValueError },
1097+ )
10831098
10841099 def test_asyncgen_finalization_by_gc (self ):
10851100 # Async generators should be finalized when garbage collected.
0 commit comments