Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.2k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from5 commits
7bc3c15
08fa06c
be30df6
acf95f6
503d9f8
a43ffe4
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -304,8 +304,16 @@ Functions and classes provided: | ||
This context manager is :ref:`reentrant <reentrant-cms>`. | ||
If the code within the :keyword:`!with` block raises an | ||
:exc:`ExceptionGroup`, suppressed exceptions are removed from the | ||
group. If any others are left, the modified group is re-raised. | ||
Otherwise, the exception group is empty and so nothing is raised. | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
.. versionadded:: 3.4 | ||
.. versionchanged:: 3.12 | ||
``suppress`` now supports suppressing exceptions raised as | ||
part of an :exc:`ExceptionGroup`. | ||
.. function:: redirect_stdout(new_target) | ||
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 | ||
if exctype is None: | ||
return | ||
if issubclass(exctype, self._exceptions): | ||
return True | ||
if issubclass(exctype, ExceptionGroup): | ||
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. Was there a specific reason to handle 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. I doubt it. Want to make a PR? | ||
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, it | ||
is now re-raised without the suppressed ones. | ||
ambv marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |