Skip to content

Conversation

@markshannon
Copy link
Member

@markshannon markshannon commented Dec 22, 2020

Consider the following weird function:

def f(x):
    while True:
        if x:
            break
        continue

On master this produces the code:

  1     >>    0 NOP

  2           2 LOAD_FAST                0 (x)
              4 POP_JUMP_IF_FALSE        0

  3           6 LOAD_CONST               0 (None)
              8 RETURN_VALUE

which, when x is False will generate the trace 1, 2, 1, 2, 1, 2, ...

with this PR it produces the following correct, if slightly less efficient code:

  1     >>    0 NOP

  2           2 LOAD_FAST                0 (x)
              4 POP_JUMP_IF_FALSE       10

  3           6 LOAD_CONST               0 (None)
              8 RETURN_VALUE

  4     >>   10 JUMP_ABSOLUTE            0

which generates the correct trace 1, 2, 4, 1, 2, 4, 1, 2, 4, ...

When https://bugs.python.org/issue42719 is addressed we should be able to produce more efficient code.

https://bugs.python.org/issue42246

@markshannon markshannon merged commit 28b75c8 into python:master Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants