summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-02-13 15:44:41 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-02-13 15:44:41 (GMT)
commit8587b3c0730236dc007e8533c3cbfccd421fa381 (patch)
treee031741700152eb780af1071669c69f397402429
parentfe62bc917d1ab51fc591839b3c65589b268cad31 (diff)
downloadcpython-8587b3c0730236dc007e8533c3cbfccd421fa381.zip
cpython-8587b3c0730236dc007e8533c3cbfccd421fa381.tar.gz
cpython-8587b3c0730236dc007e8533c3cbfccd421fa381.tar.bz2
Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
-rw-r--r--Lib/pickle.py10
-rw-r--r--Lib/test/pickletester.py6
-rw-r--r--Modules/cPickle.c14
3 files changed, 20 insertions, 10 deletions
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 @@ compatible_formats = ["1.0", # Original protocol 0
"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 @@ class Pickler:
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 @@ from test.test_support import TestFailed, have_unicode, TESTFN
# 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.
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index a9bbd36..17da7d7 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -15,7 +15,7 @@ PyDoc_STRVAR(cPickle_module_documentation,
#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
@@ -2743,11 +2743,11 @@ newPicklerobject(PyObject *file, int proto)
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;
}
@@ -4308,7 +4308,7 @@ load_proto(Unpicklerobject *self)
* 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);
@@ -5562,6 +5562,10 @@ initcPickle(void)
}
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");