简体   繁体   English

尝试传递数组时,PyObject_CallObject 返回 NULL

[英]PyObject_CallObject is returning NULL when trying to pass an array

I am trying to pass a 2D array from C++ to Python function but the PyObject_CallObject function is returning NULL .This happens in case of 1D array as well.我试图将一个 2D 数组从 C++ 传递给 Python 函数,但PyObject_CallObject函数返回NULL 。这种情况也发生在一维数组的情况下。 It works fine when I do not pass any argument or in case the argument is just single variable and not an array.当我不传递任何参数或者参数只是单个变量而不是数组时,它工作正常。 My code is as follows:我的代码如下:

#include <stdio.h>
#include <Python.h>
#include <pyhelper.hpp>
#include <string>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h> // For capturing the output values given by detector Python inference code

using namespace std;

PyObject *PythonInitialize(string script_name, string function_name);

int main(int argc, char *argv[])
{

    if (!Py_IsInitialized())
    {
        Py_InitializeEx(0); // Initialize the Python interpreter
    }

    PyObject *PythonDetectorFunction, *PythonDetectorFunctionArguments;
    PythonDetectorFunction = PythonInitialize("test", "getsum"); //getint, getsum

    PythonDetectorFunctionArguments = PyTuple_New(1); // Reference count incremented. Need to handle the reference counting
    if (PythonDetectorFunctionArguments == NULL)
        PyErr_Print();

    if (PyArray_API == NULL)
    {
        import_array();
    }

    float data[2][4] = {{1.2, 3.4, 5.6, 7.8}, {1.2, 3.4, 5.6, 7.8}};
    npy_intp dims[2] = {2, 4};

    PythonDetectorFunctionArguments = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT, data);

    PyObject *PythonDetectorFeatureMaps = PyObject_CallObject(PythonDetectorFunction, PythonDetectorFunctionArguments); // Will contain the output given by Python code

    if (PythonDetectorFeatureMaps != NULL)
        printf("PythonDetectorFeatureMaps: %p\n", PythonDetectorFeatureMaps);
    else
        printf("PythonDetectorFeatureMaps is NULL.\n");

    Py_DECREF(PythonDetectorFunction);
    Py_DECREF(PythonDetectorFunctionArguments);
    //Py_DECREF(PythonDetectorFeatureMaps);
}

PyObject *PythonInitialize(string script_name, string function_name)
{
    PyObject *pName, *pModule, *pFunc;
    string PythonCodeImportStatement = "sys.path.append(os.getcwd())";
    PyRun_SimpleString("import sys, os");
    PyRun_SimpleString(PythonCodeImportStatement.c_str()); // Relative path for the python code

    // pNmae is the name of the python script/module to be called
    pName = PyUnicode_FromString(script_name.c_str()); // Reference count incremented. Need to handle the reference counting
    if (pName == NULL)
        PyErr_Print();

    // Getting the Python module reference inside of the C code
    pModule = PyImport_Import(pName);
    if (pModule == NULL)
        PyErr_Print();

    // Python function reference
    pFunc = PyObject_GetAttrString(pModule, function_name.c_str()); // Reference count incremented. Need to handle the reference counting
    if (pFunc == NULL)
        PyErr_Print();

    // Refrence count clean-up
    Py_XDECREF(pName);
    Py_XDECREF(pModule);
    //Py_XDECREF(pFunc);
    return pFunc;
}

Python function in test.py: test.py 中的 Python 函数:

def getsum(tuple1):
    print('Python function getsum() called')
    return None

Can someone point out the mistake?有人可以指出错误吗?

PyObject_CallObject takes a callable and a tuple of arguments for the callable, or NULL for no arguments. PyObject_CallObject接受一个可调用对象和一个可调用对象的参数元组,或者 NULL 表示没有参数。 You're trying to pass it a NumPy array instead of an argument tuple.您试图将 NumPy 数组而不是参数元组传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyEval_CallObject 和 PyObject_CallObject 返回一个空对象 - PyEval_CallObject and PyObject_CallObject return a null object 第二次调用PyObject_CallObject()时出现分段错误 - Segmentation fault when calling PyObject_CallObject() the second time 第二次调用 PyObject_CallObject() 失败 - Fail calling PyObject_CallObject() for the second time PyObject_CallObject在bytearray方法上失败 - PyObject_CallObject failing on bytearray method 使用多个函数参数调用PyObject_CallObject - Call PyObject_CallObject with more than one function argument c++ 的 matplotlib 给出此错误:'PyObject_CallObject': function 不占用 3 ZDBC16FB4CAA5BDA882EBD77 - matplotlib for c++ gives this error: 'PyObject_CallObject': function does not take 3 arguments PyObject_GetAttrString C++ 函数返回 NULL:无法从 C++ 调用 Python 函数 - PyObject_GetAttrString C++ function returning NULL: Unable to call Python functions from C++ 尝试在scikit-learn中并行化参数搜索会导致“ SystemError:PyObject_Call中没有错误的NULL结果” - Trying to parallelize parameter search in scikit-learn leads to “SystemError: NULL result without error in PyObject_Call” 尝试传递无符号字符数组时出现 ArgumentError - ArgumentError when trying to pass an array of unsigned char 尝试访问表数据时函数返回 null - Function returning null when trying to access table data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM