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

for try else bug in 3.x #155

Closed
rocky opened this issue Feb 18, 2018 · 3 comments
Closed

for try else bug in 3.x #155

rocky opened this issue Feb 18, 2018 · 3 comments

Comments

@rocky
Copy link
Owner

rocky commented Feb 18, 2018

Works on 2.6 and 2.7, but not 3.x where the try-else gets compiled as try without an else clause.

That is this:

# Bug found in 2.7 test_itertools.py
def test_iziplongest(self):

    # Having a for loop seems important
    for args in ['abc']:
        self.assertEqual(1, 2)

    pass  # Having this seems important

    # The bug was the except jumping back
    # to the beginning of this for loop
    for stmt in [
        "izip_longest('abc', fv=1)",
    ]:
        try:
            eval(stmt)
        except TypeError:
            pass
        else:
            self.fail()

becomes:

def test_iziplongest(self):

    # Having a for loop seems important
    for args in ['abc']:
        self.assertEqual(1, 2)

    pass  # Having this seems important

    # The bug was the except jumping back
    # to the beginning of this for loop
    for stmt in [
        "izip_longest('abc', fv=1)",
    ]:
        try:
            eval(stmt)
        except TypeError:
            pass
     
        self.fail()
@rocky
Copy link
Owner Author

rocky commented Feb 20, 2018

Looking at disassembly between the two, the difference is a single JUMP instruction which in the try/else is a JUMP_BACK (to the loop) while in the try (no else) it is JUMP_FORWARD to the self.fail().

Given this, probably the best thing to do would be to wait a separate control-flow pass.

@pythonengineer
Copy link

Similar problem in Python 2.4 where a regular try: except: will be decompiled as try: except: else:

@rocky
Copy link
Owner Author

rocky commented Jan 1, 2019

@pythonengineer To be clear this is not about there being a bug in decompiling python 2.4 bytecode, but that when run in python 2.4 interpreter it works the way all the other Python versions work. And that is what we strive for.

@rocky rocky closed this as completed in a232177 Jan 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants