@@ -625,3 +625,73 @@ async def agen(events: list[str]) -> AsyncGenerator[None]:
625625 events .append ("body cancel" )
626626 raise
627627 assert events == ["body cancel" , "agen cancel" ]
628+
629+
630+ async def test_as_safe_channel_genexit_exception_group () -> None :
631+ @as_safe_channel
632+ async def agen () -> AsyncGenerator [None ]:
633+ try :
634+ async with trio .open_nursery ():
635+ yield
636+ except BaseException as e :
637+ assert pytest .RaisesGroup (GeneratorExit ).matches (e ) # noqa: PT017
638+ raise
639+
640+ async with agen () as g :
641+ async for _ in g :
642+ break
643+
644+
645+ async def test_as_safe_channel_does_not_suppress_nested_genexit () -> None :
646+ @as_safe_channel
647+ async def agen () -> AsyncGenerator [None ]:
648+ yield
649+
650+ with pytest .RaisesGroup (GeneratorExit ):
651+ async with agen () as g , trio .open_nursery ():
652+ await g .receive () # this is for coverage reasons
653+ raise GeneratorExit
654+
655+
656+ async def test_as_safe_channel_genexit_filter () -> None :
657+ async def wait_then_raise () -> None :
658+ try :
659+ await trio .sleep_forever ()
660+ except trio .Cancelled :
661+ raise ValueError from None
662+
663+ @as_safe_channel
664+ async def agen () -> AsyncGenerator [None ]:
665+ async with trio .open_nursery () as nursery :
666+ nursery .start_soon (wait_then_raise )
667+ yield
668+
669+ with pytest .RaisesGroup (ValueError ):
670+ async with agen () as g :
671+ async for _ in g :
672+ break
673+
674+
675+ async def test_as_safe_channel_swallowing_extra_exceptions () -> None :
676+ async def wait_then_raise (ex : type [BaseException ]) -> None :
677+ try :
678+ await trio .sleep_forever ()
679+ except trio .Cancelled :
680+ raise ex from None
681+
682+ @as_safe_channel
683+ async def agen (ex : type [BaseException ]) -> AsyncGenerator [None ]:
684+ async with trio .open_nursery () as nursery :
685+ nursery .start_soon (wait_then_raise , ex )
686+ nursery .start_soon (wait_then_raise , GeneratorExit )
687+ yield
688+
689+ with pytest .RaisesGroup (AssertionError ):
690+ async with agen (GeneratorExit ) as g :
691+ async for _ in g :
692+ break
693+
694+ with pytest .RaisesGroup (ValueError , AssertionError ):
695+ async with agen (ValueError ) as g :
696+ async for _ in g :
697+ break
0 commit comments