]> granicus.if.org Git - python/commitdiff
pickle.py, load_int(): Match cPickle's just-repaired ability to unpickle
authorTim Peters <tim.peters@gmail.com>
Tue, 28 Aug 2001 22:21:18 +0000 (22:21 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 28 Aug 2001 22:21:18 +0000 (22:21 +0000)
64-bit INTs on 32-bit boxes (where they become longs).  Also exploit that
int(str) and long(str) will ignore a trailing newline (saves creating a
new string at the Python level).

pickletester.py:  Simulate reading a pickle produced by a 64-bit box.

Lib/pickle.py
Lib/test/pickletester.py

index 8be7a8d362120ba595ca84d78f62ee2a65358569..d5773e24c77b7e512273bec70a6c3d82c1b9f93c 100644 (file)
@@ -615,7 +615,11 @@ class Unpickler:
     dispatch[NONE] = load_none
 
     def load_int(self):
-        self.append(int(self.readline()[:-1]))
+        data = self.readline()
+        try:
+            self.append(int(data))
+        except ValueError:
+            self.append(long(data))
     dispatch[INT] = load_int
 
     def load_binint(self):
index fa3fb89d33fdc967d80d33b26d57e506eea3012e..fa3ddf43ad76e297d7fc79abde46b95709346ce9 100644 (file)
@@ -221,3 +221,18 @@ def dotest(pickle):
                                      repr(s),
                                      got))
         n = n >> 1
+
+    # Fake a pickle from a sizeof(long)==8 box.
+    maxint64 = (1L << 63) - 1
+    data = 'I' + str(maxint64) + '\n.'
+    got = pickle.loads(data)
+    if maxint64 != got:
+        raise TestFailed("maxint64 test failed %r %r" % (maxint64, got))
+    # Try too with a bogus literal.
+    data = 'I' + str(maxint64) + 'JUNK\n.'
+    try:
+        got = pickle.loads(data)
+    except ValueError:
+        pass
+    else:
+        raise TestFailed("should have raised error on bogus INT literal")