summaryrefslogtreecommitdiffstats
path: root/Lib/pickle.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2003-01-27 22:47:53 (GMT)
committerGuido van Rossum <guido@python.org>2003-01-27 22:47:53 (GMT)
commitf29d3d6011e41b40282994375454f2020a429d79 (patch)
treee53afa499b68e0826bfa00dc93f1ca494fb8e9f4 /Lib/pickle.py
parent99d4abf8a27fee6531a5abb76c7a6ff875f547c2 (diff)
downloadcpython-f29d3d6011e41b40282994375454f2020a429d79.zip
cpython-f29d3d6011e41b40282994375454f2020a429d79.tar.gz
cpython-f29d3d6011e41b40282994375454f2020a429d79.tar.bz2
Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
The protocol now defaults to 1. Protocol 2 is still unimplemented.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 79ee8af..9352283 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -36,8 +36,14 @@ import re
__all__ = ["PickleError", "PicklingError", "UnpicklingError", "Pickler",
"Unpickler", "dump", "dumps", "load", "loads"]
-format_version = "1.3" # File format version we write
-compatible_formats = ["1.0", "1.1", "1.2"] # Old format versions we can read
+# These are purely informational; no code usues these
+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
mloads = marshal.loads
@@ -151,12 +157,18 @@ _quotes = ["'", '"']
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.
- The optional bin parameter if true, tells the pickler to use the more
- efficient binary pickle format, otherwise the ASCII format is used
- (this is the default).
+ The optional proto argument tells the pickler to use the given
+ protocol; supported protocols are 0, 1, 2. 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
string argument. It can thus be an open file object, a StringIO
@@ -165,7 +177,8 @@ class Pickler:
"""
self.write = file.write
self.memo = {}
- self.bin = bin
+ self.proto = proto
+ self.bin = proto >= 1
def clear_memo(self):
"""Clears the pickler's "memo".
@@ -1070,12 +1083,12 @@ try:
except ImportError:
from StringIO import StringIO
-def dump(object, file, bin = 0):
- Pickler(file, bin).dump(object)
+def dump(object, file, proto=1):
+ Pickler(file, proto).dump(object)
-def dumps(object, bin = 0):
+def dumps(object, proto=1):
file = StringIO()
- Pickler(file, bin).dump(object)
+ Pickler(file, proto).dump(object)
return file.getvalue()
def load(file):