[Python-checkins] cpython: Issue #12744: Fix inefficient representation of integers

antoine.pitrou python-checkins at python.org
Sat Aug 13 20:19:40 CEST 2011


http://hg.python.org/cpython/rev/8e824e09924a
changeset:   71860:8e824e09924a
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Aug 13 20:15:19 2011 +0200
summary:
  Issue #12744: Fix inefficient representation of integers
between 2**31 and 2**63 on systems with a 64-bit C "long".

files:
  Lib/test/pickletester.py |  10 ++++++++++
  Misc/NEWS                |   3 +++
  Modules/_pickle.c        |   2 +-
  3 files changed, 14 insertions(+), 1 deletions(-)


diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1118,6 +1118,16 @@
         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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -254,6 +254,9 @@
 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.
 
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1540,7 +1540,7 @@
         /* out of range for int pickling */
         PyErr_Clear();
     }
-    else
+    else if (val <= 0x7fffffffL && val >= -0x80000000L)
         return save_int(self, val);
 
     if (self->proto >= 2) {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list