From d370379186e1639ff43b245c54ed56dbdc8ab96c Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 22 Oct 1998 20:15:36 +0000 Subject: [PATCH] Jim Fulton writes: """ I've attached a long overdue patch to pickle.py to bring it to format 1.3, which is the same as 1.2 except that the binary float format is supported. This is done using the new platform-indepent format features of struct. This patch also gets rid of the undocumented obsolete Pickler dump_special method. """ --- Lib/pickle.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Lib/pickle.py b/Lib/pickle.py index a4e442aba8b..fda4bd7caa4 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -27,10 +27,13 @@ from types import * from copy_reg import dispatch_table, safe_constructors -import string, marshal, sys +import string +import marshal +import sys +import struct -format_version = "1.2" # File format version we write -compatible_formats = ["1.0", "1.1"] # Old format versions we can read +format_version = "1.3" # File format version we write +compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read mdumps = marshal.dumps mloads = marshal.loads @@ -81,6 +84,7 @@ TUPLE = 't' EMPTY_TUPLE = ')' SETITEMS = 'u' +BINFLOAT = 'G' class Pickler: @@ -93,14 +97,6 @@ def dump(self, object): self.save(object) self.write(STOP) - def dump_special(self, callable, args, state = None): - if type(args) is not TupleType and args is not None: - raise PicklingError, "Second argument to dump_special " \ - "must be a tuple" - - self.save_reduce(callable, args, state) - self.write(STOP) - def put(self, i): if (self.bin): s = mdumps(i)[1:] @@ -252,8 +248,11 @@ def save_long(self, object): self.write(LONG + `object` + '\n') dispatch[LongType] = save_long - def save_float(self, object): - self.write(FLOAT + `object` + '\n') + def save_float(self, object, pack=struct.pack): + if self.bin: + self.write(BINFLOAT + pack('>d', object)) + else: + self.write(FLOAT + `object` + '\n') dispatch[FloatType] = save_float def save_string(self, object): @@ -552,6 +551,10 @@ def load_float(self): self.append(string.atof(self.readline()[:-1])) dispatch[FLOAT] = load_float + def load_binfloat(self, unpack=struct.unpack): + self.append(unpack('>d', self.read(8))[0]) + dispatch[BINFLOAT] = load_binfloat + def load_string(self): self.append(eval(self.readline()[:-1], {'__builtins__': {}})) # Let's be careful