]> granicus.if.org Git - python/commitdiff
Issue #12744: Fix inefficient representation of integers
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 13 Aug 2011 18:15:19 +0000 (20:15 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 13 Aug 2011 18:15:19 +0000 (20:15 +0200)
between 2**31 and 2**63 on systems with a 64-bit C "long".

Lib/test/pickletester.py
Misc/NEWS
Modules/_pickle.c

index f90d34823d42ab9f8785ef2b93eb8d970c0128ae..ad15fe8ecd4089517150605da570c93754a4a861 100644 (file)
@@ -1118,6 +1118,16 @@ class AbstractPickleTests(unittest.TestCase):
         empty = self.loads(b'\x80\x03U\x00q\x00.', encoding='koi8-r')
         self.assertEqual(empty, '')
 
+    def test_int_pickling_efficiency(self):
+        # Test compacity of int representation (see issue #12744)
+        for proto in protocols:
+            sizes = [len(self.dumps(2**n, proto)) for n in range(70)]
+            # the size function is monotonous
+            self.assertEqual(sorted(sizes), sizes)
+            if proto >= 2:
+                self.assertLessEqual(sizes[-1], 14)
+
+
 # Test classes for reduce_ex
 
 class REX_one(object):
index 3c137e0506eeefe0f8c44ee73d5b4357aad5637d..453529dbae987227f32b1271e5e9e9e5b94caaff 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -254,6 +254,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12744: Fix inefficient representation of integers between 2**31 and
+  2**63 on systems with a 64-bit C "long".
+
 - Issue #12646: Add an 'eof' attribute to zlib.Decompress, to make it easier to
   detect truncated input streams.
 
index f147e3ec3c79756b0996aa513748cc75d12e789e..5952c1bf7a319bf40b4428f9b7f9f7bb66325503 100644 (file)
@@ -1540,7 +1540,7 @@ save_long(PicklerObject *self, PyObject *obj)
         /* out of range for int pickling */
         PyErr_Clear();
     }
-    else
+    else if (val <= 0x7fffffffL && val >= -0x80000000L)
         return save_int(self, val);
 
     if (self->proto >= 2) {