Skip to content

Python 3 SETUP_LOOP jump optimization #103

Open
@rocky

Description

@rocky

Here is a really tricky program to decompile distilled from sre.py

# From python 3.4 sre.pyc
while 1:
    if __file__:
        while 1:
            if __file__:
                break
            raise RuntimeError
    else:
        raise RuntimeError

while 1:
    if __file__:
        if __name__:
            raise RuntimeError
        else:
            # flags
            while __name__:
                group = 5
while 1:
    if __name__:
        while 1:
            if y:
                break
            raise RuntimeError
    elif __file__:
        x = 2
    else:
        raise RuntimeError

Shorter versions of the above, just the first while 1 loop on Python 3.0 gives

Warning: block stack is not empty!
while __file__:
    while __file__:
        break
    raise RuntimeError
    continue
    continue
    raise RuntimeError
    continue
    return None

While on Python 3.1 I've gotten get SEGV's. I am pretty sure newer versions of Python will give similiar results

A disassembly of the shorter version shows why this is hard:

  2           0 SETUP_LOOP              40 (to 43)

  3     >>    3 LOAD_NAME                0 (__file__)
              6 POP_JUMP_IF_FALSE       34 (to 34)

  4           9 SETUP_LOOP              28 (to 40)

  5     >>   12 LOAD_NAME                0 (__file__)
             15 POP_JUMP_IF_FALSE       22 (to 22)

  6          18 BREAK_LOOP
             19 JUMP_FORWARD             0 (to 22)

  7     >>   22 LOAD_NAME                1 (RuntimeError)
             25 RAISE_VARARGS            1
             28 JUMP_ABSOLUTE           12 (to 12)
             31 JUMP_ABSOLUTE            3 (to 3)

  9     >>   34 LOAD_NAME                1 (RuntimeError)
             37 RAISE_VARARGS            1
        >>   40 JUMP_ABSOLUTE            3 (to 3)
        >>   43 LOAD_CONST               0 (None)
             46 RETURN_VALUE

The second and inner SETUP_LOOP has as it closing target a location that is strictly outside of its source-code range.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions