Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3332.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Decrease indentation for exception groups raised in `trio.as_safe_channel`.
11 changes: 7 additions & 4 deletions src/trio/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,13 @@ async def context_manager(
except MultipleExceptionError:
# In case user has except* we make it possible for them to handle the
# exceptions.
raise BaseExceptionGroup(
"Encountered exception during cleanup of generator object, as well as exception in the contextmanager body - unable to unwrap.",
[eg],
) from None
if sys.version_info >= (3, 11):
eg.add_note(
"Encountered exception during cleanup of generator object, as "
"well as exception in the contextmanager body - unable to unwrap."
)

raise eg from None

async def _move_elems_to_channel(
agen: AsyncGenerator[T, None],
Expand Down
29 changes: 18 additions & 11 deletions src/trio/_tests/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,20 @@ async def agen(events: list[str]) -> AsyncGenerator[int]:

events: list[str] = []
with RaisesGroup(
RaisesGroup(
Matcher(ValueError, match="^agen$"),
Matcher(TypeError, match="^iterator$"),
),
match=r"^Encountered exception during cleanup of generator object, as well as exception in the contextmanager body - unable to unwrap.$",
):
Matcher(ValueError, match="^agen$"),
Matcher(TypeError, match="^iterator$"),
) as g:
async with agen(events) as recv_chan:
async for i in recv_chan: # pragma: no branch
assert i == 1
raise TypeError("iterator")

if sys.version_info >= (3, 11):
assert g.value.__notes__ == [
"Encountered exception during cleanup of generator object, as "
"well as exception in the contextmanager body - unable to unwrap."
]

assert events == ["GeneratorExit()", "finally"]


Expand Down Expand Up @@ -734,8 +737,12 @@ async def agen() -> AsyncGenerator[None]:
while True:
yield

for _ in range(10): # 20% missed-alarm rate, so run ten times
async with agen() as chan:
with pytest.raises(trio.ClosedResourceError):
async for _ in chan:
pass
async with agen() as chan:
with pytest.raises(trio.ClosedResourceError):
async for _ in chan:
pass

# This is necessary to ensure that `chan` has been sent
# to. Otherwise, this test sometimes passes on a broken
# version of trio.
await trio.testing.wait_all_tasks_blocked()
Loading