agronholm / agronholm/exceptiongroup

Raising `exc_grp.subgroup(...)` behaves differently in a `catch` handler vs. in a native `except*` handler

オープン
#157 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug
主要言語
Python
スター
49
フォーク
23
PR マージ指標
30日以内にマージされた PR はありません

説明

### Things to check first

- [x] I have searched the existing issues and didn't find my bug already reported there

- [x] I have checked that my bug is still present in the latest release

### Exceptiongroup version

v1.3.1

### Python version

CPython 3.11.15, CPython 3.14.5

### What happened?

Raising `exc_grp.subgroup(...)` behaves differently in a `catch` handler vs. in a native `except*` handler. CPython raises a group with a `__cause__` of the old group, while `catch` raises a bare exception with a `__cause__` of the old group.

### How can we reproduce the bug?

```python
from __future__ import annotations

import sys

import exceptiongroup

def foo() -> RuntimeError:
try:
try:
raise ValueError("a")
except ValueError as exc:
raise RuntimeError("b") from exc
except RuntimeError as exc:
return exc

def bar() -> RuntimeError:
try:
try:
raise ValueError("c")
except ValueError as exc:
raise RuntimeError("d") from exc
except RuntimeError as exc:
return exc

def raise_grp() -> None:
try:
raise ValueError("asdf")
except ValueError as exc:
raise ExceptionGroup("", [foo(), bar()]) from exc

def handle_RuntimeErrors(exc_grp: ExceptionGroup[RuntimeError], /) -> None:
_, rest = exc_grp.split(lambda exc: str(exc) == "b")
if rest:
raise rest
# These also behave differently with catch than with except*:
# raise rest from None
# raise rest from exc_grp

assert len(sys.argv) >= 2
match sys.argv[1]:
case "native":
try:
raise_grp()
except* RuntimeError as exc_grp:
handle_RuntimeErrors(exc_grp)
case "backport":
with exceptiongroup.catch({RuntimeError: handle_RuntimeErrors}):
raise_grp()
case _:
raise NotImplementedError()
```

Output:

(This is on 3.11, but it's well above 3.11.4 (see 821d5ebaadfd0b5b16c785a942f69e89933217bf). The behavior is the same on 3.11.15 and 3.14.5.)

```console
$ uv run --no-project --isolated --exclude-newer=2026-06-01T00:00:00Z -p 3.11.15 --with exceptiongroup==1.3.1 python eg.py native
Traceback (most recent call last):
File "/home/ganden/vc/exceptiongroup/eg.py", line 30, in raise_grp
raise ValueError("asdf")
ValueError: asdf

The above exception was the direct cause of the following exception:

+ Exception Group Traceback (most recent call last):
| File "/home/ganden/vc/exceptiongroup/eg.py", line 50, in
| handle_RuntimeErrors(exc_grp)
| File "/home/ganden/vc/exceptiongroup/eg.py", line 38, in handle_RuntimeErrors
| raise rest
| File "/home/ganden/vc/exceptiongroup/eg.py", line 48, in
| raise_grp()
| File "/home/ganden/vc/exceptiongroup/eg.py", line 32, in raise_grp
| raise ExceptionGroup("", [foo(), bar()]) from exc
| ExceptionGroup: (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/home/ganden/vc/exceptiongroup/eg.py", line 21, in bar
| raise ValueError("c")
| ValueError: c
|
| The above exception was the direct cause of the following exception:
|
| Traceback (most recent call last):
| File "/home/ganden/vc/exceptiongroup/eg.py", line 23, in bar
| raise RuntimeError("d") from exc
| RuntimeError: d
+------------------------------------
$
$ uv run --no-project --isolated --exclude-newer=2026-06-01T00:00:00Z -p 3.11.15 --with exceptiongroup==1.3.1 python eg.py backport
Traceback (most recent call last):
File "/home/ganden/vc/exceptiongroup/eg.py", line 30, in raise_grp
raise ValueError("asdf")
ValueError: asdf

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/ganden/vc/exceptiongroup/eg.py", line 52, in
with exceptiongroup.catch({RuntimeError: handle_RuntimeErrors}):
File "/home/ganden/.cache/uv/archive-v0/12KxN_vHBN-mZJyS/lib/python3.11/site-packages/exceptiongroup/_catch.py", line 39, in __exit__
raise unhandled from exc.__cause__
File "/home/ganden/vc/exceptiongroup/eg.py", line 23, in bar
raise RuntimeError("d") from exc
RuntimeError: d
```

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。