Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 029656f

Browse files
committed
Issue #3236: Return small longs from PyLong_FromString.
1 parent 2f5799b commit 029656f

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/test/test_int.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def test_basic(self):
9595
self.assertRaises(ValueError, int, "0b", 2)
9696
self.assertRaises(ValueError, int, "0b", 0)
9797

98+
# Bug #3236: Return small longs from PyLong_FromString
99+
self.assert_(int("10") is 10)
100+
self.assert_(int("-1") is -1)
98101

99102
# SF bug 1334662: int(string, base) wrong answers
100103
# Various representations of 2**32 evaluated to 0

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's new in Python 3.0b2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3236: Return small longs from PyLong_FromString.
16+
1517
Library
1618
-------
1719

Objects/longobject.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,14 @@ digit beyond the first.
19811981
goto onError;
19821982
if (pend)
19831983
*pend = str;
1984+
long_normalize(z);
1985+
if (ABS(Py_SIZE(z)) <= 1) {
1986+
long res = MEDIUM_VALUE(z);
1987+
if (-NSMALLPOSINTS <= res && res <= NSMALLPOSINTS) {
1988+
Py_DECREF(z);
1989+
return PyLong_FromLong(res);
1990+
}
1991+
}
19841992
return (PyObject *) z;
19851993

19861994
onError:

0 commit comments

Comments
 (0)