3

I have implemented the new python buffer interface in C++ outlined here:

https://docs.python.org/2/c-api/buffer.html

I have implemented my Py_buffer struct and filled it in:

template<typename T>
static int getbuffer(PyObject *obj, Py_buffer *view, int flags) 
{
    flags;
    static const Py_ssize_t suboffsets[1] = { 0};
    view->buf           = (void*)(_Cast<T>(obj)->getbuffer());
    view->obj           = NULL;
    view->len           = _Cast<T>(obj)->getbuffersize();
    view->itemsize      = 1;
    view->readonly      = _Cast<T>(obj)->getreadonly();
    view->ndim          = 0;
    view->format        = NULL;
    view->shape         = NULL;
    view->strides       = NULL;
    view->suboffsets    = NULL;
    view->internal      = NULL;
    return 0;
}

I am creating my Python buffer class in Python and handing it to C++. I am getting a pyObject along with my Py_Buffer. So now my question is, how am I supposed to write and resize this pyBuffer in C++? I can get access to the pointer directly and a size. But if its a newly created buffer how do I tell it how much space I need? There does not seem to be any sort of resize function for me to call.

I can use: int result = PyBuffer_FromContiguous(&m_view, const_cast<void*>(data), pySize, 'A');

to add data to my buffer. But my buffer must already have the correct size or it wont write. I do not think this is the correct way to be using it anyway.

Cython is not an option.

13
  • How about realloc? Commented Apr 10, 2015 at 17:25
  • I thought of a similar solution but am I allowed to reassign the view->buf pointer? Is this how things are supposed to be done? Is there any kind of documentation on how to implement a python write buffer? Other then just the method names. Commented Apr 10, 2015 at 17:29
  • this won't reassign, this'll extend. I think, you can give this a try. Commented Apr 10, 2015 at 17:31
  • ""The function may move the memory block to a new location (whose address is returned by the function)"", The only way I can get the python data is through the function pointer that view->buf exposes. I cannot change the pointer that the function pointer is returning. Commented Apr 10, 2015 at 17:33
  • 1
    I don't think you would resize the pybuffer directly, I think you would resize the associated pyobject, e.g. with PyByteArray_Resize() Commented Apr 10, 2015 at 18:57

1 Answer 1

2
+100

You shouldn't resize the Py_buffer directly, since it is just an interface to the data of a PyObject.

Instead, use PyByteArray_Resize() (or possibly _PyString_Resize()) on the underlying PyObject.

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.