从C++调用带参数的Python函数

1 投票
1 回答
2275 浏览
提问于 2025-04-18 10:45

我创建了一个嵌入式的Python解释器,它可以调用一个有三个参数的函数。我之前成功地调用了一个没有参数的函数,但当我执行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) ()

调用的源代码是:

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

这个.cpp文件的完整源代码可以在这里找到:https://github.com/huggle/huggle3-qt-lx/blob/master/huggle/pythonengine.cpp

这到底是哪里出问题了?这样调用函数是对的吗?我参考了https://docs.python.org/3/c-api/object.htmlhttps://docs.python.org/2/c-api/tuple.html的文档。

1 个回答

0

这里有一个来自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);

我觉得你应该注意一下引用计数。

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

关于段错误的问题可以在这里找到讨论。

从C++(或C)回调中调用Python方法

撰写回答