2014-10-06 196 views
0

我想创建一些python对象并使用C++中的NumPy。我修改了一些我发现的例子。我的代码如下查找NumPy模块,函数numpy.array但为pValue返回null。我已经使用Python3.4。任何人都可以阐明为什么函数总是返回NULL?c-api函数返回null

#include <stdlib.h> 
#include <iostream> 
#include <vector> 
#include <Python.h> 
int runPython(int argc, char * argv[]) { 

    std::cout << "Hello from runPython()" << std::endl; 
    PyObject *pFunc; 
    PyObject *pValue, *pYVec; 
    int i; 

    Py_Initialize(); 

    std::cout << Py_GetVersion() << std::endl; 

    PyObject *sys = PyImport_ImportModule("sys"); 
    PyObject *path = PyObject_GetAttrString(sys, "path"); 
    std::cout << PyList_Size(path) << std::endl; 
    Py_DecRef(path); 
    Py_DecRef(sys); 

    PyObject* item = PyList_GetItem(path,1); 
    std::cout << PyString_Check(item) << std::endl; 
    Py_DecRef(item); 

    PyObject *numpy = PyImport_ImportModule("numpy"); 

    if (numpy != NULL) { 
     pFunc = PyObject_GetAttrString(numpy, "array"); //Get the function by its name 
     if (pFunc && PyCallable_Check(pFunc)) { 
      static const double yarr[] = {0,0,1,1,0,0,2,2,0,0,1,1,0,0}; 
      std::vector<double> yvec (yarr, yarr + sizeof(yarr)/sizeof(yarr[0])); 

      //Transfer the other C++ vector to a python list 
      pYVec = PyList_New(yvec.size()); 
      for (i = 0; i < yvec.size(); ++i) { 
       pValue = PyFloat_FromDouble(yvec[i]); 
       if (!pValue) { 

        fprintf(stderr, "Cannot convert array value\n"); 
        return 1; 
       } 
       PyList_SetItem(pYVec, i, pValue); // 
      } 

      //Call the python function 
      pValue = PyObject_CallMethodObjArgs(pFunc, pYVec); 
      if (pValue != NULL) { 
       printf("Result of call: %ld\n", PyInt_AsLong(pValue)); 
      } 
      //Some error catching 
      else { 
       Py_DecRef(pValue); 
       Py_DecRef(pYVec); 
       //Py_DecRef(pFunc); 

       Py_XDECREF(pFunc); 
       Py_DecRef(numpy); 
       fprintf(stderr,"Call failed\n"); 
       PyErr_Print(); 

       return 1; 
      } 
     } 
     else { 
      if (PyErr_Occurred()) 
       PyErr_Print(); 
      fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); 
     } 
     //Py_DECREF(pModule); 
    } 
    else { 
     PyErr_Print(); 
     fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); 
     return 1; 
    } 
    Py_Finalize(); 
    return 0; 
} 

int main (int argc, char *argv[]) 
{ 
    std::cout << "Hello from main" << std::endl; 
    runPython(argc, argv); 
    std::cout << "Program finished" << std::endl; 
    return EXIT_SUCCESS; 
} 

产生输出

Hello from main 
Hello from runPython() 
3.4.0 (default, Apr 11 2014, 13:08:40) 
[GCC 4.8.2] 
7 
0 
Call failed 
Program finished 
+0

@matsjoyce - 我有一个看看库,但似乎没有多大的文档。你有SSCE吗? – 2014-10-10 09:44:02

回答

0

你的参数PyObject_CallMethodObjArgs是不正确的。事实上,我认为你正在调用错误的功能。 PyObject_CallFunctionObjArgs看起来更合适。取而代之的

pValue = PyObject_CallMethodObjArgs(pFunc, pYVec); 

尝试

pValue = PyObject_CallFunctionObjArgs(pFunc, pYVec, NULL);