How to use io.BytesIO in Python to write to an existing buffer?

  Kiến thức lập trình

According to How the write(), read() and getvalue() methods of Python io.BytesIO work? , it seems io.BytesIO will copy initial bytes provided.

I have a buffer, and I want pickle to directly dump object in that buffer, without any copy. So I tried to use f = io.BytesIO(buffer). However, after f.write, my buffer is not modified.

Here is an example:

a = bytearray(1024)
import io
b = io.BytesIO(memoryview(a))
b.write(b"1234")
a[:4] # a does not change

What I want, is to make io.BytesIO directly writes to my buffer.

My ultimate goal is:

a = bytearray(1024)
import io, pickle
with io.BytesIO(memoryview(a)) as f:
    obj = [1, 2, 3]
    pickle.dump(obj, f)
# a should hold the pickled data of obj

LEAVE A COMMENT