Skip to content

Commit

Permalink
Changed handling of a single exception raised in exception group hand…
Browse files Browse the repository at this point in the history
…lers to match Python 3.11.4

Fixes #64.
  • Loading branch information
agronholm committed Jul 2, 2023
1 parent 8a104eb commit 821d5eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Version history

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Changed handling of exceptions in exception group handler callbacks to not wrap a
single exception in an exception group, as per
`CPython issue 103590 <https://github.com/python/cpython/issues/103590>`_

**1.1.1**

- Worked around
Expand Down
5 changes: 4 additions & 1 deletion src/exceptiongroup/_catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __exit__(

return False

def handle_exception(self, exc: BaseException) -> BaseExceptionGroup | None:
def handle_exception(self, exc: BaseException) -> BaseException | None:
excgroup: BaseExceptionGroup | None
if isinstance(exc, BaseExceptionGroup):
excgroup = exc
Expand All @@ -57,6 +57,9 @@ def handle_exception(self, exc: BaseException) -> BaseExceptionGroup | None:
break

if new_exceptions:
if len(new_exceptions) == 1:
return new_exceptions[0]

if excgroup:
new_exceptions.append(excgroup)

Expand Down
8 changes: 1 addition & 7 deletions tests/test_catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,9 @@ def test_catch_handler_raises():
def handler(exc):
raise RuntimeError("new")

try:
with pytest.raises(RuntimeError, match="new"):
with catch({(ValueError, ValueError): handler}):
raise ExceptionGroup("booboo", [ValueError("bar")])
except ExceptionGroup as exc:
assert exc.message == ""
assert len(exc.exceptions) == 1
assert isinstance(exc.exceptions[0], RuntimeError)
else:
pytest.fail("Did not raise an ExceptionGroup")


def test_catch_subclass():
Expand Down
8 changes: 1 addition & 7 deletions tests/test_catch_py311.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,11 @@ def test_catch_full_match():


def test_catch_handler_raises():
try:
with pytest.raises(RuntimeError, match="new"):
try:
raise ExceptionGroup("booboo", [ValueError("bar")])
except* ValueError:
raise RuntimeError("new")
except ExceptionGroup as exc:
assert exc.message == ""
assert len(exc.exceptions) == 1
assert isinstance(exc.exceptions[0], RuntimeError)
else:
pytest.fail("Did not raise an ExceptionGroup")


def test_catch_subclass():
Expand Down

0 comments on commit 821d5eb

Please sign in to comment.