Decompile mistake with nested if/else + and conditions

Open
#160 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python

Research direction

Start with the attached toto.py example, compile it for Python 3.8, and run decompyle3/bin/decompile.py on the resulting toto.cpython-38.pyc. Trace the nested if/else handling that produces the shown indentation and control-flow error. Done means the decompiled output places the print and return outside the inner conditions while preserving the intended else behavior.

Written by the indexing model from the issue text.

Description

bug control flow Python 3.8

Description

The following code will decompile incorrectly with pyhon 3.8 :

def toto(a, b, c):
	if c:
		if a and b:
				print('a and b')
		print('c, whatever a/b')
		return
	else:
		raise ValueError('not a or not b')

Decompilation result :

def toto(a, b, c):
    if c:
        if a:
            if b:
                print('a and b')
            print('c, whatever a/b')
            return
    raise ValueError('not a or not b')

As you can see the print and return are still within a condition instead of being one indentation level up.

How to Reproduce

I attach the examples although they are quite easy to reproduce.

toto.zip

(.env) C:\work\logh_home\decomp-tests>python -m py_compile toto.py

(.env) C:\work\logh_home\decomp-tests>python ..\python-decompile3\decompyle3\bin\decompile.py __pycache__\toto.cpython-38.pyc
# decompyle3 version 3.9.1.dev0
# Python bytecode version base 3.8.0 (3413)
# Decompiled from: Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:34:34) [MSC v.1928 32 bit (Intel)]
# Embedded file name: toto.py
# Compiled at: 2023-11-16 12:28:39
# Size of source mod 2**32: 153 bytes


def toto(a, b, c):
    if c:
        if a:
            if b:
                print('a and b')
            print('c, whatever a/b')
            return
    raise ValueError('not a or not b')

# okay decompiling __pycache__\toto.cpython-38.pyc

(.env) C:\work\logh_home\decomp-tests>

Environment

(.env) C:\work\logh_home\python-decompile3>git log -1
commit ed375df6b21ecbddd21cf6aaa37c7673da876751 (HEAD -> master, origin/master, origin/HEAD)
Author: rocky <rb@dustyfeet.com>
Date:   Mon Nov 13 08:09:43 2023 -0500

    Forgot to add these to the last commit

(.env) C:\work\logh_home>pip list
Package      Version    Location
------------ ---------- -----------------------------------
click        8.1.7
colorama     0.4.6
decompyle3   3.9.1.dev0 c:\work\logh_home\python-decompile3
pip          21.1.2
setuptools   57.0.0
six          1.16.0
spark-parser 1.8.9
wheel        0.36.2
xdis         6.1.0.dev0
WARNING: You are using pip version 21.1.2; however, version 23.3.1 is available.
You should consider upgrading via the 'C:\work\logh_home\decomp-tests\.env\Scripts\python.exe -m pip install --upgrade pip' command.



(.env) C:\work\logh_home>python --version
Python 3.8.10

Windows 10 22H2

Priority

Up to you.

Additional Context

I lost the source code of the binary release of one of my software.

So, I am in the interesting position where I know very well the original source code, and I am still very interested in recovering it fully, and I can spot mistakes.

In this case, I have a state machine for parsing a file, with deep if/else flow.

And of course, I like open source.

Dominant language
Python
Stars
1.3k
Forks
167
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rocky/python-decompile3

All issues in rocky/python-decompile3

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.