嵌入式 python 如何调用 C++ class 的函数?

How can embedded python call a C++ class's function?

所以,Whosebug,我被难住了。

我的代码是一个嵌入了 Python 的 C++ 函数。我在 C++ 端生成一条消息,将其发送到 python,得到一条不同的消息。我让它工作,我测试它,到目前为止,非常好。

下一步是我需要 Python 自行生成消息并将它们发送到 C++ 中。这就是我开始陷入困境的地方。在花了几个小时对文档感到困惑之后,最好的方法似乎是定义一个模块来保存我的函数。所以我写了以下存根:

static PyMethodDef mailbox_methods[] = {
    { "send_external_message", 
      [](PyObject *caller, PyObject *args) -> PyObject *
      {
         classname *interface = (classname *) 
             PyCapsule_GetPointer(PyTuple_GetItem(args, 0), "turkey");
         class_field_type toReturn;
         toReturn = class_field_type.python2cpp(PyTuple_GetItem(args, 1));
         interface ->send_message(toReturn);
         Py_INCREF(Py_None);
         return Py_None;
     },
     METH_VARARGS, 
     "documentation" },
     { NULL, NULL, 0, NULL }
};

static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "turkey",
    "documentation",
    -1,
    mailbox_methods
};

//defined in header file, just noting its existence here
PyObject *Module;

PyMODINIT_FUNC PyInit_turkey()
{
    Module = PyModule_Create(&moduledef);
    return Module;
}

而在 Python 端,我有以下接收器代码:

import turkey

我收到以下回复:

ImportError: No module named 'turkey'

现在,这是我真正感到困惑的部分。这是我的初始化函数中的代码:

PyInit_turkey();
PyObject *interface = PyCapsule_New(this, "instance", NULL);
char *repr = PyUnicode_AsUTF8(PyObject_Repr(Module));
cout << "REPR: " << repr << "\n";
if (PyErr_Occurred())
    PyErr_Print();

打印出来

REPR: <module 'turkey'>
Traceback (most recent call last):
    <snipped>
    import turkey
ImportError: No module named 'turkey'

所以模块 存在 ,但它从未被传递到任何地方 Python。我找不到有关如何传递它并在 Python 端对其进行初始化的文档。我意识到我可能只是错过了一个微不足道的步骤,但我终生无法弄清楚它是什么。有人可以帮忙吗?

答案最终是我缺少的一个功能。在我调用 Py_Initialize:

之前包含在我的初始化函数的开头
PyImport_AppendInittab("facade", initfunc);
Py_Initialize();
PyEval_InitThreads();

Python 文档没有提到 PyImport_AppendInittab 除了顺带一提,这就是为什么我很难跳转。

对于以后发现此问题的任何其他人:您不需要创建 DLL 来扩展 python 或使用 pre-built 库将模块引入 Python。它可以比这容易得多。