2012-01-19 247 views
3

我该如何运行python数据,我用Python.h在内存中加载了?我正在使用包含已编译的Python代码的专有文件格式。从内存中运行pyc数据C

例如,我的代码

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); 
+2

[documentation](http://docs.python.org/extending/index.html)位于python站点上。 – 2012-01-19 00:29:37

回答

1

您可以检查出boost.python,这将允许你嵌入Python解释成C++程序。即使你不能直接从boost python执行字节码(并且我没有看到任何其他说法),你可以编写一个小的python脚本来执行字节码,然后从C++执行它增强python。