1

I'm writing a c++ code to call a python function and the returned array from the python function will be store in an array in c++. I am able to call the python function in c++ but I am able to return only one value from Python to C++ and what I want to return is an array. Below is my C++ code:

int main(int argc, char *argv[])
{
int i;
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

if (argc < 3) 
{
    printf("Usage: exe_name python_source function_name\n");
    return 1;
}

// Initialize the Python Interpreter
Py_Initialize();

// Build the name object
pName = PyString_FromString(argv[1]);

// Load the module object
pModule = PyImport_Import(pName);

// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);

// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, argv[2]);

    pValue = PyObject_CallObject(pFunc, NULL);
    if (pValue != NULL) 
    {
        printf("Return of call : %d\n", PyInt_AsLong(pValue));
        PyErr_Print();
        Py_DECREF(pValue);
    }
    else 
    {
        PyErr_Print();
    }

Here, the value which pValue should take is an array but am able to successfully execute when it takes a single element only.

I am not able to understand how will an array be passed from python to C++.

2 Answers 2

3

A Python list is 1 object. Can you return a list from python and check that you got it in C++ with PyList_Check? Then see how long it is with PyList_Size and fish out the items with PyList_GetItem.

Sign up to request clarification or add additional context in comments.

1 Comment

@Christopher..thanks a lot for the guidance..the first 2 steps are correct but the third step doesnt holds good as he data which I send is in the form of a float array but when it is received it is in the form of a char array. But with some more manipulations I am able to complete what I aspired to do. thanks again :)
1

I was able to solve the above problem with the help of Chris's guidance as follows:

When returning the data from Python, return a list instead of an array.

    pValue = PyObject_CallObject(pFunc, pArgTuple);
    Py_DECREF(pArgTuple);
    if (pValue != NULL) 
    {   

        printf("Result of call: %d\n", PyList_Check(pValue));
        int count = (int) PyList_Size(pValue);
        printf("count : %d\n",count);
        float temp[count];
        PyObject *ptemp, *objectsRepresentation ;
        char* a11;

        for (i = 0 ; i < count ; i++ )
        {
            ptemp = PyList_GetItem(pValue,i);
            objectsRepresentation = PyObject_Repr(ptemp);
            a11 = PyString_AsString(objectsRepresentation);
            temp[i] = (float)strtod(a11,NULL);
        }

Here, your temp array of float will hold the array which you sent as a list from python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.