Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d370379

Browse files
committed
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. """
1 parent cf1daad commit d370379

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

Lib/pickle.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727

2828
from types import *
2929
from copy_reg import dispatch_table, safe_constructors
30-
import string, marshal, sys
30+
import string
31+
import marshal
32+
import sys
33+
import struct
3134

32-
format_version = "1.2" # File format version we write
33-
compatible_formats = ["1.0", "1.1"] # Old format versions we can read
35+
format_version = "1.3" # File format version we write
36+
compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read
3437

3538
mdumps = marshal.dumps
3639
mloads = marshal.loads
@@ -81,6 +84,7 @@
8184
TUPLE = 't'
8285
EMPTY_TUPLE = ')'
8386
SETITEMS = 'u'
87+
BINFLOAT = 'G'
8488

8589
class Pickler:
8690

@@ -93,14 +97,6 @@ def dump(self, object):
9397
self.save(object)
9498
self.write(STOP)
9599

96-
def dump_special(self, callable, args, state = None):
97-
if type(args) is not TupleType and args is not None:
98-
raise PicklingError, "Second argument to dump_special " \
99-
"must be a tuple"
100-
101-
self.save_reduce(callable, args, state)
102-
self.write(STOP)
103-
104100
def put(self, i):
105101
if (self.bin):
106102
s = mdumps(i)[1:]
@@ -252,8 +248,11 @@ def save_long(self, object):
252248
self.write(LONG + `object` + '\n')
253249
dispatch[LongType] = save_long
254250

255-
def save_float(self, object):
256-
self.write(FLOAT + `object` + '\n')
251+
def save_float(self, object, pack=struct.pack):
252+
if self.bin:
253+
self.write(BINFLOAT + pack('>d', object))
254+
else:
255+
self.write(FLOAT + `object` + '\n')
257256
dispatch[FloatType] = save_float
258257

259258
def save_string(self, object):
@@ -552,6 +551,10 @@ def load_float(self):
552551
self.append(string.atof(self.readline()[:-1]))
553552
dispatch[FLOAT] = load_float
554553

554+
def load_binfloat(self, unpack=struct.unpack):
555+
self.append(unpack('>d', self.read(8))[0])
556+
dispatch[BINFLOAT] = load_binfloat
557+
555558
def load_string(self):
556559
self.append(eval(self.readline()[:-1],
557560
{'__builtins__': {}})) # Let's be careful

0 commit comments

Comments
 (0)