forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-103791: Make contextlib.suppress also act on exceptions with…
…in an ExceptionGroup (python#103792) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
- Loading branch information
1 parent
19e4f75
commit 22bed58
Showing
6 changed files
with
73 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2023-04-24-23-07-56.gh-issue-103791.bBPWdS.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |