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
Copy link
Copy Markdown
Contributor

@MazinSharaf MazinSharaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, may need further discussion for more simplification, and for more code that is tested for broader cases.

def test_try_literal_stmt_exception_table(self):
# Like try + continue, a try body that is only a literal statement must
# not leave the "invisible" result-discard outside the exception table.
src = textwrap.dedent('''\
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a little manual rather than actually facing the problem itself. Let's say if it was:

try:
      x = 1
      42 # or continue

then it would be a problem because the line would just go back to the previous line instead of the try block, which means that this isn't the right fix for the problem, it's just a workaround for a specific fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think the code is not only fixing a specific problem, but it also seems a bit excessive the way it is trying to tackle the issue of:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong, I think we should do some tests and such for this, like regression tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As feedback, I would like to suggest a possible simplification, I think there could be more room for simplification here, also maybe apart of the function names as well, a bit repetitive in my opinion, however it is your choice to keep that. I also would like to add my thought on the code logic itself, which I think does not broadly apply to all cases, so we can try further discussing some ways on improving the code so that it reaches more use cases more reliably.

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

2 participants