]> granicus.if.org Git - python/commitdiff
Fix Issue #1769: Now int('- 1') or int('+ 1') is not allowed
authorFacundo Batista <facundobatista@gmail.com>
Sat, 19 Jan 2008 19:12:01 +0000 (19:12 +0000)
committerFacundo Batista <facundobatista@gmail.com>
Sat, 19 Jan 2008 19:12:01 +0000 (19:12 +0000)
any more.  Thanks Juan Jose Conti.  Also added tests.

Lib/test/test_builtin.py
Misc/NEWS
Objects/longobject.c

index b10caf511b8dd56225171230b7239567fd5a0eb8..4cf59166c42d5ebbcb57d60dcea7b0b3a594dc62 100644 (file)
@@ -49,7 +49,7 @@ class BitBucket:
     def write(self, line):
         pass
 
-L = [
+test_conv_no_sign = [
         ('0', 0),
         ('1', 1),
         ('9', 9),
@@ -71,6 +71,28 @@ L = [
         (chr(0x200), ValueError),
 ]
 
+test_conv_sign = [
+        ('0', 0),
+        ('1', 1),
+        ('9', 9),
+        ('10', 10),
+        ('99', 99),
+        ('100', 100),
+        ('314', 314),
+        (' 314', ValueError),
+        ('314 ', 314),
+        ('  \t\t  314  \t\t  ', ValueError),
+        (repr(sys.maxsize), sys.maxsize),
+        ('  1x', ValueError),
+        ('  1  ', ValueError),
+        ('  1\02  ', ValueError),
+        ('', ValueError),
+        (' ', ValueError),
+        ('  \t\t  ', ValueError),
+        (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
+        (chr(0x200), ValueError),
+]
+
 class TestFailingBool:
     def __bool__(self):
         raise RuntimeError
@@ -641,8 +663,18 @@ class BuiltinTest(unittest.TestCase):
         # Different base:
         self.assertEqual(int("10",16), 16)
         # Test conversion from strings and various anomalies
-        for s, v in L:
-            for sign in "", "+", "-":
+        # Testing with no sign at front
+        for s, v in test_conv_no_sign:
+            for prefix in "", " ", "\t", "  \t\t  ":
+                ss = prefix + s
+                vv = v
+                try:
+                    self.assertEqual(int(ss), vv)
+                except v:
+                    pass
+        # No whitespaces allowed between + or - sign and the number
+        for s, v in test_conv_sign:
+            for sign in "+", "-":
                 for prefix in "", " ", "\t", "  \t\t  ":
                     ss = prefix + sign + s
                     vv = v
index c59efa1b435bb977662fd395f8693f1ffd152d18..cb6288ae19ea698c393cdbd8a71089235389a58b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.0a3?
 Core and Builtins
 -----------------
 
+- Issue #1769: Now int("- 1") is not allowed any more.
+
 - Object/longobject.c: long(float('nan')) raises an OverflowError instead
   of returning 0.
 
index 4b7eee059cd7ea4198fa3492069835242589e39b..18f158a78a467b6973f97c0e1572cec2c94d4ea5 100644 (file)
@@ -1685,8 +1685,6 @@ PyLong_FromString(char *str, char **pend, int base)
                ++str;
                sign = -1;
        }
-       while (*str != '\0' && isspace(Py_CHARMASK(*str)))
-               str++;
        if (base == 0) {
                if (str[0] != '0')
                        base = 10;