zrax / pycdc

C++ python bytecode disassembler and decompiler
GNU General Public License v3.0
3.38k stars 646 forks source link

Support for walrus operator #513

Open greenozon opened 3 months ago

greenozon commented 3 months ago

Python has an interesting feature - walrus operator

if you try to decompile it, the output would be pretty strange, for example

the code

y = 1
x = (y:=10)

decompiles as

c:\Dev\>pycdc walrus.cpython-312.pyc
# Source Generated with Decompyle++
# File: walrus.cpython-312.pyc (Python 3.12)

y = 1
y = 10
x = None

corresponsing disassembly is:

c:\Dev\>pycdas walrus.cpython-312.pyc
walrus.cpython-312.pyc (Python 3.12)
[Code]
    File Name: walrus.py
    Object Name: <module>
    Qualified Name: <module>
    Arg Count: 0
    Pos Only Arg Count: 0
    KW Only Arg Count: 0
    Stack Size: 2
    Flags: 0x00000000
    [Names]
        'y'
        'x'
    [Locals+Names]
    [Constants]
        1
        10
        None
    [Disassembly]
        0       RESUME                          0
        2       LOAD_CONST                      0: 1
        4       STORE_NAME                      0: y
        6       LOAD_CONST                      1: 10
        8       COPY                            1
        10      STORE_NAME                      0: y
        12      STORE_NAME                      1: x
        14      RETURN_CONST                    2: None

walrus-files.zip