Python 在C中从内存运行pyc数据

Python 在C中从内存运行pyc数据,python,Python,如何运行用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

如何运行用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);

你可以签出,这将允许你把Python解释器嵌入到C++程序中。即使你不能直接从Boost Python执行字节代码(我也没有看到任何一种方法,你可以编写一个小的Python脚本来执行字节代码,然后从C++ Boost Python执行)。