c-api函数返回null

时间:2014-10-06 14:45:53

标签: python c++ numpy

我正在尝试创建一些python对象并使用C ++中的NumPy。我修改了一些我发现的例子。下面的代码找到NumPy模块,函数numpy.array但是为pValue返回null。我使用过Python3.4。任何人都可以解释为什么函数总是返回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

1 个答案:

答案 0 :(得分:0)

您对PyObject_CallMethodObjArgs的论据不正确。事实上,我认为你正在调用错误的功能。 PyObject_CallFunctionObjArgs看起来更合适。而不是

    pValue = PyObject_CallMethodObjArgs(pFunc, pYVec);

    pValue = PyObject_CallFunctionObjArgs(pFunc, pYVec, NULL);