GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup by ambv · Pull Request #103792 · python/cpython

Conversation

@ambv

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

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Issue: contextlib.suppress does not support ExceptionGroups #103791

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

watchakorn-18k reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@ambv
pythongh-103791: Make contextlib.suppress also act on exceptions with…
7bc3c15
…in an ExceptionGroup
@ambv
ambv requested review from 1st1 and iritkatriel as code owners April 24, 2023 20:24
@bedevere-bot bedevere-bot mentioned this pull request Apr 24, 2023
contextlib.suppress does not support ExceptionGroups #103791
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Apr 24, 2023
ambv added 3 commits April 24, 2023 22:39
@ambv
Remove unnecessary print() calls
08fa06c
@ambv
Fix docstring typo
be30df6
@ambv
Don't chain with the old exception: split already has the correct TB
acf95f6
ambv
ambv commented Apr 24, 2023
View reviewed changes
Comment thread Lib/contextlib.py
match, rest = excinst.split(self._exceptions)
if rest is None:
return True
raise rest

@ambv ambv Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

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.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ambv
Documentation and Blurb
503d9f8
iritkatriel
iritkatriel approved these changes Apr 24, 2023
View reviewed changes

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

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.

LGTM. Minor doc tweaks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/contextlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2023-04-24-23-07-56.gh-issue-103791.bBPWdS.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Apr 24, 2023
@ambv @iritkatriel
Apply suggestions from code review
a43ffe4
Co-authored-by: Irit Katriel <[email protected]>
@ambv
ambv enabled auto-merge (squash) April 24, 2023 22:02
hugovk
hugovk previously requested changes Apr 24, 2023
View reviewed changes

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

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.

(just testing out "request changes")

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting merge labels Apr 24, 2023
@bedevere-bot

bedevere-bot commented Apr 24, 2023

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

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

@ambv
ambv merged commit 22bed58 into python:main Apr 24, 2023
@bedevere-bot bedevere-bot removed the awaiting changes label Apr 24, 2023
carljm added a commit to carljm/cpython that referenced this pull request Apr 24, 2023
@carljm
Merge branch 'main' into superopt_spec
af807d6
* 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)
@Zac-HD Zac-HD mentioned this pull request Nov 8, 2023
Backport changes to contextlib.suppress()? agronholm/exceptiongroup#91
Closed
Zac-HD
Zac-HD reviewed Nov 8, 2023
View reviewed changes
Comment thread Lib/contextlib.py
return
if issubclass(exctype, self._exceptions):
return True
if issubclass(exctype, ExceptionGroup):

@Zac-HD Zac-HD Nov 8, 2023

Copy link
Copy Markdown
Contributor

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 not BaseExceptionGroup?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iritkatriel iritkatriel Nov 9, 2023

Copy link
Copy Markdown
Member

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Zac-HD reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@Zac-HD Zac-HD mentioned this pull request Nov 9, 2023
gh-103791: handle BaseExceptionGroup in contextlib.suppress() #111910
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel iritkatriel approved these changes
@hugovk hugovk hugovk left review comments
@1st1 1st1 Awaiting requested review from 1st1
+1 more reviewer
@Zac-HD Zac-HD Zac-HD left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@ambv @bedevere-bot @iritkatriel @hugovk @Zac-HD
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.