pythonc++embeddingpython-embedding

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. 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:

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

Can someone point out the mistake?


Solution

  • PyObject_CallObject takes a callable and a tuple of arguments for the callable, or NULL for no arguments. You're trying to pass it a NumPy array instead of an argument tuple.