Problem:
- Binary data of fixed size records
- Want to use struct.unpack_from and struct.pack_into to manipulate the binary data
- Want no copies of the data
- Want multiple views into the memory to simply offset calculations etc.
- Data could be in an array.array bytearray or ctypes string buffer
What I tried to do:
part1 = buffer(binary_data, 0, size1)
part2 = buffer(binary_data, size1, size2)
part3 = buffer(binary_data, size1 + size2) # no size is given for this one as it should consume the rest of the buffer
struct.pack_into('I', part3, 4, 42)
The problem here is that struct.pack_into complains about the buffers being read only. I have looked into memoryviews as they can create a read/write view however they don't allow you to specify the offset and size like the buffer function does.
How can I accomplish having multiple zero-copy views into a buffer of bytes that is readable,writable and can be accessed/modified using struct.unpack_from and struct.pack_into
In 2.6+, ctypes data types have a
from_buffer
method that takes an optional offset. It expects a writable buffer and will raise an exception otherwise. (For readonly buffers there'sfrom_buffer_copy
.) Here's a quick translation of your example to use ctypeschar
arrays: