From 02062c5dc0c457caf9bc00378232fce565aa685e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 25 Jul 2025 10:25:33 -0400 Subject: [PATCH] Drop `except*` usage from `._taskc` unmasker That is from `maybe_raise_from_masking_exc()` thus minimizing us to a single `except BaseException` block with logic branching for the beg vs. `unmask_from` exc cases. Also, - raise val-err when `unmask_from` is not a `tuple`. - tweak the exc-note warning format. - drop all pausing from dev work. --- tractor/trionics/_taskc.py | 72 ++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/tractor/trionics/_taskc.py b/tractor/trionics/_taskc.py index 8809524b..fb743bbc 100644 --- a/tractor/trionics/_taskc.py +++ b/tractor/trionics/_taskc.py @@ -74,8 +74,9 @@ async def maybe_raise_from_masking_exc( raise_unmasked: bool = True, extra_note: str = ( 'This can occurr when,\n' - ' - a `trio.Nursery` scope embeds a `finally:`-block ' - 'which executes a checkpoint!' + '\n' + ' - a `trio.Nursery/CancelScope` embeds a `finally/except:`-block ' + 'which execs an un-shielded checkpoint!' # # ^TODO? other cases? ), @@ -104,62 +105,52 @@ async def maybe_raise_from_masking_exc( individual sub-excs but maintain the eg-parent's form right? ''' + if not isinstance(unmask_from, tuple): + raise ValueError( + f'Invalid unmask_from = {unmask_from!r}\n' + f'Must be a `tuple[Type[BaseException]]`.\n' + ) + from tractor.devx.debug import ( BoxedMaybeException, - pause, ) boxed_maybe_exc = BoxedMaybeException( raise_on_exit=raise_unmasked, ) matching: list[BaseException]|None = None - maybe_eg: ExceptionGroup|None - - if tn: - try: # handle egs - yield boxed_maybe_exc - return - except* unmask_from as _maybe_eg: - maybe_eg = _maybe_eg + try: + yield boxed_maybe_exc + return + except BaseException as _bexc: + bexc = _bexc + if isinstance(bexc, BaseExceptionGroup): matches: ExceptionGroup - matches, _ = maybe_eg.split( - unmask_from - ) - if not matches: - raise + matches, _ = bexc.split(unmask_from) + if matches: + matching = matches.exceptions - matching: list[BaseException] = matches.exceptions - else: - try: # handle non-egs - yield boxed_maybe_exc - return - except unmask_from as _maybe_exc: - maybe_exc = _maybe_exc - matching: list[BaseException] = [ - maybe_exc - ] - - # XXX, only unmask-ed for debuggin! - # TODO, remove eventually.. - except BaseException as _berr: - berr = _berr - await pause(shield=True) - raise berr + elif ( + unmask_from + and + type(bexc) in unmask_from + ): + matching = [bexc] if matching is None: raise masked: list[tuple[BaseException, BaseException]] = [] for exc_match in matching: - if exc_ctx := find_masked_excs( maybe_masker=exc_match, - unmask_from={unmask_from}, + unmask_from=set(unmask_from), ): masked.append((exc_ctx, exc_match)) boxed_maybe_exc.value = exc_match note: str = ( f'\n' - f'^^WARNING^^ the above {exc_ctx!r} was masked by a {unmask_from!r}\n' + f'^^WARNING^^\n' + f'the above {type(exc_ctx)!r} was masked by a {type(exc_match)!r}\n' ) if extra_note: note += ( @@ -171,14 +162,13 @@ async def maybe_raise_from_masking_exc( if type(exc_match) in always_warn_on: log.warning(note) - # await tractor.pause(shield=True) if raise_unmasked: if len(masked) < 2: raise exc_ctx from exc_match - else: - # ?TODO, see above but, possibly unmasking sub-exc - # entries if there are > 1 - await pause(shield=True) + # else: + # # ?TODO, see above but, possibly unmasking sub-exc + # # entries if there are > 1 + # await pause(shield=True) else: raise