Robert Poenaru
Robert Poenaru

Reputation: 301

Python C binding - get array from python to C++

As the title says: I wold like to make a python binding in C++ that does some algebraic operations on some array. For this, I have to parse the python "array object" into C++ as a vector of double or integer or whatever the case may be. I tried to do this but I face some issues. I've created a new python type and a class with the name Typer where I have this method that tries to get the elements of a python array, then compute the sum (as a first step).

tatic PyObject *Typer_vectorsum(Typer *self, PyObject *args)
{
    PyObject *retval;
    PyObject *list;

    if (!PyArg_ParseTuple(args, "O", &list))
        return NULL;
    double *arr;
    arr = (double *)malloc(sizeof(double) * PyTuple_Size(list));
    int length;
    length = PyTuple_Size(list);
    PyObject *item = NULL;
    for (int i = 0; i < length; ++i)
    {
        item = PyTuple_GetItem(list, i);
        if (!PyFloat_Check(item))
        {
            exit(1);
        }
        arr[i] = PyFloat_AsDouble(item);
    }
    double result = 0.0;
    for (int i = 0; i < length; ++i)
    {
        result += arr[i];
    }
    retval = PyFloat_FromDouble(result);
    free(arr);
    return retval;
}

In this method I parse the python array object into a C array (allocating the memory of the array with malloc). Then I add every element from the object to my C array and just compute the sum in the last for-loop.

If I build the project and then create a python test file, nothing happens (the file compiles without any issues but it is not printing anything).

y = example.Typer() .  #typer is the init
tuple = (1, 2, 3)
print(y.vectorsum(tuple))

Am I missing something? And also, Is there a nice and easy way of getting a python array object into C++, but as a std::vector instead of a classic C array?

Thank you in advance!

Upvotes: 1

Views: 1117

Answers (1)

Wim Lavrijsen
Wim Lavrijsen

Reputation: 3788

The tuple contains ints, not floats, so your PyFloat_Check fails. And no, there is no direct way from Python tuple to C array or C++ std::vector. The reason being that the tuple is an array of Python objects, not an array of C values such as doubles.

Here's your example with improved error checking, after which it should work:

PyObject *retval;
PyObject *list;

if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &list))
    return NULL;

double *arr = 
arr = (double *)malloc(sizeof(double) * PyTuple_GET_SIZE(list));
int length;
length = PyTuple_GET_SIZE(list);
PyObject *item = NULL;
for (int i = 0; i < length; ++i)
{   
    item = PyTuple_GET_ITEM(list, i);
    arr[i] = PyFloat_AsDouble(item);
    if (arr[i] == -1. && PyErr_Occurred())
    {   
        exit(1);
    }
}
double result = 0.0;
for (int i = 0; i < length; ++i)
{
    result += arr[i];
}
retval = PyFloat_FromDouble(result);
free(arr);
return retval;

Upvotes: 1

Related Questions