I am doing some tests with Python/C API to understand how does it work and how properly use it. My goal is to create C++ wrapper, that allows me to run Python scripts from C++ code. I cannot use external bindind libraries (like Boost.Python or Cython). Everything is going fine, except one thing: now, I'm loading scripts using PyImport_Import()
PyObject* py_module = PyImport_Import(py_module_name); //imports *.py file
//do something, call functions, save results
PyDecRef(py_module);
In release version, however, they have to be distributed in proprietary binary format and loaded into memory on startup. I was looking for some hints how to achieve this - with no result. Basically, I need to do something like this:
File* file = fopen("scripts.bin", "rb");
char* c_buff = malloc(...);
fread(c_buff, file, ...);
PyObject* py_module = CreatePyObjectFromBinaryData(c_buff, ...);
Could smeone provide possible solutions? I thought about using marshalling functionality in this way:
FILE* file = fopen("scripts.bin", "wb");
PyMarshal_WriteObjectToFile(py_module, file, Py_MARSHAL_VERSION);
However, this doesn't seem to work. In fact, I am not sure what objects can be marshalled this way, because documentation doesn't say anything about it.
Optional question: I have all *.py files in my binary folder. On startup, after PyImport_Import() they are implcitly compiled to bytecode (.pyc) in /pycache. I know bytecode files (.pyc or *.pyo) can be created using compileall module. Is it possible to create PyObject containing module data using contents of such file?