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?

2
  • You want to look up the C API buffer protocol. Both Numpy or the standard library array module would be fine options to pass in - the buffer protocol would support either. Commented Feb 14, 2020 at 8:22
  • Thanks for pointing me in the right direction. I've got it working now. Commented Feb 14, 2020 at 23:55

0

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.