-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add except* support #6530
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
Merged
Merged
Add except* support #6530
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bca3cd
Initial plan
Copilot 1e9ab2a
Implement except* handling and starred subscript support
Copilot 97ecbda
Move except* and starred subscript tests into builtin_exceptions
Copilot 4dc0152
Fix CPython-compatible assert in builtin_exceptions test
Copilot a62981f
Refine except* codegen using named constant
Copilot a73b2d6
except*
youknowone 8c2cddc
impl syntaxerror
youknowone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
🧩 Analysis chain
🏁 Script executed:
# First, examine the prep_reraise_star and is_same_exception_metadata implementations wc -l crates/vm/src/exceptions.rsRepository: RustPython/RustPython
Length of output: 97
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 789
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 154
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 881
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1587
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1209
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 333
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 158
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 122
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 750
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 109
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 999
🏁 Script executed:
# Check CPython's _PyExc_PrepReraiseStar to understand the expected behavior web_search CPython _PyExc_PrepReraiseStar implementation nested exception groupsRepository: RustPython/RustPython
Length of output: 161
🌐 Web query:
CPython _PyExc_PrepReraiseStar implementation exception groups💡 Result:
Short answer — what _PyExc_PrepReraiseStar does and why
_PyExc_PrepReraiseStar is an internal CPython helper used when re‑raising an exception group with the "raise " (reraise‑star) semantics introduced by PEP 654. Its job is to prepare a value suitable for re‑raising from an original exception object and a sequence (or group) of nested exceptions: normalizing/constructing the appropriate ExceptionGroup/BaseExceptionGroup object, and making sure context/cause/traceback behavior matches the semantics PEP 654 requires for except / reraise-star. [1][2]
The function is declared in CPython’s internal pycore pyerrors header as:
PyObject* _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs);
(where orig is the original exception object and excs is the sequence / group of exceptions to be reraised). [2]
Conceptually it implements the re‑raise semantics described in PEP 654: when an except* handler yields a subset of a group (or re‑raises parts), CPython must produce a (possibly new) exception group that preserves appropriate metadata (message, cause/context, and composed tracebacks for leaf exceptions) and obeys ephemeral/handler semantics documented in PEP 654. _PyExc_PrepReraiseStar centralizes those preparation steps before the interpreter actually raises the resulting exception object. [1]
References
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1625
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 108
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 2105
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 2371
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 235
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 107
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1931
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 255
🏁 Script executed:
# Read the full builtin_exceptions.py to see if there are exception group tests wc -l extra_tests/snippets/builtin_exceptions.pyRepository: RustPython/RustPython
Length of output: 111
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 2105
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 409
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 451
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1538
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 619
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 47
prep_reraise_starand exception grouping behavior need verification against complex PEP 654 scenariosThe current implementation handles basic cases but has notable gaps:
Flattened exception structure: All matched and reraised exceptions are collected into a single flat tuple and wrapped as
ExceptionGroup("", all_excs). This differs from CPython's approach of preserving nested group structure when selectively reraising subsets, which may affect tracebacks and exception grouping semantics in non-trivial cases.Limited test coverage: Only one trivial test exists (
extra_tests/snippets/builtin_exceptions.py:371–380—a bareValueErrorwithexcept*). No tests cover nested exception groups, multiple exceptions, mixed raise/reraise patterns, or the richer PEP 654 scenarios. This makes it difficult to assess correctness beyond the simplest paths.Potential non-group
origgaps: CPython's_PyExc_PrepReraiseStarhas special-case handling whenorigis not anExceptionGroupitself. The current implementation does not appear to have this optimization.To improve confidence, add tests for nested groups and complex except* patterns (matching the PEP 654 examples), then compare behavior against CPython. The design decision to always create
BaseExceptionGroup(rather than preserving the original exception class) is intentional per prior decisions, but the exception grouping logic itself should be validated against CPython's behavior for correctness.