From 029656fb3bc23a2ce03a81fe30bc1b1e5ce5e4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Mon, 30 Jun 2008 04:06:08 +0000 Subject: Issue #3236: Return small longs from PyLong_FromString. --- Lib/test/test_int.py | 3 +++ Misc/NEWS | 2 ++ Objects/longobject.c | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 3462540..dd40ef6 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -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 diff --git a/Misc/NEWS b/Misc/NEWS index 0ba9a9c..b3d4f30 100644 --- 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 ------- diff --git a/Objects/longobject.c b/Objects/longobject.c index d1c27e6..2c684cb 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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: -- cgit v0.12