8000 gh-103590: do not wrap a single exception raised from a try-except* by iritkatriel · Pull Request #103665 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103590: do not wrap a single exception raised from a try-except* #103665

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

Merged
merged 10 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ Other Language Changes
wrapped by a :exc:`RuntimeError`. Context information is added to the
exception as a :pep:`678` note. (Contributed by Irit Katriel in :gh:`77757`.)

* When a ``try-except*`` construct handles the entire :exc:`ExceptionGroup`
and raises one other exception, that exception is no longer wrapped in an
:exc:`ExceptionGroup`. (Contributed by Irit Katriel in :gh:`103590`.)


New Modules
===========

Expand Down
44 changes: 20 additions & 24 deletions Lib/test/test_except_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,17 @@ def test_raise_handle_all_raise_one_named(self):
raise orig
except* (TypeError, ValueError) as e:
raise SyntaxError(3)
except BaseException as e:
except SyntaxError as e:
exc = e

self.assertExceptionIsLike(
exc, ExceptionGroup("", [SyntaxError(3)]))
self.assertExceptionIsLike(exc, SyntaxError(3))

self.assertExceptionIsLike(
exc.exceptions[0].__context__,
exc.__context__,
ExceptionGroup("eg", [TypeError(1), ValueError(2)]))

self.assertMetadataNotEqual(orig, exc)
self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
self.assertMetadataEqual(orig, exc.__context__)

def test_raise_handle_all_raise_one_unnamed(self):
orig = ExceptionGroup("eg", [TypeError(1), ValueError(2)])
Expand All @@ -638,18 +637,17 @@ def test_raise_handle_all_raise_one_unnamed(self):
raise orig
except* (TypeError, ValueError) as e:
raise SyntaxError(3)
except ExceptionGroup as e:
except SyntaxError as e:
exc = e

self.assertExceptionIsLike(
exc, ExceptionGroup("", [SyntaxError(3)]))
self.assertExceptionIsLike(exc, SyntaxError(3))

self.assertExceptionIsLike(
exc.exceptions[0].__context__,
exc.__context__,
ExceptionGroup("eg", [TypeError(1), ValueError(2)]))

self.assertMetadataNotEqual(orig, exc)
self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
self.assertMetadataEqual(orig, exc.__context__)

def test_raise_handle_all_raise_two_named(self):
orig = ExceptionGroup("eg", [TypeError(1), ValueError(2)])
Expand Down Expand Up @@ -773,23 +771,22 @@ def test_raise_handle_all_raise_one_named(self):
raise orig
except* (TypeError, ValueError) as e:
raise SyntaxError(3) from e
except BaseException as e:
except SyntaxError as e:
exc = e

self.assertExceptionIsLike(
exc, ExceptionGroup("", [SyntaxError(3)]))
self.assertExceptionIsLike(exc, SyntaxError(3))

self.assertExceptionIsLike(
exc.exceptions[0].__context__,
exc.__context__,
ExceptionGroup("eg", [TypeError(1), ValueError(2)]))

self.assertExceptionIsLike(
exc.exceptions[0].__cause__,
exc.__cause__,
ExceptionGroup("eg", [TypeError(1), ValueError(2)]))

self.assertMetadataNotEqual(orig, exc)
self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
self.assertMetadataEqual(orig, exc.exceptions[0].__cause__)
self.assertMetadataEqual(orig, exc.__context__)
self.assertMetadataEqual(orig, exc.__cause__)

def test_raise_handle_all_raise_one_unnamed(self):
orig = ExceptionGroup("eg", [TypeError(1), ValueError(2)])
Expand All @@ -799,23 +796,22 @@ def test_raise_handle_all_raise_one_unnamed(self):
except* (TypeError, ValueError) as e:
e = sys.exception()
raise SyntaxError(3) from e
except ExceptionGroup as e:
except SyntaxError as e:
exc = e

self.assertExceptionIsLike(
exc, ExceptionGroup("", [SyntaxError(3)]))
self.assertExceptionIsLike(exc, SyntaxError(3))

self.assertExceptionIsLike(
exc.exceptions[0].__context__,
exc.__context__,
ExceptionGroup("eg", [TypeError(1), ValueError(2)]))

self.assertExceptionIsLike(
exc.exceptions[0].__cause__,
exc.__cause__,
ExceptionGroup("eg", [TypeError(1), ValueError(2)]))

self.assertMetadataNotEqual(orig, exc)
self.assertMetadataEqual(orig, exc.exceptions[0].__context__)
self.assertMetadataEqual(orig, exc.exceptions[0].__cause__)
self.assertMetadataEqual(orig, exc.__context__)
self.assertMetadataEqual(orig, exc.__cause__)

def test_raise_handle_all_raise_two_named(self):
orig = ExceptionGroup("eg", [TypeError(1), ValueError(2)])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not wrap a single exception raised from a ``try-except*`` construct in an :exc:`ExceptionGroup`.
7 changes: 6 additions & 1 deletion Objects/exceptions.c
5143
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,12 @@ _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs)
if (res < 0) {
goto done;
}
result = _PyExc_CreateExceptionGroup("", raised_list);
if (PyList_GET_SIZE(raised_list) > 1) {
result = _PyExc_CreateExceptionGroup("", raised_list);
}
else {
result = Py_NewRef(PyList_GetItem(raised_list, 0));
}
if (result == NULL) {
goto done;
}
Expand Down
0