Skip to content
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

Python 3 SETUP_LOOP jump optimization #103

Open
rocky opened this issue Nov 8, 2016 · 0 comments
Open

Python 3 SETUP_LOOP jump optimization #103

rocky opened this issue Nov 8, 2016 · 0 comments

Comments

@rocky
Copy link

rocky commented Nov 8, 2016

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.

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

No branches or pull requests

1 participant