2014-10-06 225 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);