PyObject_CallMethod 在调用 python 方法时有时会出现段错误
Posted
技术标签:
【中文标题】PyObject_CallMethod 在调用 python 方法时有时会出现段错误【英文标题】:PyObject_CallMethod sometimes seg fault when calling python method 【发布时间】:2020-09-18 02:21:03 【问题描述】:我正在测试一个场景,当 C++ 将函数指针设置为 python 类变量,然后使用 PyObject_CallMethod 运行另一个包含该类变量的 python 方法。
整个过程都会这样。
(1)。 PyCFunction_NewEx()
做一个 py 函数 -> (2)。 PyDict_SetItemString()
分配给 __dict__
-> (3) 下的类变量。 PyObject_CallMethod()
调用python方法巫婆包含(1)。
当我将所有代码放入main()
函数(whitout void setCallback()
和void setCallback()
中的所有代码都放入main()
),它运行得非常好。但是,在我将一些代码放入函数后,有时会出现段错误,有时不会在python中调用函数指针,有时会得到正确的答案。
我该如何解决这个问题?
C++ 代码:main.cpp
#include <python3.7/Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <python3.7/methodobject.h>
// func ptr.
PyObject *myCallFunc(PyObject *self,PyObject *args)
printf(" aaaaaaaaaaaaaaaaaaaaaaa\n");
return NULL;
// def func ptr
typedef PyObject *(*PyCallFunc)(PyObject *self,PyObject *arg);
// set func ptr into python member var
void setCallback(PyObject *ClassObj)
PyCallFunc pyCallFunc = myCallFunc;
PyMethodDef methd = "methd",pyCallFunc,METH_VARARGS,"py call func";
PyObject *fName = PyUnicode_FromString(methd.ml_name);
if(fName == NULL)
printf(" fName\n");
exit(0);
PyObject *pyRunFunc = PyCFunction_NewEx(&methd,NULL,fName);
if(pyRunFunc == NULL)
printf(" can not create py function. exit.");
exit(0);
Py_DECREF(fName);
PyObject* classAttrDict = PyObject_GetAttrString(ClassObj, "__dict__"); // extract instance Dictionary.
if(classAttrDict == NULL)
printf(" classAttrDict\n");
exit(0);
int pRetSetCurrPrice = PyDict_SetItemString(classAttrDict, "callFunc", pyRunFunc);
if(pRetSetCurrPrice != 0)
printf(" set error. exit.");
exit(0);
int main(int argc,char **argv)
Py_SetProgramName((wchar_t *)argv[0]);
void *pyMem = PyMem_Malloc(sizeof(wchar_t*)*argc);
wchar_t** _argv = (wchar_t**)&pyMem;
for (int i=0; i<argc; i++)
wchar_t* arg = Py_DecodeLocale(argv[i], NULL);
_argv[i] = arg;
Py_Initialize();
PySys_SetArgv(argc, _argv);
PyObject* programName = PyUnicode_FromString("test");
if(programName == NULL)
printf(" programName\n");
exit(0);
PyObject* pCustomFunc = PyImport_Import(programName); // import test
Py_DECREF(programName);
if(pCustomFunc == NULL)
printf(" pCustomFunc\n");
exit(0);
PyObject* pClass = PyObject_GetAttrString(pCustomFunc, "Test"); // pClass = test.Test
if(pClass == NULL)
printf(" pClass\n");
exit(0);
PyObject* pNewInstance = PyObject_CallObject(pClass,NULL); // pNewInstance = test.Test()
if(pNewInstance == NULL)
printf(" pNewInstance\n");
exit(0);
setCallback(pNewInstance);
PyObject* pCallRet = PyObject_CallMethod(pNewInstance, "runCustomFunc",NULL); // pCallRet = pNewInstance.callFunc()
if(pCallRet == NULL)
printf(" pCallRet\n");
//exit(0);
sleep(2);
printf(" \n\nend\n\n");
Py_Finalize();
return 0;
Python 代码:test.py
import sys
def dummyFunc():
pass
class Test:
def __init__(self):
self.aaa = 0
self.callFunc = dummyFunc
def runCustomFunc(self):
print(" print from python.")
print(" ref count of self.callFunc 1 is %d" %(sys.getrefcount(self.callFunc)))
self.callFunc()
print(" ref count of self.callFunc 2 is %d" %(sys.getrefcount(self.callFunc)))
return 1
此测试项目的 cmake:CMakeLists.txt
# set cmake and compiler.
cmake_minimum_required(VERSION 3.12...3.15)
set(CMAKE_CXX_FLAGS -std=c++17)
# set variable
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # test if this can resolve the problem
set(THREADS_PREFER_PTHREAD_FLAG ON)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra") # test if optimize cause the problem
set(CMAKE_CXX_FLAGS_DEBUG "-g") # test if optimize cause the problem
set(CMAKE_CXX_FLAGS_RELEASE "-O0") # test if optimize cause the problem
set(LINK_LIB "/usr/local/lib")
set(PYTHON3_LINKER "-lpython3.7")
#set(PTHREAD "-lpthread")
set(PYTHON3_HEADER "/usr/include/python3.7")
set(PYTHON3_LIB "/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu")
set(CPP_FILE_LIST "main.cpp")
include_directories( $PYTHON3_HEADER)
link_directories( $PYTHON3_LIB $LINK_LIB)
add_executable(pyEmbedFunPtrTest $CPP_FILE_LIST)
target_link_libraries(pyEmbedFunPtrTest $PYTHON3_LINKER)
find_package(Threads REQUIRED)
target_link_libraries(pyEmbedFunPtrTest Threads::Threads)
#target_compile_options(pyEmbedFunPtrTest PUBLIC "-pthread")
【问题讨论】:
【参考方案1】:这可能是因为PyMethodDef
是在setCallback
的堆栈上创建的
可以在cpythonhere的源码中验证。
PyMethodDef
未被复制,而是被引用。
【讨论】:
我将其更改为PyMethodDef *methd = new PyMethodDef"methd",pyCallFunc,METH_VARARGS,"py call func";
并解决了问题。谢谢。以上是关于PyObject_CallMethod 在调用 python 方法时有时会出现段错误的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Cdecl 调用在“标准”P/Invoke 约定中经常不匹配?
为啥 Cdecl 调用在“标准”P/Invoke 约定中经常不匹配?