首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回空的c-api函数

返回空的c-api函数
EN

Stack Overflow用户
提问于 2014-10-06 14:45:52
回答 1查看 695关注 0票数 0

我正在尝试创建一些python对象并使用来自C++的C++。我修改了一些我找到的例子。下面的代码查找NumPy模块,即函数numpy.array,但对于pValue返回null。我用过Python3.4。有人能解释为什么函数总是返回NULL吗?

代码语言:javascript
复制
#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;
}

产生输出

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-10 02:28:28

您对PyObject_CallMethodObjArgs的论点是不正确的。事实上,我认为你打错了电话。PyObject_CallFunctionObjArgs看起来更合适。而不是

代码语言:javascript
复制
    pValue = PyObject_CallMethodObjArgs(pFunc, pYVec);

试一试

代码语言:javascript
复制
    pValue = PyObject_CallFunctionObjArgs(pFunc, pYVec, NULL);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26218770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档