Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running pyc data from memory in C

Tags:

python

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);
like image 516
david davidson Avatar asked Aug 05 '26 16:08

david davidson


1 Answers

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.

like image 113
Todd Gardner Avatar answered Aug 07 '26 04:08

Todd Gardner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!