]> granicus.if.org Git - python/commitdiff
Issue #3236: Return small longs from PyLong_FromString.
authorMartin v. Löwis <martin@v.loewis.de>
Mon, 30 Jun 2008 04:06:08 +0000 (04:06 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Mon, 30 Jun 2008 04:06:08 +0000 (04:06 +0000)
Lib/test/test_int.py
Misc/NEWS
Objects/longobject.c

index 3462540e7563868767e2c1e6c9d3df0224077a25..dd40ef6fe6df4936d694371f7e2eaf9e72171d34 100644 (file)
@@ -95,6 +95,9 @@ class IntTestCases(unittest.TestCase):
         self.assertRaises(ValueError, int, "0b", 2)
         self.assertRaises(ValueError, int, "0b", 0)
 
+        # Bug #3236: Return small longs from PyLong_FromString
+        self.assert_(int("10") is 10)
+        self.assert_(int("-1") is -1)
 
         # SF bug 1334662: int(string, base) wrong answers
         # Various representations of 2**32 evaluated to 0
index 0ba9a9c001c3ca8d3127e7cbe40a99ae74c16473..b3d4f30c66e9cb4c79f16df2933fd55702380d0e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's new in Python 3.0b2?
 Core and Builtins
 -----------------
 
+- Issue #3236: Return small longs from PyLong_FromString.
+
 Library
 -------
 
index d1c27e6b69a1c47245fb7220013643bf4fac9225..2c684cbea5e47411713a0d62805b4e39c7403ef8 100644 (file)
@@ -1981,6 +1981,14 @@ digit beyond the first.
                goto onError;
        if (pend)
                *pend = str;
+       long_normalize(z);
+       if (ABS(Py_SIZE(z)) <= 1) {
+               long res = MEDIUM_VALUE(z);
+               if (-NSMALLPOSINTS <= res && res <= NSMALLPOSINTS) {
+                       Py_DECREF(z);
+                       return PyLong_FromLong(res);
+               }
+       }
        return (PyObject *) z;
 
  onError: