Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bare raise in catch behaves differently to bare raise in native except* #70

Closed
graingert opened this issue Jul 13, 2023 · 1 comment · Fixed by #71
Closed

bare raise in catch behaves differently to bare raise in native except* #70

graingert opened this issue Jul 13, 2023 · 1 comment · Fixed by #71

Comments

@graingert
Copy link

with the backport:

import sys
import exceptiongroup


def raise_exc():
    raise ExceptionGroup("bad", [ValueError(), ValueError(), TypeError()])

def handle(eg):
    raise

def noop(*args, **kwargs):
    pass

with exceptiongroup.catch({ValueError: handle, TypeError: noop}):
    raise_exc()

I get:

  + Exception Group Traceback (most recent call last):
  |   File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/exceptiongroup/_catch.py", line 52, in handle_exception
  |     handler(matched)
  |   File "/home/graingert/projects/dask/backport.py", line 15, in <module>
  |     raise_exc()
  |   File "/home/graingert/projects/dask/backport.py", line 6, in raise_exc
  |     raise ExceptionGroup("bad", [ValueError(), ValueError(), TypeError()])
  | ExceptionGroup: bad (3 sub-exceptions)
  +-+---------------- 1 ----------------
    | ValueError
    +---------------- 2 ----------------
    | ValueError
    +---------------- 3 ----------------
    | TypeError
    +------------------------------------

with the native code:

import sys
import exceptiongroup


def raise_exc():
    raise ExceptionGroup("bad", [ValueError(), ValueError(), TypeError()])

try:
    raise_exc()
except* ValueError as eg:
    raise
except* TypeError:
    pass

I get:

  + Exception Group Traceback (most recent call last):
  |   File "/home/graingert/projects/dask/native.py", line 9, in <module>
  |     raise_exc()
  |   File "/home/graingert/projects/dask/native.py", line 6, in raise_exc
  |     raise ExceptionGroup("bad", [ValueError(), ValueError(), TypeError()])
  | ExceptionGroup: bad (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | ValueError
    +---------------- 2 ----------------
    | ValueError
    +------------------------------------
@agronholm
Copy link
Owner

This is caused by the original exception present in wherever sys.exc_info() gets the exception info from, as that's the exception in flight that raise re-raises. The only way to fix this is to raise the split exception group ourselves, letting the callback handle that then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants