summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-04-05 19:30:08 (GMT)
committerGuido van Rossum <guido@python.org>2002-04-05 19:30:08 (GMT)
commite276339cea6a8ea9ac9ff72ed9e1b8740eceac49 (patch)
treeb6bed6eb8aae111a9d2fe7cbb000c50a881719e3 /Lib
parentd15a0a05d3161930928355e0b89091994bc54ee5 (diff)
downloadcpython-e276339cea6a8ea9ac9ff72ed9e1b8740eceac49.zip
cpython-e276339cea6a8ea9ac9ff72ed9e1b8740eceac49.tar.gz
cpython-e276339cea6a8ea9ac9ff72ed9e1b8740eceac49.tar.bz2
Implement an idea by Paul Rubin:
Change pickling format for bools to use a backwards compatible encoding. This means you can pickle True or False on Python 2.3 and Python 2.2 or before will read it back as 1 or 0. The code used for pickling bools before would create pickles that could not be read in previous Python versions.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pickle.py26
-rw-r--r--Lib/test/test_bool.py6
2 files changed, 18 insertions, 14 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 96ee5c1..541624a 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -101,8 +101,8 @@ TUPLE = 't'
EMPTY_TUPLE = ')'
SETITEMS = 'u'
BINFLOAT = 'G'
-TRUE = 'Z'
-FALSE = 'z'
+TRUE = 'I01\n'
+FALSE = 'I00\n'
__all__.extend([x for x in dir() if re.match("[A-Z][A-Z0-9_]+$",x)])
@@ -639,20 +639,18 @@ class Unpickler:
self.append(None)
dispatch[NONE] = load_none
- def load_false(self):
- self.append(False)
- dispatch[FALSE] = load_false
-
- def load_true(self):
- self.append(True)
- dispatch[TRUE] = load_true
-
def load_int(self):
data = self.readline()
- try:
- self.append(int(data))
- except ValueError:
- self.append(long(data))
+ if data == FALSE[1:]:
+ val = False
+ elif data == TRUE[1:]:
+ val = True
+ else:
+ try:
+ val = int(data)
+ except ValueError:
+ val = long(data)
+ self.append(val)
dispatch[INT] = load_int
def load_binint(self):
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index ee5d5fa..0a60a3b 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -224,5 +224,11 @@ veris(pickle.loads(cPickle.dumps(False)), False)
veris(cPickle.loads(pickle.dumps(True)), True)
veris(cPickle.loads(pickle.dumps(False)), False)
+# Test for specific backwards-compatible pickle values
+vereq(pickle.dumps(True), "I01\n.")
+vereq(pickle.dumps(False), "I00\n.")
+vereq(cPickle.dumps(True), "I01\n.")
+vereq(cPickle.dumps(False), "I00\n.")
+
if verbose:
print "All OK"