rocky / python-uncompyle6

A cross-version Python bytecode decompiler
GNU General Public License v3.0
3.74k stars 408 forks source link

failure for "if a or not b:" #478

Closed pfremy closed 10 months ago

pfremy commented 10 months ago

Description

uncompyle6 fails at this simple condition :

if a or not b:
     print("a or not b")

This is actually turned into :

if not (a or b):
    print('a or not b')

How to Reproduce

Compile then decompile the following function :

def a_or_not_b(a, b):
    if a or b:
        print('a or b')

    if a or not b:
        print('a or not b')

You get :

def a_or_not_b(a, b):
    if a or b:
        print('a or b')
    if not (a or b):
        print('a or not b')

Output Given

No specific error reported.

Expected behavior

In this case, no surprises, it should produce code that is equivalent to the original one, even if not identical.

If a is True, in the original code, the outcome is print('a or not b'). In the compiled code, the outcome is incorrect.

If you remove the first if, you can also get an incorrect decompiling, but with a slightly different transformation.

Environment


(.env) c:\work\log_enhancer>uncompyle6 --version
uncompyle6 3.9.0
(.env) c:\work\log_enhancer>python --version
Python 3.8.10

(.env) c:\work\log_enhancer>pip show xdis
Name: xdis
Version: 6.0.5
Summary: Python cross-version byte-code disassembler and marshal routines
Home-page: https://github.com/rocky/python-xdis/
Author: Rocky Bernstein, Hartmut Goebel and others
Author-email: rb@dustyfeet.com
License: GPL-2.0
Location: c:\work\log_enhancer\.env\lib\site-packages
Requires: six, click
Required-by: uncompyle6

(.env) c:\work\log_enhancer>python -c "import sys; print(sys.version)"
3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:34:34) [MSC v.1928 32 bit (Intel)]

Windows 10 Enterprise 22H2  build 19045.3570

Workarounds

Not really so far. I'll check other uncompilers.

Priority

Up to you.

pfremy commented 10 months ago

github won't let me attach python or pyc files

rocky commented 10 months ago

github won't let me attach python or pyc files

Not a problem here, the report is detailed and simple enough that I can reproduce the problem. A fix for this seems pretty straightforward and I'll make this for decompyle3 which specializes in 3.7 and 3.8 bytecode. Backporting it will be left as an exercise for a volunteer (although until the code bases are brought more in sync, details are a little different, but the same idea).

For the future, to attach python and pyc files, put them both in a zip or tar.gz file.

pfremy commented 10 months ago

Thanks for the super quick answer. I read your comments about lack of financial support so I was not expecting this to be fixed that quickly.

I'll check the updated code. It seems to me that there are more ways if/else condition can be incorrectly decompiled. I'll report on decompyle3 for the future.

rocky commented 10 months ago

Speed depends on a number of factors like:

rocky commented 10 months ago

Ooops - looks like I forgot to include in decompyle3 the commit to the code base: https://github.com/rocky/python-decompile3/commit/ed375df6b21ecbddd21cf6aaa37c7673da876751