david davidson
david davidson

Reputation: 31

Running pyc data from memory in C

How would I run pyc data I have loaded in memory with Python.h? I'm using a proprietary file format that contains the compiled python code.

For example, I have the code

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

Upvotes: 3

Views: 589

Answers (1)

Todd Gardner
Todd Gardner

Reputation: 13521

You can check out boost.python, which will allow you the embed a python interpreter into a c++ program. Even if you couldn't execute the byte code directly from boost python (and I don't see anything that says one way or the other), you can write a small python script to execute the byte code, then execute that from the c++ boost python.

Upvotes: 1

Related Questions