Skip to content

Inconsistent locations for conditional branches in while statements #122821

Description

@markshannon

Bug report

Bug description:

Reported here: #122762 (comment)

The locations for branches in while statements are not consistent. The first iteration shows different locations from the second and later iterations.

I've not check earlier versions, but this could be an issue for 3.12 as well.

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.13only security fixes
    3.14bugs and security fixes
    on Aug 8, 2024
  2. markshannon commented on Aug 8, 2024

    @markshannon
    MemberAuthor

    Here's my hypothesis:

    This is an artifact of compiling while cond: body as:

    start:
        if not cond: goto end
        body
        if cond goto start
    end:
    

    and the assembler converting condition backwards jumps from if cond goto target to

        if not cond goto end
        goto target
    end:
    

    So we end up with:

    start:
        if not cond: goto end
        body
        if not cond goto end
        goto start
    end:
    

    When cond is True, the first branch jumps from cond to body, but the second branch jumps from cond to cond.

    Compiling while cond: body as:

    start:
        if not cond: goto end
        body
        goto start
    end:
    

    should fix this.

  3. self-assigned this
    on Aug 12, 2024
  4. added a commit that references this issue on Aug 13, 2024
  5. added a commit that references this issue on Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions