[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Native object exposing buffer protocol

On Saturday, January 6, 2018 at 12:02:18 AM UTC, Rob Gaddi wrote:
> I'd like to create a native Python object that exposes the buffer 
> protocol.  Basically, something with a ._data member which is a 
> bytearray that I can still readinto, make directly into a numpy array, etc.
> I can do it by inheriting the entire thing from bytearray directly, but 
> that gives me a whole lot of methods that are unsuitable for what I'm 
> doing with it, which is reading the contents of a binary file, allowing 
> them to be lightly malleable in memory, and then sending them on to a 
> device.
> Not the end of the world (the file's less than 2KB), but it seems like 
> something that should be doable easily without having to throw around a 
> lot of extraneous copies.
> -- 
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com
> Email address domain is currently out of order.  See above to fix.

Curiosity having got the better of me I did some searching and found this "Implementing the buffer protocol" http://cython.readthedocs.io/en/latest/src/userguide/buffer.html, any use to you?  Note that at the bottom of the link the final section "References" states "The buffer interface used here is set out in PEP 3118, Revising the buffer protocol.", i.e. it is the new protocol and not the old Python 2 one.

Kindest regards.

Mark Lawrence.