Create a Python3 module at runtime while initialize an embedded Python

老子叫甜甜 提交于 2019-12-08 07:19:06

问题


We have a DLL that implements a custom programming language. What I want to do is adding support for the python language keeping the same code for "API function".

I have succefully embedded python in this DLL, now I'm approaching the problem to expose all the old function as a python module.

Now this DLL doesn't expose the API function as interface function but it's installed (as function pointer) to the language engine. In this way it's impossible to create a new python module (a new DLL). But I need to keep the compatibility with the old method...

It's possible to create (and install) at runtime a module defined in the same DLL where the Python is located?

I think something like calling the PyInit_xxxx method after PyInitialize();


回答1:


I've solved using a code like this before Py_Initialize();

/* Add a built-in module, before Py_Initialize */
PyImport_AppendInittab("xxx", PyInit_xxx);



回答2:


The easiest way to handle this is to statically initialize your statically-linked modules by directly calling initspam() after the call to Py_Initialize() or PyMac_Initialize():

int main(int argc, char **argv)
{
    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(argv[0]);

    /* Initialize the Python interpreter.  Required. */
    Py_Initialize();

    /* Add a static module */
    initspam();

An example may be found in the file Demo/embed/demo.c in the Python source distribution.




回答3:


This has gotten substantially more complicated in Python 3 (vs. how it was in Python 2), but I've gotten it working for my code, so I hope this works for you as well.

// Python 3's init function must return the module's PyObject* made 
// with PyModule_Create()
PyObject* initspam(); 
const char* spam_module_name;

int main(int argc, char **argv)
{
    Py_Initialize();

    PyImport_AddModule(spam_module_name);
    PyObject* module = initspam();

    PyObject* sys_modules = PyImport_GetModuleDict();
    PyDict_SetItemString(sys_modules, spam_module_name, module);
    Py_DECREF(module)

    ...
}

I found an example of this in the python 3 source code:

Python-3.4.2\Python\pythonrun.c : import_init()

That has much better error checking and such than my example above.



来源:https://stackoverflow.com/questions/18107783/create-a-python3-module-at-runtime-while-initialize-an-embedded-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!