Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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  ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading