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. """
This commit is contained in:
parent
cf1daad891
commit
d370379186
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue