1

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!

1

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

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.