如果已在 Tuple 中设置项目,函数对象会在 Py_Finalize 中使程序崩溃

PyFunctionObject crash the programm in Py_Finalize if it has being set item in PyTuple

我需要通过 python 文件获取函数名称和签名列表。 Python 函数 inspect.signature 可以获得函数签名。 但是 inspect.signature 需要一个函数对象 (PyFunctionObject)。 inspect.getmembers可以return这样的对象。 但是,如果我将 PyFunctionObject 设置为 PyTuple 中的项目,当执行 Py_Finalize 时 - 我会收到错误消息。 另外,我正在使用 Qt。

因此第 PyTuple_SetItem(pFuncLink_arg, 0, pGetmember_tupleList); 行结果错误。

代码:.

PyObject *pName, *pModule;
PyObject *pInspect_module_name, *pInspect_module;
PyObject *pGetmember_function, *pGetmember_call_args, *pGetmembers_reply, *pGetsignature_function, *pGetmember_itemList, *pGetmember_tupleList;
PyObject *pFuncName, *pFuncLink_arg, *pFuncSign;

Py_Initialize();

QFileInfo fileInfo(QFile("X:/Projects/p-text.py"));
QString absPath = fileInfo.absolutePath();

QString fileName = fileInfo.baseName();

PyObject* sysPath = PySys_GetObject((char*)"path"); 
PyObject* programName = PyUnicode_FromString(absPath.toAscii());
PyList_Append(sysPath, programName);

pName = PyUnicode_FromString(fileName.toLatin1());
pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL){
    pInspect_module_name = PyUnicode_DecodeFSDefault("inspect");
    pInspect_module = PyImport_Import(pInspect_module_name);
    pGetmember_function = PyObject_GetAttrString(pInspect_module, "getmembers");    
    pGetsignature_function = PyObject_GetAttrString(pInspect_module, "signature");

    pGetmember_call_args = PyTuple_New(1);
    PyTuple_SetItem(pGetmember_call_args, 0, pModule);
    pGetmembers_reply = PyObject_CallObject(pGetmember_function, pGetmember_call_args);

    if (pGetmembers_reply){
        Py_ssize_t const num_args = PyList_Size(pGetmembers_reply);
        for (Py_ssize_t i = 0; i < num_args; ++i){
            pGetmember_itemList = PyList_GetItem(pGetmembers_reply, i);
            pGetmember_tupleList =  PyTuple_GetItem(pGetmember_itemList, 1);

            if (PyFunction_Check(pGetmember_tupleList)){
                pFuncName =  PyTuple_GetItem(pGetmember_itemList, 0);
                QString funcName = PyObjectToString(pFuncName);
                    Py_DECREF(pFuncName);

                pFuncLink_arg = PyTuple_New(1);
                PyTuple_SetItem(pFuncLink_arg, 0, pGetmember_tupleList);

                pFuncSign = PyObject_CallObject(pGetsignature_function, pFuncLink_arg);

                if (pFuncSign == NULL) {
                    if (PyErr_Occurred()) {
                        PyErr_Print(); 
                    }
                    return;
                }

                QString funcSign = PyObjectToString(pFuncSign);

                Py_DECREF(pFuncSign);

                cout<<funcSign.toStdString()<<endl;
            }

            Py_DECREF(pGetmember_tupleList);
            Py_DECREF(pGetmember_itemList);
        }
    }
    Py_DECREF(pInspect_module_name);
    Py_DECREF(pInspect_module);
    Py_DECREF(pGetmember_function);
    Py_DECREF(pGetsignature_function);
    Py_DECREF(pGetmember_call_args);
}
else {
    PyErr_Print();
}

Py_Finalize();

Py_Finalize() 抛出错误 "project_name.exe has triggered a breakpoint"

引用计数错误。

PyTuple_GetItemreturns一个"borrowed reference"。您不拥有 pGetmember_tupleList.

PyTuple_SetItem(在同一 link... 中找到它)"steals a reference",即假设您在通话前确实拥有 pGetmember_tupleList 但您不拥有通话后。

获取后需要加一个Py_INCREF(pGetmember_tupleList)


您有一个与 pFuncName 相关的独立引用计数错误 - 您 decref 了一个不属于您的借用引用。我怀疑这里可能还有其他引用计数错误...


你也没有错误检查;几乎每个 Python 调用之后都应该检查它是否引发异常(通常针对 NULL 指针)。