python-xdis icon indicating copy to clipboard operation
python-xdis copied to clipboard

Basically a Python interpreter version and a list of code object, unable to generate the correct pyc file

Open Zrincet opened this issue 3 years ago • 0 comments

In stackoverflow, there is the code:

import xdis
from xdis import PYTHON3
from xdis.magics import magics
from xdis.marsh import dumps
from struct import pack
import time


def write_pycfile(pyc_file, code_list, version=xdis.PYTHON_VERSION):
    if PYTHON3:
        file_mode = 'wb'
    else:
        file_mode = 'w'

    with open(pyc_file, file_mode) as fp:
        fp.write(magics[version])
        timestamp = int(time.time())
        fp.write(pack('I', timestamp))
        if version > 3.2:
            fp.write(pack('I', 0))
        for co in code_list:
            try:
                co_obj = dumps(co, python_version=str(version))
                if PYTHON3 and version < 3.0:
                    co_obj = str.encode(co_obj)
                    pass

                fp.write(co_obj)
            except:
                pass
            pass
    print("Wrote %s" % pyc_file)

write_pycfile("D:\Person_Program\Python_Decompile\test.pyc", [write_pycfile.__code__])

then, I run this:

pydisasm --format xasm "D:\Person_Program\Python_Decompile\test.pyc"

The console shows that the target file is not a pyc type file

File name: 'D:\Person_Program\Python_Decompile\test.pyc (12 bytes)' is too short to be a valid pyc file

My python version is 3.7. does this question have anything to do with my python version?

Zrincet avatar Dec 05 '21 08:12 Zrincet