Python 3 - not copying stream interface for bytearray?

I am reading a data buffer from somewhere to bytearray . Now I want to work with this data using the stream interface (i.e. read , seek , etc.)

Can I just transfer bytearray io.BytesIO ?

 mybytearray = bytearray(...) stream = io.BytesIO(mybytearray) 

My fear here is BytesIO copying BytesIO data, but I do not want this because the buffer is very large. I do not need copies, I want the stream to work with the source data and also be able to modify it. What can be done?

+6
source share
1 answer

BytesIO manages its own memory and copies the buffer used to initialize it. You can encapsulate your bytearray in a file-like class. Or you can go the other way, allowing you to allocate a BytesIO memory BytesIO . Then you can get an idea of ​​the buffer, which can be changed by index and slice, but you cannot change the size of the buffer during the presentation:

 >>> f = io.BytesIO(b'abc') >>> view = f.getbuffer() >>> view[:] = b'def' >>> f.getvalue() b'def' >>> view[3] = b'g' IndexError: index out of bounds >>> f.seek(0, 2) >>> f.write(b'g') BufferError: Existing exports of data: object cannot be re-sized >>> del view >>> f.write(b'g') >>> f.getvalue() b'defg' 

Edit:

See issue 22003 , BytesIO copy-on-write. The latest patch (cow6) only supports copy-on-write for bytes only.

+8
source

Source: https://habr.com/ru/post/902170/


All Articles