File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -2393,6 +2393,15 @@ impl Compiler {
23932393
23942394 self . pop_fblock ( FBlockType :: HandlerCleanup ) ;
23952395
2396+ // Create a block for normal path continuation (after handler body succeeds)
2397+ let handler_normal_exit = self . new_block ( ) ;
2398+ emit ! (
2399+ self ,
2400+ Instruction :: Jump {
2401+ target: handler_normal_exit,
2402+ }
2403+ ) ;
2404+
23962405 // CPython compile.c:3568-3577 - cleanup_end block for named handler
23972406 // IMPORTANT: In CPython, cleanup_end is within outer SETUP_CLEANUP scope (line 3506),
23982407 // so when RERAISE is executed, it goes to the cleanup block which does POP_EXCEPT.
@@ -2417,6 +2426,9 @@ impl Compiler {
24172426 ) ;
24182427 }
24192428
2429+ // Switch to normal exit block - this is where handler body success continues
2430+ self . switch_to_block ( handler_normal_exit) ;
2431+
24202432 // Now pop ExceptionHandler - the normal path continues from here
24212433 // CPython compile.c:3557-3558: POP_BLOCK (HandlerCleanup) then POP_BLOCK (SETUP_CLEANUP)
24222434 // followed by POP_EXCEPT at line 3559
Original file line number Diff line number Diff line change @@ -393,7 +393,12 @@ impl ExecutingFrame<'_> {
393393 }
394394 }
395395
396- vm. contextualize_exception ( & exception) ;
396+ // Only contextualize exception for new raises, not re-raises
397+ // CPython only calls _PyErr_SetObject (which does chaining) on initial raise
398+ // RERAISE just propagates the exception without modifying __context__
399+ if !is_reraise {
400+ vm. contextualize_exception ( & exception) ;
401+ }
397402
398403 // Use exception table for zero-cost exception handling (CPython 3.11+)
399404 // But first check if there's a handler in block stack (hybrid approach)
You can’t perform that action at this time.
0 commit comments