On 9/30/07, Guido van Rossum <[EMAIL PROTECTED]> wrote: > Pickling > -------- > > Left as an exercise for the reader. >
A simple way to add specific pickling support for bytes/buffer objects would be to define two new constants: BYTES = b'\x8c' # push a bytes object BUFFER = b'\x8d' # push a buffer object And add the following pickling and unpickling procedures: def save_bytes(self, obj, pack=struct.pack): n = len(obj) self.write(BYTES + pack("<i", n) + obj) def save_buffer(self, obj, pack=struct.pack): n = len(obj) self.write(BUFFER + pack("<i", n) + obj) def load_bytes(self): len = mloads(b'i' + self.read(4)) self.append(self.read(len)) def load_buffer(self): len = mloads(b'i' + self.read(4)) self.append(buffer(self.read(len))) The only problem with this approach is that bytes object bigger than 4GB cannot be pickled. Currently, this applies to all string-like objects, so I don't think this restriction will cause any trouble. Also, it would be a good idea to bump the protocol version to 3 to ensure that older Python versions don't try to load pickle streams created with these new constants. By the way, would it be a good idea to add specific pickling support for sets (and frozensets)? -- Alexandre _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com