gpt4 book ai didi

python - PyObject_CallMethod有时在调用python方法时出现段错误

转载 作者:行者123 更新时间:2023-12-03 07:03:40 25 4
gpt4 key购买 nike

我正在测试一个场景,当C++将函数指针设置为python类变量,然后使用PyObject_CallMethod运行另一个包含该类变量的python方法时。
整个过程都会这样。
(1)。 PyCFunction_NewEx()做一个py函数->(2)。 PyDict_SetItemString()分配给__dict__->(3)下的类变量。 PyObject_CallMethod()调用python方法witch contains(1)。
当我将所有代码放入main()函数中(witt 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")

最佳答案

这可能是因为PyMethodDef是在setCallback的堆栈上创建的
您可以在cpython here的源代码中对其进行验证。
不会复制PyMethodDef,而是对其进行引用。

关于python - PyObject_CallMethod有时在调用python方法时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63948447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com