-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add support for except* handling and starred subscripts
#6522
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3dea814
Initial plan
Copilot c37fc4f
Implement except* handling and starred subscript support
Copilot 62bb015
Move except* and starred subscript tests into builtin_exceptions
Copilot fbcf0b8
Fix CPython-compatible assert in builtin_exceptions test
Copilot 9b94189
Refine except* codegen using named constant
Copilot 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -45,6 +45,7 @@ use rustpython_wtf8::Wtf8Buf; | |||||
| use std::{borrow::Cow, collections::HashSet}; | ||||||
|
|
||||||
| const MAXBLOCKS: usize = 20; | ||||||
| const COPY_TOP: u32 = 1; | ||||||
|
|
||||||
| #[derive(Debug, Clone, Copy)] | ||||||
| pub enum FBlockType { | ||||||
|
|
@@ -471,7 +472,16 @@ impl Compiler { | |||||
| } | ||||||
| } else { | ||||||
| // VISIT(c, expr, e->v.Subscript.slice) | ||||||
| self.compile_expression(slice)?; | ||||||
| match slice { | ||||||
| Expr::Starred(starred) => { | ||||||
| self.starunpack_helper( | ||||||
| &[Expr::Starred(starred.clone())], | ||||||
| 0, | ||||||
| CollectionType::Tuple, | ||||||
| )?; | ||||||
| } | ||||||
| other => self.compile_expression(other)?, | ||||||
| } | ||||||
|
|
||||||
| // Emit appropriate instruction based on context | ||||||
| match ctx { | ||||||
|
|
@@ -2121,12 +2131,150 @@ impl Compiler { | |||||
|
|
||||||
| fn compile_try_star_statement( | ||||||
|
Collaborator
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.
Suggested change
|
||||||
| &mut self, | ||||||
| _body: &[Stmt], | ||||||
| _handlers: &[ExceptHandler], | ||||||
| _orelse: &[Stmt], | ||||||
| _finalbody: &[Stmt], | ||||||
| body: &[Stmt], | ||||||
| handlers: &[ExceptHandler], | ||||||
| orelse: &[Stmt], | ||||||
| finalbody: &[Stmt], | ||||||
| ) -> CompileResult<()> { | ||||||
| Err(self.error(CodegenErrorType::NotImplementedYet)) | ||||||
| let handler_block = self.new_block(); | ||||||
| let finally_block = self.new_block(); | ||||||
|
|
||||||
| if !finalbody.is_empty() { | ||||||
|
Collaborator
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'd rather have it split like CPython does it This makes it easier to maintain when looking for the diff between our impl and theirs |
||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::SetupFinally { | ||||||
| handler: finally_block, | ||||||
| } | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| let else_block = self.new_block(); | ||||||
|
|
||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::SetupExcept { | ||||||
| handler: handler_block, | ||||||
| } | ||||||
| ); | ||||||
| self.compile_statements(body)?; | ||||||
| emit!(self, Instruction::PopBlock); | ||||||
| emit!(self, Instruction::Jump { target: else_block }); | ||||||
|
|
||||||
| self.switch_to_block(handler_block); | ||||||
| // incoming stack: [exc] | ||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::CallIntrinsic1 { | ||||||
| func: bytecode::IntrinsicFunction1::EnsureExceptionGroup | ||||||
| } | ||||||
| ); | ||||||
| // stack: [remainder] | ||||||
|
|
||||||
| for handler in handlers { | ||||||
| let ExceptHandler::ExceptHandler(ExceptHandlerExceptHandler { | ||||||
| type_, name, body, .. | ||||||
| }) = handler; | ||||||
|
|
||||||
| let skip_block = self.new_block(); | ||||||
| let next_block = self.new_block(); | ||||||
|
|
||||||
| // Split current remainder for this handler | ||||||
| if let Some(exc_type) = type_ { | ||||||
| self.compile_expression(exc_type)?; | ||||||
| } else { | ||||||
| return Err(self.error(CodegenErrorType::SyntaxError( | ||||||
| "except* must specify an exception type".to_owned(), | ||||||
| ))); | ||||||
| } | ||||||
|
|
||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::CallIntrinsic2 { | ||||||
| func: bytecode::IntrinsicFunction2::ExceptStarMatch | ||||||
| } | ||||||
| ); | ||||||
| emit!(self, Instruction::UnpackSequence { size: 2 }); // stack: [rest, match] | ||||||
| emit!(self, Instruction::CopyItem { index: COPY_TOP }); // duplicate match for truthiness test | ||||||
| emit!(self, Instruction::ToBool); | ||||||
| emit!(self, Instruction::PopJumpIfFalse { target: skip_block }); | ||||||
|
|
||||||
| // Handler selected, stack: [rest, match] | ||||||
| if let Some(alias) = name { | ||||||
| self.store_name(alias.as_str())?; | ||||||
| } else { | ||||||
| emit!(self, Instruction::Pop); | ||||||
| } | ||||||
|
|
||||||
| self.compile_statements(body)?; | ||||||
|
|
||||||
| if let Some(alias) = name { | ||||||
| self.emit_load_const(ConstantData::None); | ||||||
| self.store_name(alias.as_str())?; | ||||||
| self.compile_name(alias.as_str(), NameUsage::Delete)?; | ||||||
| } | ||||||
|
|
||||||
| emit!(self, Instruction::Jump { target: next_block }); | ||||||
|
|
||||||
| // Skip handler when no match (stack currently [rest, match]) | ||||||
| self.switch_to_block(skip_block); | ||||||
| emit!(self, Instruction::Pop); // drop match | ||||||
| emit!(self, Instruction::Jump { target: next_block }); | ||||||
|
|
||||||
| // Continue with remaining exceptions | ||||||
| self.switch_to_block(next_block); | ||||||
| } | ||||||
|
|
||||||
| let handled_block = self.new_block(); | ||||||
|
|
||||||
| // If remainder is truthy, re-raise it | ||||||
| emit!(self, Instruction::CopyItem { index: COPY_TOP }); | ||||||
| emit!(self, Instruction::ToBool); | ||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::PopJumpIfFalse { | ||||||
| target: handled_block | ||||||
| } | ||||||
| ); | ||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::Raise { | ||||||
| kind: bytecode::RaiseKind::Raise | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| // All exceptions handled | ||||||
| self.switch_to_block(handled_block); | ||||||
| emit!(self, Instruction::Pop); // drop remainder (None) | ||||||
| emit!(self, Instruction::PopException); | ||||||
|
|
||||||
| if !finalbody.is_empty() { | ||||||
| emit!(self, Instruction::PopBlock); | ||||||
| emit!(self, Instruction::EnterFinally); | ||||||
| } | ||||||
|
|
||||||
| emit!( | ||||||
| self, | ||||||
| Instruction::Jump { | ||||||
| target: finally_block, | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| // try-else path | ||||||
| self.switch_to_block(else_block); | ||||||
| self.compile_statements(orelse)?; | ||||||
|
|
||||||
| if !finalbody.is_empty() { | ||||||
| emit!(self, Instruction::PopBlock); | ||||||
| emit!(self, Instruction::EnterFinally); | ||||||
| } | ||||||
|
|
||||||
| self.switch_to_block(finally_block); | ||||||
| if !finalbody.is_empty() { | ||||||
| self.compile_statements(finalbody)?; | ||||||
| emit!(self, Instruction::EndFinally); | ||||||
| } | ||||||
|
|
||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| fn is_forbidden_arg_name(name: &str) -> bool { | ||||||
|
|
||||||
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
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.
I don't see this logic in CPython:
https://github.com/python/cpython/blob/627894459a84be3488a1789919679c997056a03c/Python/compile.c#L6669-L6678
Assuming that this works, I'd like to have a comment explaining why we are deviating from the CPython logic