Skip to content

Commit fc16b60

Browse files
authored
Merge pull request #3334 from A5rocks/improved-safe-generator
Wrap exception groups less when raising in `as_safe_channel`
2 parents 0f975c3 + d09e942 commit fc16b60

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

newsfragments/3332.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Decrease indentation for exception groups raised in `trio.as_safe_channel`.

src/trio/_channel.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,13 @@ async def context_manager(
556556
except MultipleExceptionError:
557557
# In case user has except* we make it possible for them to handle the
558558
# exceptions.
559-
raise BaseExceptionGroup(
560-
"Encountered exception during cleanup of generator object, as well as exception in the contextmanager body - unable to unwrap.",
561-
[eg],
562-
) from None
559+
if sys.version_info >= (3, 11):
560+
eg.add_note(
561+
"Encountered exception during cleanup of generator object, as "
562+
"well as exception in the contextmanager body - unable to unwrap."
563+
)
564+
565+
raise eg from None
563566

564567
async def _move_elems_to_channel(
565568
agen: AsyncGenerator[T, None],

src/trio/_tests/test_channel.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,17 +519,20 @@ async def agen(events: list[str]) -> AsyncGenerator[int]:
519519

520520
events: list[str] = []
521521
with RaisesGroup(
522-
RaisesGroup(
523-
Matcher(ValueError, match="^agen$"),
524-
Matcher(TypeError, match="^iterator$"),
525-
),
526-
match=r"^Encountered exception during cleanup of generator object, as well as exception in the contextmanager body - unable to unwrap.$",
527-
):
522+
Matcher(ValueError, match="^agen$"),
523+
Matcher(TypeError, match="^iterator$"),
524+
) as g:
528525
async with agen(events) as recv_chan:
529526
async for i in recv_chan: # pragma: no branch
530527
assert i == 1
531528
raise TypeError("iterator")
532529

530+
if sys.version_info >= (3, 11):
531+
assert g.value.__notes__ == [
532+
"Encountered exception during cleanup of generator object, as "
533+
"well as exception in the contextmanager body - unable to unwrap."
534+
]
535+
533536
assert events == ["GeneratorExit()", "finally"]
534537

535538

@@ -734,8 +737,12 @@ async def agen() -> AsyncGenerator[None]:
734737
while True:
735738
yield
736739

737-
for _ in range(10): # 20% missed-alarm rate, so run ten times
738-
async with agen() as chan:
739-
with pytest.raises(trio.ClosedResourceError):
740-
async for _ in chan:
741-
pass
740+
async with agen() as chan:
741+
with pytest.raises(trio.ClosedResourceError):
742+
async for _ in chan:
743+
pass
744+
745+
# This is necessary to ensure that `chan` has been sent
746+
# to. Otherwise, this test sometimes passes on a broken
747+
# version of trio.
748+
await trio.testing.wait_all_tasks_blocked()

0 commit comments

Comments
 (0)