Skip to content

gh-148278: Fix try except block bug#148333

Open
Shubham-Developer02 wants to merge 2 commits intopython:mainfrom
Shubham-Developer02:fix_try_except_exception_bug_148278
Open

gh-148278: Fix try except block bug#148333
Shubham-Developer02 wants to merge 2 commits intopython:mainfrom
Shubham-Developer02:fix_try_except_exception_bug_148278

Conversation

@Shubham-Developer02
Copy link
Copy Markdown

@Shubham-Developer02 Shubham-Developer02 commented Apr 10, 2026

Summary

  • Fix missing/incorrect exception table coverage for some try regions by labeling block-setup and block-pop pseudos with the active handler in label_exception_targets().
  • Add regression tests that assert relevant bytecode offsets fall inside a try entry in co_exceptiontable.

Fixes #148278.

Details

label_exception_targets() assigns instr->i_except, which becomes co_exceptiontable ranges after assembly. Previously:

  • SETUP_FINALLY (and other is_block_push instructions) updated the internal except stack but did not set instr->i_except, so the start of the protected span could be absent from the table.
  • POP_BLOCK was rewritten to NOP without setting instr->i_except, which could split or omit coverage for the following instruction (notably continue’s backward jump, or minimal try bodies).

The fix sets instr->i_except appropriately for those instructions so tracing and pending exceptions on those offsets resolve to the intended except handler.

Tests

  • Lib/test/test_compile.py (TestSpecifics):
    • test_while_continue_try_except_exception_table
    • test_try_literal_stmt_exception_table

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot bot commented Apr 10, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 10, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland StanFromIreland changed the title fix try except block bug gh-148278: Fix try except block bug Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exceptions raised while tracing certain statements cannot be caught

1 participant