Execute binary string of C module

社会主义新天地 提交于 2021-01-29 13:21:44

问题


Someone created a C module for python with Nuitka. (The original Python code is not available, the module is already compiled - so it is a machine binary file.) I would like to use the code within another tool, which only excepts Python files. So I would like to include the C code into Python.

To get more specific: So far I have the files thatmodule.pyi and a thatmodule.so. I can include them into my current Python code simply by running import thatmodule inside mymodule.py. Now I only want one single Python file mymodule.py.

My current idea is to copy the code from thatmodule.pyi to the beginning of mymodule.py and to convert thatmodule.so to a binary string with

with open('thatmodule.so', mode='rb') as file:
    fileContent = file.read()

... missing ... how to convert fileContent to b'string'...

and put this binary string into mymodule.py. And then I have to execute this binary string from within my python module mymodule.py. How can I do this?


回答1:


You'll have to write it out to a file (and presumably the .pyi too) and then use python's importlib to dynamically import it.




回答2:


If you have a documentation, which describes functions for thatmodule.so , you can use the following:

import ctypes
mylib = ctypes.CDLL("thatmodule.so")

Documentation here



来源:https://stackoverflow.com/questions/60788861/execute-binary-string-of-c-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!