在C中从内存运行pyc数据
我想知道如何用Python.h运行我已经加载到内存中的pyc数据?我正在使用一种专有的文件格式,这种格式包含了编译过的Python代码。
比如,我有以下代码:
FILE *fp;
long code_size;
char *code;
fp = fopen("file.format", "rb");
fread(&code_size, sizeof(long), 1, fp);
if (code_size > 0)
{
code = malloc(code_size);
/* the pyc data starts at offset 0x100 for example */
fseek(fp, 0x100, SEEK_SET);
/* read the pyc into the allocated memory */
fread(code, sizeof(char), code_size, fp);
/* execute the pyc data somehow using functions in Python.h.. */
}
fclose(fp);
1 个回答
1
你可以查看一下 boost.python,这个工具可以让你把一个Python解释器嵌入到C++程序中。即使你不能直接通过boost.python执行字节码(我也没有看到有什么说明),你仍然可以写一个小的Python脚本来执行这些字节码,然后再从C++的boost.python中运行这个脚本。