Commit f29d3d60 authored by Guido van Rossum's avatar Guido van Rossum

Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.

The protocol now defaults to 1.  Protocol 2 is still unimplemented.
parent 99d4abf8
...@@ -36,8 +36,14 @@ import re ...@@ -36,8 +36,14 @@ import re
__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler", __all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
"Unpickler", "dump", "dumps", "load", "loads"] "Unpickler", "dump", "dumps", "load", "loads"]
format_version = "1.3" # File format version we write # These are purely informational; no code usues these
compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read format_version = "2.0" # File format version we write
compatible_formats = ["1.0", # Original protocol 0
"1.1", # Protocol 0 with class supprt added
"1.2", # Original protocol 1
"1.3", # Protocol 1 with BINFLOAT added
"2.0", # Protocol 2
] # Old format versions we can read
mdumps = marshal.dumps mdumps = marshal.dumps
mloads = marshal.loads mloads = marshal.loads
...@@ -151,12 +157,18 @@ _quotes = ["'", '"'] ...@@ -151,12 +157,18 @@ _quotes = ["'", '"']
class Pickler: class Pickler:
def __init__(self, file, bin = 0): def __init__(self, file, proto=1):
"""This takes a file-like object for writing a pickle data stream. """This takes a file-like object for writing a pickle data stream.
The optional bin parameter if true, tells the pickler to use the more The optional proto argument tells the pickler to use the given
efficient binary pickle format, otherwise the ASCII format is used protocol; supported protocols are 0, 1, 2. The default
(this is the default). protocol is 1 (in previous Python versions the default was 0).
Protocol 1 is more efficient than protocol 0; protocol 2 is
more efficient than protocol 1. Protocol 2 is not the default
because it is not supported by older Python versions.
XXX Protocol 2 is not yet implemented.
The file parameter must have a write() method that accepts a single The file parameter must have a write() method that accepts a single
string argument. It can thus be an open file object, a StringIO string argument. It can thus be an open file object, a StringIO
...@@ -165,7 +177,8 @@ class Pickler: ...@@ -165,7 +177,8 @@ class Pickler:
""" """
self.write = file.write self.write = file.write
self.memo = {} self.memo = {}
self.bin = bin self.proto = proto
self.bin = proto >= 1
def clear_memo(self): def clear_memo(self):
"""Clears the pickler's "memo". """Clears the pickler's "memo".
...@@ -1070,12 +1083,12 @@ try: ...@@ -1070,12 +1083,12 @@ try:
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
def dump(object, file, bin = 0): def dump(object, file, proto=1):
Pickler(file, bin).dump(object) Pickler(file, proto).dump(object)
def dumps(object, bin = 0): def dumps(object, proto=1):
file = StringIO() file = StringIO()
Pickler(file, bin).dump(object) Pickler(file, proto).dump(object)
return file.getvalue() return file.getvalue()
def load(file): def load(file):
......
...@@ -1902,7 +1902,7 @@ def dis(pickle, out=None, indentlevel=4): ...@@ -1902,7 +1902,7 @@ def dis(pickle, out=None, indentlevel=4):
_dis_test = """ _dis_test = """
>>> import pickle >>> import pickle
>>> x = [1, 2, (3, 4), {'abc': u"def"}] >>> x = [1, 2, (3, 4), {'abc': u"def"}]
>>> pik = pickle.dumps(x) >>> pik = pickle.dumps(x, 0)
>>> dis(pik) >>> dis(pik)
0: ( MARK 0: ( MARK
1: l LIST (MARK at 0) 1: l LIST (MARK at 0)
...@@ -1955,13 +1955,13 @@ Try again with a "binary" pickle. ...@@ -1955,13 +1955,13 @@ Try again with a "binary" pickle.
Exercise the INST/OBJ/BUILD family. Exercise the INST/OBJ/BUILD family.
>>> import random >>> import random
>>> dis(pickle.dumps(random.random)) >>> dis(pickle.dumps(random.random, 0))
0: c GLOBAL 'random random' 0: c GLOBAL 'random random'
15: p PUT 0 15: p PUT 0
18: . STOP 18: . STOP
>>> x = [pickle.PicklingError()] * 2 >>> x = [pickle.PicklingError()] * 2
>>> dis(pickle.dumps(x)) >>> dis(pickle.dumps(x, 0))
0: ( MARK 0: ( MARK
1: l LIST (MARK at 0) 1: l LIST (MARK at 0)
2: p PUT 0 2: p PUT 0
...@@ -2016,7 +2016,7 @@ True ...@@ -2016,7 +2016,7 @@ True
True True
>>> T[0][0] is T >>> T[0][0] is T
True True
>>> dis(pickle.dumps(L)) >>> dis(pickle.dumps(L, 0))
0: ( MARK 0: ( MARK
1: l LIST (MARK at 0) 1: l LIST (MARK at 0)
2: p PUT 0 2: p PUT 0
...@@ -2043,7 +2043,7 @@ doesn't trigger this glitch, because the disassembler realizes that ...@@ -2043,7 +2043,7 @@ doesn't trigger this glitch, because the disassembler realizes that
POP_MARK gets rid of the MARK. Doing a better job on the protocol 0 POP_MARK gets rid of the MARK. Doing a better job on the protocol 0
pickle would require the disassembler to emulate the stack. pickle would require the disassembler to emulate the stack.
>>> dis(pickle.dumps(T)) >>> dis(pickle.dumps(T, 0))
0: ( MARK 0: ( MARK
1: ( MARK 1: ( MARK
2: l LIST (MARK at 1) 2: l LIST (MARK at 1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment