gpt4 book ai didi

python - C++ python API : second call of PyImport_Import results in SIGSEGV

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:42 24 4
gpt4 key购买 nike

我正在尝试通过 c Api 从 c++ 调用 python,以获取 c++ 中两个 numpy 数组的值。第一次调用我的程序 callPython() 时,一切似乎都运行良好,但第二次调用导致 SIGSEGV 时

  pModule = PyImport_Import(pName);

被执行。

flebool 的回答中,有一个比我的简单得多的最小示例代码,但有同样的错误。

最小.cpp

#include <Python.h>
#include <numpy/arrayobject.h>

long int geTuple( PyObject *pValue , PyObject *objI , int i)
{
objI = PyTuple_GetItem(pValue, i);

long int n,M;
double *xJ;

if (objI != NULL)
{
n = PyArray_NDIM(objI);
printf("PyArray_NDIM(objI): %ld\n" , n );

M = *PyArray_DIMS(objI);
printf("PyArray_DIMS(objI) : %ld\n" , M );

for (int k = 0; k < M; k++)
{
xJ = (double *) PyArray_GETPTR1(objI, k );
printf("xJ : %f\n" , *xJ );
}
return M;
}
else
{
printf("geTuple is Null \n");
return -1;
}
}

void callPython()
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;

Py_Initialize();

//Import current folder to Python path
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.insert(0, '')");

// Load name of pythonfile without py
pName = PyString_FromString( "minimal" );
/* Error checking of pName left out */

pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL)
{
//Name of the Python function
pFunc = PyObject_GetAttrString(pModule, "minimalFunction" );
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc))
{
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, PyInt_FromLong(2) );

pValue = PyObject_CallObject(pFunc, pArgs);

Py_DECREF(pArgs);

if (pValue != NULL)
{
long int dims[2];
PyObject *ob1,*ob2;

dims[0] = geTuple( pValue , ob1 , 0);
dims[1] = geTuple( pValue , ob2 , 1);

Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return;
}
}
else
{
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", "minimalFunction");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", "minimal.py");
return ;
}
Py_Finalize();

}


int
main(int argc, char *argv[])
{
callPython();

printf("2nd Call\n");
callPython();

printf("Run over\n");
return 0;
}

最小.py

#! /usr/bin/env python
import numpy as np

def minimalFunction(dim):
xLower = np.ones(dim)
dCp = np.zeros(dim)

return xLower , dCp

我在 Ubuntu 12.04 上使用 python 和 numpy 包以及 anaconda 编译我的程序,使用以下命令:

gcc minimal.cpp -o minimal -I/usr/include/python2.7 -I/usr/include/python2.7 -lpython2.7 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security

缩短的 gdb 回溯是

#101 0x00007ffff7a1ebeb in ?? () from /usr/lib/libpython2.7.so.1.0
#102 0x00007ffff79e972e in PyObject_CallFunction () from /usr/lib/libpython2.7.so.1.0
#103 0x00007ffff79b312d in PyImport_Import () from /usr/lib/libpython2.7.so.1.0
#104 0x0000000000400cea in callPython () at minimal.cpp:48
#105 0x0000000000400af8 in main (argc=<optimized out>, argv=<optimized out>) at minimal.cpp:110

我对 python 的调用可能出了什么问题?

最佳答案

这是评论,不是真正的答案。我不知道你的代码有什么问题。以下(更简单的)示例也失败了。 最小.cpp:

#include <Python.h>

void callPython(){

Py_Initialize();

//Import current folder to Python path
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.insert(0, '')");

// Load name of pythonfile without py
PyObject *pName= PyString_FromString( "minimal" );
/* Error checking of pName left out */

PyObject *pModule= PyImport_Import(pName);
Py_DECREF(pName);
Py_DECREF(pModule);
Py_Finalize();
}

int main(int argc, char *argv[])
{
callPython();
printf("2nd Call\n");
callPython();
printf("Run over\n");
return 0;
}

用这个minimal.py:

import numpy as np

def minimalFunction(dim):
return 1

有趣的是,如果您注释掉 import numpy 语句,一切正常。

我试过:

  • 运行 python -m minimal && python -m minimal 而没有来自 shell 的任何段错误,这排除了问题可能在 numpy 中,或在至少它不只是在那里。
  • cpp 代码中两次调用 callPython() 之间调用 sleep(2)。无论如何它都会出现段错误。

祝你好运!

关于python - C++ python API : second call of PyImport_Import results in SIGSEGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25530727/

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