[Python-3000] Last call for PEP 3137: Immutable Bytes and Mutable Buffer
Alexandre Vassalotti
alexandre at peadrop.com
Mon Oct 1 04:44:31 CEST 2007
On 9/30/07, Guido van Rossum <guido at python.org> 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
More information about the Python-3000
mailing list