Skip to content

Commit 2336bdd

Browse files
Fix Issue python#1769: Now int('- 1') or int('+ 1') is not allowed
any more. Thanks Juan Jose Conti. Also added tests.
1 parent 587c2bf commit 2336bdd

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

Lib/test/test_builtin.py

+35-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BitBucket:
4949
def write(self, line):
5050
pass
5151

52-
L = [
52+
test_conv_no_sign = [
5353
('0', 0),
5454
('1', 1),
5555
('9', 9),
@@ -71,6 +71,28 @@ def write(self, line):
7171
(chr(0x200), ValueError),
7272
]
7373

74+
test_conv_sign = [
75+
('0', 0),
76+
('1', 1),
77+
('9', 9),
78+
('10', 10),
79+
('99', 99),
80+
('100', 100),
81+
('314', 314),
82+
(' 314', ValueError),
83+
('314 ', 314),
84+
(' \t\t 314 \t\t ', ValueError),
85+
(repr(sys.maxsize), sys.maxsize),
86+
(' 1x', ValueError),
87+
(' 1 ', ValueError),
88+
(' 1\02 ', ValueError),
89+
('', ValueError),
90+
(' ', ValueError),
91+
(' \t\t ', ValueError),
92+
(str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
93+
(chr(0x200), ValueError),
94+
]
95+
7496
class TestFailingBool:
7597
def __bool__(self):
7698
raise RuntimeError
@@ -641,8 +663,18 @@ def test_int(self):
641663
# Different base:
642664
self.assertEqual(int("10",16), 16)
643665
# Test conversion from strings and various anomalies
644-
for s, v in L:
645-
for sign in "", "+", "-":
666+
# Testing with no sign at front
667+
for s, v in test_conv_no_sign:
668+
for prefix in "", " ", "\t", " \t\t ":
669+
ss = prefix + s
670+
vv = v
671+
try:
672+
self.assertEqual(int(ss), vv)
673+
except v:
674+
pass
675+
# No whitespaces allowed between + or - sign and the number
676+
for s, v in test_conv_sign:
677+
for sign in "+", "-":
646678
for prefix in "", " ", "\t", " \t\t ":
647679
ss = prefix + sign + s
648680
vv = v

Misc/NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 3.0a3?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #1769: Now int("- 1") is not allowed any more.
16+
1517
- Object/longobject.c: long(float('nan')) raises an OverflowError instead
1618
of returning 0.
1719

Objects/longobject.c

-2
Original file line numberDiff line numberDiff line change
@@ -1685,8 +1685,6 @@ PyLong_FromString(char *str, char **pend, int base)
16851685
++str;
16861686
sign = -1;
16871687
}
1688-
while (*str != '\0' && isspace(Py_CHARMASK(*str)))
1689-
str++;
16901688
if (base == 0) {
16911689
if (str[0] != '0')
16921690
base = 10;

0 commit comments

Comments
 (0)