Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Zrincet opened this issue Dec 5, 2021 · 1 comment

Comments

@Zrincet
Copy link

Zrincet commented Dec 5, 2021

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
Copy link
Author

Zrincet commented Nov 26, 2022

In https://stackoverflow.com/posts/61271815/revisions, the problem has been solved. However, when referencing methods in the .pyd module, the resulting .pyc file cannot be used correctly.

@Zrincet Zrincet closed this as completed Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants