-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup #103792
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
Changes from all commits
7bc3c15
08fa06c
be30df6
acf95f6
503d9f8
a43ffe4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -441,7 +441,16 @@ def __exit__(self, exctype, excinst, exctb): | |
# exactly reproduce the limitations of the CPython interpreter. | ||
# | ||
# See http://bugs.python.org/issue12029 for more details | ||
return exctype is not None and issubclass(exctype, self._exceptions) | ||
if exctype is None: | ||
return | ||
if issubclass(exctype, self._exceptions): | ||
return True | ||
if issubclass(exctype, ExceptionGroup): | ||
match, rest = excinst.split(self._exceptions) | ||
if rest is None: | ||
return True | ||
raise rest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't ideal as it makes the exception group's own traceback include the
in case of the newly added test. This isn't a big problem because:
Ideally, we wouldn't need this. However, the API of |
||
return False | ||
|
||
|
||
class _BaseExitStack: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class ExceptionIsLikeMixin: | ||
def assertExceptionIsLike(self, exc, template): | ||
""" | ||
Passes when the provided `exc` matches the structure of `template`. | ||
Individual exceptions don't have to be the same objects or even pass | ||
an equality test: they only need to be the same type and contain equal | ||
`exc_obj.args`. | ||
""" | ||
if exc is None and template is None: | ||
return | ||
|
||
if template is None: | ||
self.fail(f"unexpected exception: {exc}") | ||
|
||
if exc is None: | ||
self.fail(f"expected an exception like {template!r}, got None") | ||
|
||
if not isinstance(exc, ExceptionGroup): | ||
self.assertEqual(exc.__class__, template.__class__) | ||
self.assertEqual(exc.args[0], template.args[0]) | ||
else: | ||
self.assertEqual(exc.message, template.message) | ||
self.assertEqual(len(exc.exceptions), len(template.exceptions)) | ||
for e, t in zip(exc.exceptions, template.exceptions): | ||
self.assertExceptionIsLike(e, t) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:class:`contextlib.suppress` now supports suppressing exceptions raised as | ||
part of an :exc:`ExceptionGroup`. If other exceptions exist on the group, they | ||
are re-raised in a group that does not contain the suppressed exceptions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a specific reason to handle
ExceptionGroup
but notBaseExceptionGroup
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt it. Want to make a PR?