执行C模块的二进制字符串 [英] Execute binary string of C module
问题描述
有人用 Nuitka 为 python 创建了一个 C 模块.(原始的 Python 代码不可用,该模块已经编译 - 所以它是一个机器二进制文件.)我想在另一个工具中使用该代码,该工具只除了 Python 文件.所以我想将 C 代码包含到 Python 中.
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.
更具体地说:到目前为止,我有文件 thatmodule.pyi
和一个 thatmodule.so
.我可以通过在 mymodule.py
中运行 import thatmodule
将它们包含到我当前的 Python 代码中.现在我只想要一个 Python 文件 mymodule.py
.
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
.
我目前的想法是将 thatmodule.pyi
中的代码复制到 mymodule.py
的开头,并将 thatmodule.so
转换为带有
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'...
并将这个二进制字符串放入mymodule.py
.然后我必须从我的 python 模块 mymodule.py
中执行这个二进制字符串.我该怎么做?
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?
推荐答案
如果您有描述 thatmodule.so
函数的文档,您可以使用以下内容:
If you have a documentation, which describes functions for thatmodule.so
, you can use the following:
import ctypes
mylib = ctypes.CDLL("thatmodule.so")
文档此处
这篇关于执行C模块的二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!