Message56809
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)? |
|
| Date |
User |
Action |
Args |
| 2007-10-26 19:06:01 | gvanrossum | set | spambayes_score: 0.00366992 -> 0.00366992 recipients:
+ gvanrossum |
| 2007-10-26 19:06:01 | gvanrossum | set | spambayes_score: 0.00366992 -> 0.00366992 messageid: <1193425561.23.0.273747744158.issue1338@psf.upfronthosting.co.za> |
| 2007-10-26 19:06:01 | gvanrossum | link | issue1338 messages |
| 2007-10-26 19:06:00 | gvanrossum | create | |
|