【发布时间】:2015-02-03 15:32:09
【问题描述】:
(抱歉标题含糊,但它表明我对这个问题感到多么目瞪口呆)。
所以我正在从 C++ 程序运行 Python 代码,遵循此处描述的方法:https://docs.python.org/2/extending/embedding.html。
这是 C++ 代码:
#include <Python.h>
#include <iostream>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyString_FromString("."));
pName = PyString_FromString((char*)argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pArgs = PyList_New(4);
PyList_SetItem(pArgs,0,PyString_FromString("H-SAMPLE1-OH"));
PyList_SetItem(pArgs,1,PyInt_FromLong(2));
PyList_SetItem(pArgs,2,PyString_FromString("H-SAMPLE2-OH"));
PyList_SetItem(pArgs,3,PyInt_FromLong(3));
PyObject *arglist = Py_BuildValue("(O)", pArgs);
Py_DECREF(pArgs);
for(int run = 0; run < 2; run++)
{
std::cout << "begin" << std::endl;
pValue = PyObject_CallObject(pFunc, arglist);
//Py_DECREF(arglist);
if (pValue != NULL)
{
int py_list_size = PyList_Size(pValue);
printf("list size = %d\n",py_list_size);
int sub_list_size = 0;
for(Py_ssize_t i = 0; i < py_list_size; ++i)
{
PyObject *pList = PyList_GetItem(pValue, i);
sub_list_size = PyList_Size(pList);
if(PyList_Check(pList))
{
for(Py_ssize_t j = 0; j < sub_list_size; ++j)
{
PyObject *pListItem = PyList_GetItem(pList, j);
double pyNumber = PyFloat_AsDouble(pListItem);
std::cout << "pynumber ok" << std::endl;
Py_DECREF(pListItem);
printf("Result of call: %f\n", pyNumber);
}
}
else
{
printf("Not list!\n");
}
Py_DECREF(pList);
}
Py_DECREF(pValue);
}
else {
std::cout << "Else" << std::endl;
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
这里是玩具 Python 代码:
def test(a):
print a
return [[1.2,2.6],[4.7,5.6]]
注意 C++ 代码中的主循环,迭代变量“run”。当循环内的代码只执行一次时,它就像一个魅力。如果我尝试更多地运行它,例如只运行两次,它就会出错,我会遇到分段错误。显然,错误发生在第 61 行,尝试执行时
double pyNumber = PyFloat_AsDouble(pListItem);
我觉得这很奇怪。它在第一次执行期间工作正常,然后突然如果无法从 pListItem 正确获取某些东西(尽管它确实收到了它识别为大小为 2 的列表并且似乎正确处理所有其他 pyObject 指针)。知道发生了什么吗?
重现:
我编译如下:
g++ -L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -I/usr/include/python2.7 -o ms2pip ms2pip.c -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
然后执行如下:
$ ./ms2pip python_code test
(so ./executable < python_file_without_py_extension > < function_name >)
【问题讨论】:
标签: python c++ python-c-api