"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()!
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)
import unittest
import pickle
+import cPickle
import pickletools
import copy_reg
# 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.
#define WRITE_BUF_SIZE 256
/* Bump this when new opcodes are added to the pickle protocol. */
-#define CURRENT_PROTOCOL_NUMBER 2
+#define HIGHEST_PROTOCOL 2
/*
* Pickle opcodes. These must be kept in synch with pickle.py. Extensive
Picklerobject *self;
if (proto < 0)
- proto = CURRENT_PROTOCOL_NUMBER;
- if (proto > CURRENT_PROTOCOL_NUMBER) {
+ proto = HIGHEST_PROTOCOL;
+ if (proto > HIGHEST_PROTOCOL) {
PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
"the highest available protocol is %d",
- proto, CURRENT_PROTOCOL_NUMBER);
+ proto, HIGHEST_PROTOCOL);
return NULL;
}
* int when chewing on 1 byte.
*/
assert(i >= 0);
- if (i <= CURRENT_PROTOCOL_NUMBER)
+ if (i <= HIGHEST_PROTOCOL)
return 0;
PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
}
Py_DECREF(di);
+ i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
+ if (i < 0)
+ return;
+
/* These are purely informational; no code uses them. */
/* File format version we write. */
format_version = PyString_FromString("2.0");