Skip to content

Inconsistent locations for conditional branches in while statements #122821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
markshannon opened this issue Aug 8, 2024 · 1 comment
Open
Assignees
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@markshannon
Copy link
Member

markshannon commented Aug 8, 2024

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

@markshannon markshannon added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Aug 8, 2024
@markshannon
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant