summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-10-22 20:15:36 (GMT)
committerGuido van Rossum <guido@python.org>1998-10-22 20:15:36 (GMT)
commitd370379186e1639ff43b245c54ed56dbdc8ab96c (patch)
treee57cd3d3795901f52cf67194b0b525d4e93b0247
parentcf1daad891924413ac42d3bdcefbc973799f8302 (diff)
downloadcpython-d370379186e1639ff43b245c54ed56dbdc8ab96c.zip
cpython-d370379186e1639ff43b245c54ed56dbdc8ab96c.tar.gz
cpython-d370379186e1639ff43b245c54ed56dbdc8ab96c.tar.bz2
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. """
-rw-r--r--Lib/pickle.py29
1 files changed, 16 insertions, 13 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index a4e442a..fda4bd7 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -27,10 +27,13 @@ __version__ = "$Revision$" # Code version
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 @@ SETITEM = 's'
TUPLE = 't'
EMPTY_TUPLE = ')'
SETITEMS = 'u'
+BINFLOAT = 'G'
class Pickler:
@@ -93,14 +97,6 @@ class Pickler:
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 @@ class Pickler:
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 @@ class Unpickler:
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