سؤال

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

هل كانت مفيدة؟

المحلول 2

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

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

نصائح أخرى

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top