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
>Solution :
IIUC you can try to make your own IO class:
import io
import pickle
class MyIO(io.IOBase):
def __init__(self, buf):
self.buf = memoryview(buf)
self.position = 0
def write(self, b):
start = self.position
n = len(b)
end = start + n
self.buf[start: end] = b
self.position = end
a = bytearray(1024)
with MyIO(a) as f:
obj = [1, 2, 3]
pickle.dump(obj, f)
print(a)
Prints:
bytearray(b'\x80\x04\x95\x0b\x00\x00\x00\x00\x00\x00\x00]\x94(K\x01K\x02K\x03e.\x00\x00\x00 ...