1

I understand a basic C++ function wrapped for Python looks like this:

int square(int n)
{
  return n*n;
}

static PyObject* square_wrapper(PyObject* self, PyObject* args)
{
  int n = 0;

  if(!PyArg_ParseTuple(args, "i", &n))
      return Py_RETURN_NONE;

  return Py_BuildValue("i", square(n));
}

Some of the C++ functions I'd like to wrap take in double arrays and modify them. This isn't a supported type by PyArg_ParseTuple. After doing some searching it is my understanding that using PyArg_ParseTuple to get a PyObject and then making that a PyArray_DOUBLE is an option. How would I go about doing this and how would I traverse such an array. I assume if I modify the array in the C++ extension that the Python version will be modified as well. Also, is NumPy the best way to make a double/float array to pass to my C++ extension?

  • You want to look up [the C API buffer protocol](https://docs.python.org/3/c-api/buffer.html). Both Numpy or the standard library `array` module would be fine options to pass in - the buffer protocol would support either. – DavidW Feb 14 '20 at 08:22
  • Thanks for pointing me in the right direction. I've got it working now. – Aidan Lethaby Feb 14 '20 at 23:55

0 Answers0