diff --git a/Lib/_opcode_metadata.py b/Lib/_opcode_metadata.py index 2effcd30536..db1c3146766 100644 --- a/Lib/_opcode_metadata.py +++ b/Lib/_opcode_metadata.py @@ -216,8 +216,6 @@ 'BEFORE_WITH': 213, 'BINARY_SUBSCR': 214, 'BUILD_CONST_KEY_MAP': 215, - 'JUMP_IF_NOT_EXC_MATCH': 220, - 'SET_EXC_INFO': 223, 'INSTRUMENTED_END_FOR': 234, 'INSTRUMENTED_POP_ITER': 235, 'INSTRUMENTED_END_SEND': 236, diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 6902c092988..4386bba6fdf 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -2925,12 +2925,19 @@ impl Compiler { // If we gave a typ, // check if this handler can handle the exception: if let Some(exc_type) = type_ { - // Duplicate exception for test: - emit!(self, Instruction::Copy { index: 1_u32 }); - // Check exception type: + // Stack: [prev_exc, exc] self.compile_expression(exc_type)?; - emit!(self, Instruction::JumpIfNotExcMatch(next_handler)); + // Stack: [prev_exc, exc, type] + emit!(self, Instruction::CheckExcMatch); + // Stack: [prev_exc, exc, bool] + emit!( + self, + Instruction::PopJumpIfFalse { + target: next_handler + } + ); + // Stack: [prev_exc, exc] // We have a match, store in name (except x as y) if let Some(alias) = name { @@ -3308,13 +3315,9 @@ impl Compiler { // Handler matched // Stack: [prev_exc, orig, list, new_rest, match] + // Note: CheckEgMatch already sets the matched exception as current exception let handler_except_block = self.new_block(); - // Set matched exception as current exception (for __context__ in handler body) - // This ensures that exceptions raised in the handler get the matched part - // as their __context__, not the original full exception group - emit!(self, Instruction::SetExcInfo); - // Store match to name or pop if let Some(alias) = name { self.store_name(alias.as_str())?; diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap index c5b2be80ee8..b13f6e8e32e 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap @@ -83,10 +83,10 @@ expression: "compile_exec(\"\\\nasync def test():\n for stop_exc in (StopIter 73 RERAISE (1) >> 74 JUMP_FORWARD (100) 75 PUSH_EXC_INFO - 76 COPY (1) - 7 77 LOAD_GLOBAL (6, Exception) - 78 JUMP_IF_NOT_EXC_MATCH(96) + 7 76 LOAD_GLOBAL (6, Exception) + 77 CHECK_EXC_MATCH + 78 POP_JUMP_IF_FALSE (96) 79 STORE_FAST (1, ex) 8 80 LOAD_GLOBAL (2, self) diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index d7705ace5dd..3165b0f08aa 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -266,8 +266,6 @@ pub enum Instruction { BuildConstKeyMap { size: Arg, } = 215, // Placeholder - JumpIfNotExcMatch(Arg