Skip to content

gh-130279: Fix missing exception handlers for jumps in loops - #159460

Open
LH-and-FPGA wants to merge 3 commits into
python:mainfrom
LH-and-FPGA:gh-130279-backward-jump-handler
Open

LH-and-FPGA wants to merge 3 commits into
python:mainfrom
LH-and-FPGA:gh-130279-backward-jump-handler

Conversation

@LH-and-FPGA

Copy link
Copy Markdown

When a loop body ends with an if whose false branch jumps back to the loop head, the backward jump ended up outside the exception table ranges of the enclosing try/with. An exception raised when that JUMP_BACKWARD checks the eval breaker (e.g. KeyboardInterrupt, or an async exception set via PyThreadState_SetAsyncExc) then skipped the except/finally blocks, and __exit__ was not called for with blocks.

def scan(items):
    try:
        for x in items:
            if x < 0:
                raise ValueError(x)
    except KeyboardInterrupt:
        return "handled"

Two places in Python/flowgraph.c created jump instructions without an exception handler after label_exception_targets() had already run:

  • jump_thread() replaced the original jump with a new one, dropping its i_except.
  • normalize_jumps_in_block() added NOT_TAKEN + JUMP for backward conditional jumps without copying the handler.

Both now copy the handler of the original jump.

@python-cla-bot

python-cla-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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.

1 participant