gh-148278: Fix try except block bug#148333
gh-148278: Fix try except block bug#148333Shubham-Developer02 wants to merge 2 commits intopython:mainfrom
Conversation
|
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 |
MazinSharaf
left a comment
There was a problem hiding this comment.
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('''\ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
I may be wrong, I think we should do some tests and such for this, like regression tests.
There was a problem hiding this comment.
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.
Summary
tryregions by labeling block-setup and block-pop pseudos with the active handler inlabel_exception_targets().tryentry inco_exceptiontable.Fixes #148278.
Details
label_exception_targets()assignsinstr->i_except, which becomesco_exceptiontableranges after assembly. Previously:SETUP_FINALLY(and otheris_block_pushinstructions) updated the internal except stack but did not setinstr->i_except, so the start of the protected span could be absent from the table.POP_BLOCKwas rewritten toNOPwithout settinginstr->i_except, which could split or omit coverage for the following instruction (notablycontinue’s backward jump, or minimal try bodies).The fix sets
instr->i_exceptappropriately for those instructions so tracing and pending exceptions on those offsets resolve to the intendedexcepthandler.Tests
Lib/test/test_compile.py(TestSpecifics):test_while_continue_try_except_exception_tabletest_try_literal_stmt_exception_table