FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup by ambv · Pull Request #103792 · python/cpython · GitHub

/ cpython Public

gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup - #103792

Merged
ambv merged 6 commits into
python:mainfrom
ambv:gh-103791
Apr 24, 2023
Merged

gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup#103792
ambv merged 6 commits into
python:mainfrom
ambv:gh-103791

Conversation

ambv commented Apr 24, 2023
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

Comment thread Lib/contextlib.py
match, rest = excinst.split(self._exceptions)
if rest is None:
return True
raise rest

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This isn't ideal as it makes the exception group's own traceback include the def __exit__(...) frame. For example:

  File "/Volumes/RAMDisk/cpython/Lib/test/test_contextlib.py", line 1220, in test_exception_groups
    with suppress(ValueError):
  File "/Volumes/RAMDisk/cpython/Lib/contextlib.py", line 454, in __exit__
    raise rest from excinst
  File "/Volumes/RAMDisk/cpython/Lib/test/test_contextlib.py", line 1221, in test_exception_groups
    raise eg_all()

in case of the newly added test.

This isn't a big problem because:

  • the group's member exceptions contain pristine tracebacks, only the group itself contains extra frames; and
  • the raise of the original group is still included in the traceback because we used .split() to create the new object.

Ideally, we wouldn't need this. However, the API of __exit__ makes it impossible to replace the ExceptionGroup instance with another one, while ExceptionGroup itself makes its exceptions read-only.

iritkatriel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. Minor doc tweaks.

Comment thread Doc/library/contextlib.rst Outdated
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
ambv enabled auto-merge (squash) April 24, 2023 22:02
hugovk previously requested changes Apr 24, 2023

hugovk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

(just testing out "request changes")

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

ambv dismissed hugovk’s stale review April 24, 2023 22:09

OK, GitHub indeed doesn't disable auto-merge when changes are requested

ambv merged commit 22bed58 into python:main Apr 24, 2023
carljm added a commit to carljm/cpython that referenced this pull request Apr 24, 2023
* main:
  pythongh-87729: add LOAD_SUPER_ATTR instruction for faster super() (python#103497)
  pythongh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup (python#103792)
Comment thread Lib/contextlib.py
return
if issubclass(exctype, self._exceptions):
return True
if issubclass(exctype, ExceptionGroup):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Was there a specific reason to handle ExceptionGroup but not BaseExceptionGroup?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I doubt it. Want to make a PR?

This file contains hidden or 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL