2012-01-19 31 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。