Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 00f5834..74748f8 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -47,6 +47,10 @@
"2.0", # Protocol 2
] # Old format versions we can read
+# Keep in synch with cPickle. This is the highest protocol number we
+# know how to read.
+HIGHEST_PROTOCOL = 2
+
# Why use struct.pack() for pickling but marshal.loads() for
# unpickling? struct.pack() is 40% faster than marshal.dumps(), but
# marshal.loads() is twice as fast as struct.unpack()!
@@ -200,9 +204,9 @@
if protocol is None:
protocol = 0
if protocol < 0:
- protocol = 2
- elif protocol not in (0, 1, 2):
- raise ValueError, "pickle protocol must be 0, 1 or 2"
+ protocol = HIGHEST_PROTOCOL
+ elif not 0 <= protocol <= HIGHEST_PROTOCOL:
+ raise ValueError("pickle protocol must be <= %d" % HIGHEST_PROTOCOL)
self.write = file.write
self.memo = {}
self.proto = int(protocol)
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 734f2a3..8479d43 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1,5 +1,6 @@
import unittest
import pickle
+import cPickle
import pickletools
import copy_reg
@@ -7,8 +8,9 @@
# Tests that try a number of pickle protocols should have a
# for proto in protocols:
-# kind of outer loop. Bump the 3 to 4 if/when protocol 3 is invented.
-protocols = range(3)
+# kind of outer loop.
+assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
+protocols = range(pickle.HIGHEST_PROTOCOL + 1)
# Return True if opcode code appears in the pickle, else False.