Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not possible to get a Py_buffer from an array object?

The python documentation on array clearly states that the array conforms to the buffer interface. It even suggest not using the buffer_info() method. But when I try to get a Py_Buffer from C/C++ code with PyObject_GetBuffer() or use python's memoryview, I get a failure.

For example, in python (I use version 2.7):

>>> a = array.array('c')
>>> memoryview(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot make memory view because object does not have the buffer interface

In fact, when I search python's code base, only bytearrayobject (bytearray), memoryobject (memoryview), and stringobject (str) have the required Py_TPFLAGS_HAVE_NEWBUFFER flag set on them. To my understanding, the documentation is wrong; array does not support the buffer interface.

I could use bytearray which supports the buffer interface, the problem is that I need the array's practical fromfile() method to read in a buffer that I can use in my C/C++ code.

Is there an alternative that would allow me to read a file into a buffer and use this buffer from C code, and not involve memory copies ? (I want to treat big binary files and copying is a less desirable option).

like image 739
David Avatar asked Feb 02 '11 17:02

David


1 Answers

memoryview works only on objects that support the Python 3 buffer interface. array.array in Python 3 does, but it doesn't in Python 2.7. You might want to file a bug report for that. Simply use use bytearray (or str if you're using it read-only). Both support memoryview just fine.

like image 191
Rosh Oxymoron Avatar answered Sep 19 '22 02:09

Rosh Oxymoron