[New-bugs-announce] [issue1338] pickling bytes?

Guido van Rossum report at bugs.python.org
Fri Oct 26 21:06:01 CEST 2007


New submission from Guido van Rossum:

Alexandre Vassalotti suggested the following:

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)?

----------
keywords: py3k
messages: 56809
nosy: gvanrossum
priority: normal
severity: normal
status: open
title: pickling bytes?
versions: Python 3.0

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1338>
__________________________________


More information about the New-bugs-announce mailing list