Facepalm, fix `raise from` in `collapse_eg()`

I dunno what exactly I was thinking but we definitely don't want to
**ever** raise from the original exc-group, instead always raise from
any original `.__cause__` to be consistent with the embedded src-error's
context.

Also, adjust `maybe_collapse_eg()` to return `False` in the non-single
`.exceptions` case, again don't know what I was trying to do but this
simplifies caller logic and the prior return-semantic had no real
value..

This fixes some final usage in the runtime (namely top level nursery
usage in `._root`/`._runtime`) which was previously causing test suite
failures prior to this fix.
free_threading_prep
Tyler Goodlet 2025-07-07 10:02:27 -04:00
parent b244cf844d
commit f86f4ae48d
1 changed files with 7 additions and 6 deletions

View File

@ -31,7 +31,7 @@ import trio
def maybe_collapse_eg( def maybe_collapse_eg(
beg: BaseExceptionGroup, beg: BaseExceptionGroup,
) -> BaseException: ) -> BaseException|bool:
''' '''
If the input beg can collapse to a single non-eg sub-exception, If the input beg can collapse to a single non-eg sub-exception,
return it instead. return it instead.
@ -40,13 +40,12 @@ def maybe_collapse_eg(
if len(excs := beg.exceptions) == 1: if len(excs := beg.exceptions) == 1:
return excs[0] return excs[0]
return beg return False
@acm @acm
async def collapse_eg( async def collapse_eg(
hide_tb: bool = True, hide_tb: bool = True,
raise_from_src: bool = False,
): ):
''' '''
If `BaseExceptionGroup` raised in the body scope is If `BaseExceptionGroup` raised in the body scope is
@ -61,9 +60,11 @@ async def collapse_eg(
except* BaseException as beg: except* BaseException as beg:
if ( if (
exc := maybe_collapse_eg(beg) exc := maybe_collapse_eg(beg)
) is not beg: ):
from_exc = beg if raise_from_src else None if cause := exc.__cause__:
raise exc from from_exc raise exc from cause
raise exc
raise beg raise beg