]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Test full range of native ints. This exposes two more binary pickle
authorTim Peters <tim.peters@gmail.com>
Tue, 10 Apr 2001 03:41:41 +0000 (03:41 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 10 Apr 2001 03:41:41 +0000 (03:41 +0000)
bugs on sizeof(long)==8 machines.  pickle.py has no idea what it's
doing with very large ints, and variously gets things right by accident,
computes nonsense, or generates corrupt pickles.  cPickle fails on
cases 2**31 <= i < 2**32:  since it *thinks* those are 4-byte ints
(the "high 4 bytes" are all zeroes), it stores them in the (signed!) BININT
format, so they get unpickled as negative values.

Lib/test/pickletester.py

index 54a43b27494a9cd0a6f4864921ec856933967566..8014efe8c1c6614e1514efa75f3dd0b857a2ddf6 100644 (file)
@@ -1,5 +1,8 @@
 # test_pickle and test_cpickle both use this.
 
+from test_support import TestFailed
+import sys
+
 # break into multiple strings to please font-lock-mode
 DATA = """(lp1
 I0
@@ -197,3 +200,19 @@ def dotest(pickle):
         else:
             if u2 != u:
                 print "Endcase failure: %s => %s" % (`u`, `u2`)
+
+    # Test the full range of Python ints.
+    n = sys.maxint
+    while n:
+        for expected in (-n, n):
+            for binary_mode in (0, 1):
+                s = pickle.dumps(expected, binary_mode)
+                got = pickle.loads(s)
+                if expected != got:
+                    raise TestFailed("for %s-mode pickle of %d, pickle "
+                                     "string is %s, loaded back as %s" % (
+                                     binary_mode and "binary" or "text",
+                                     expected,
+                                     repr(s),
+                                     got))
+        n = n >> 1