简体   繁体   English

使用C ++中的参数调用python函数

[英]calling python function with parameters from C++

I created embedded python interpreter which is calling a function, that has 3 parameters. 我创建了嵌入式python解释器,该解释器正在调用具有3个参数的函数。 I successfully managed to do this for function without parameters, but when I execute PyObject_CallObject the program crashes with SEGFAULT: 我成功地对没有参数的函数做到了这一点,但是当我执行PyObject_CallObject ,程序因SEGFAULT而崩溃:

#0  0x00007ffff79c3a25 in PyEval_EvalCodeEx () from /usr/lib/libpython3.2mu.so.1.0
#1  0x00007ffff79c42bf in ?? () from /usr/lib/libpython3.2mu.so.1.0
#2  0x00007ffff79c730a in PyObject_Call () from /usr/lib/libpython3.2mu.so.1.0
#3  0x00000000004f3f31 in Huggle::Python::PythonScript::Hook_SpeedyFinished(Huggle::WikiEdit*, bool) ()

The source code of call is: 调用的源代码为:

void PythonScript::Hook_SpeedyFinished(WikiEdit *edit, bool successfull)
{
    if (edit == nullptr)
        return;
    if (this->ptr_Hook_SpeedyFinished != nullptr)
    {
        HUGGLE_DEBUG("Calling hook Hook_SpeedyFinished @" + this->Name, 2);
        // let's make a new list of params
        PyObject *args = PyTuple_New(3);
        PyObject *page_name = PyUnicode_FromString(edit->Page->PageName.toUtf8().data());
        PyObject *user_name = PyUnicode_FromString(edit->User->Username.toUtf8().data());
        PyObject *success;
        if (!successfull)
            successfull = PyUnicode_FromString("fail");
        else
            successfull = PyUnicode_FromString("success");
        if (PyTuple_SetItem(args, 0, page_name))
            HUGGLE_DEBUG("Failed to pass page_name to tuple @hook_speedy_finished", 3);
        if (PyTuple_SetItem(args, 1, user_name))
            HUGGLE_DEBUG("Failed to pass user to tuple @hook_speedy_finished", 3);
        if (PyTuple_SetItem(args, 2, success))
            HUGGLE_DEBUG("Failed to pass success to tuple @hook_speedy_finished", 3);
        PyObject_CallObject(this->ptr_Hook_SpeedyFinished, args);
        HUGGLE_DEBUG("finished", 1);
    }
}

Full source code of this .cpp file is https://github.com/huggle/huggle3-qt-lx/blob/master/huggle/pythonengine.cpp 该.cpp文件的完整源代码是https://github.com/huggle/huggle3-qt-lx/blob/master/huggle/pythonengine.cpp

What is wrong? 怎么了? Is this even proper way to call the function? 这是调用函数的正确方法吗? I am following https://docs.python.org/3/c-api/object.html and https://docs.python.org/2/c-api/tuple.html 我正在关注https://docs.python.org/3/c-api/object.htmlhttps://docs.python.org/2/c-api/tuple.html

Here is an example from the python documentation. 这是python文档中的示例。

PyObject *arglist;
...
arglist = Py_BuildValue("(l)", eventcode);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
if (result == NULL)
    return NULL; /* Pass error back */
/* Here maybe use the result */
Py_DECREF(result);

It seems to me you should take care of the refcount. 在我看来,您应该注意refcount。

https://docs.python.org/3.0/extending/extending.html#calling-python-functions-from-c https://docs.python.org/3.0/extending/extending.html#calling-python-functions-from-c

Segmentation fault is discussed here. 分割故障在这里讨论。

Calling python method from C++ (or C) callback 从C ++(或C)回调中调用python方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM