rocky / python-uncompyle6

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

Missing "While True" #485

Open hikali8 opened 6 months ago

hikali8 commented 6 months ago

Description

"While True" will disappear if a "if" branch is indirectly under it

How to Reproduce

import time

L = ['a', 's']
def thread():
    while True:
        for c in L:
            if c == 'a':
                return
        time.sleep(0.5)

Output Given

# uncompyle6 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:48:03) [MSC v.1928 64 bit (AMD64)]
# Embedded file name: simple.py
# Compiled at: 2024-03-05 23:05:50
# Size of source mod 2**32: 157 bytes
import time
L = ["a", "s"]

def thread():
    for c in L:
        if c == "a":
            return             return None
        time.sleep(0.5)

Expected behavior

def thread():
+    while True:
+        for c in L:
+            if c == "a":
+                return
-    for c in L:
-        if c == "a":
-            return             return None
        time.sleep(0.5)

Environment

rocky commented 6 months ago

I believe this works okay on the newer version of this that is specialized or Python 3.8 (and 3.7), called decompyle3.

rocky commented 6 months ago

@hikali8 (or anyone wanting to fix): If you are up for it see why "COME_FROM" at the very top is missing in this code base while it is correctly added in decompyle3.

hikali8 commented 6 months ago

@hikali8 (or anyone wanting to fix): If you are up for it see why "COME_FROM" at the very top is missing in this code base while it is correctly added in decompyle3.

Thank you rocky, I just tested that code in decompyle3 and it was intactly printed, so I will likely be observing decompyle3 and figuring out the cream in it

rocky commented 6 months ago

I will likely be observing decompyle3 and figuring out the cream in it

Great - thanks! The options that will display a lot of information regarding the thought processes the decompilers use is -AgT, for example

decompyle3 -AgT  bug-while.pyc 

I will be giving a talk on these decompilers at Blackhat Asia 2024 .