Serhiy Storchaka added the comment:

Patches updated to address Cris's  comments on Rietveld (thank you, Cris, for 
review). In additional new tests added. I think merging test_int and test_long 
is hard task and should be done in separated issue. Instead I have merged and 
simplified some test to make synchronization simpler.

Two new possible bugs were found during working on these tests (issue16772 and 
issue16773).

----------
Added file: http://bugs.python.org/file28424/int_without_x-3.3_2.patch
Added file: http://bugs.python.org/file28425/int_without_x-3.2_2.patch
Added file: http://bugs.python.org/file28426/int_without_x-2.7_2.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue16761>
_______________________________________
diff -r 10656b0975b3 Lib/test/test_int.py
--- a/Lib/test/test_int.py      Mon Dec 24 13:16:47 2012 +0200
+++ b/Lib/test/test_int.py      Mon Dec 24 21:40:02 2012 +0200
@@ -74,8 +74,6 @@
         self.assertEqual(x >> 1, x//2)
 
         self.assertRaises(ValueError, int, '123\0')
-        self.assertRaises(ValueError, int, '53', 40)
-
         # SF bug 1545497: embedded NULs were not detected with
         # explicit base
         self.assertRaises(ValueError, int, '123\0', 10)
@@ -85,7 +83,8 @@
         self.assertIsInstance(x, int)
 
 
-        self.assertRaises(TypeError, int, 1, 12)
+        self.assertRaises(TypeError, int, 1, 10)
+        self.assertRaises(TypeError, int, 1, 0)
 
         self.assertEqual(int('0o123', 0), 83)
         self.assertEqual(int('0x123', 16), 291)
@@ -100,10 +99,6 @@
         self.assertRaises(ValueError, int, "0b", 2)
         self.assertRaises(ValueError, int, "0b", 0)
 
-        # Bug #3236: Return small longs from PyLong_FromString
-        self.assertTrue(int("10") is 10)
-        self.assertTrue(int("-1") is -1)
-
         # SF bug 1334662: int(string, base) wrong answers
         # Various representations of 2**32 evaluated to 0
         # rather than 2**32 in previous versions
@@ -221,24 +216,22 @@
         self.assertEqual(int('2br45qc', 35), 4294967297)
         self.assertEqual(int('1z141z5', 36), 4294967297)
 
+    @support.cpython_only
+    def test_small_ints(self):
+        # Bug #3236: Return small longs from PyLong_FromString
+        self.assertIs(int("10"), 10)
+        self.assertIs(int("-1"), -1)
+
     def test_no_args(self):
-        self.assertEquals(int(), 0)
+        self.assertEqual(int(), 0)
 
     def test_keyword_args(self):
         # Test invoking int() using keyword arguments.
-        self.assertEquals(int(x=1.2), 1)
-        self.assertEquals(int('100', base=2), 4)
-        self.assertEquals(int(x='100', base=2), 4)
-
-    # For example, PyPy 1.9.0 raised TypeError for these cases because it
-    # expects x to be a string if base is given.
-    @support.cpython_only
-    def test_base_arg_with_no_x_arg(self):
-        self.assertEquals(int(base=6), 0)
-        # Even invalid bases don't raise an exception.
-        self.assertEquals(int(base=1), 0)
-        self.assertEquals(int(base=1000), 0)
-        self.assertEquals(int(base='foo'), 0)
+        self.assertEqual(int(x=1.2), 1)
+        self.assertEqual(int('100', base=2), 4)
+        self.assertEqual(int(x='100', base=2), 4)
+        self.assertRaises(TypeError, int, base=10)
+        self.assertRaises(TypeError, int, base=0)
 
     def test_non_numeric_input_types(self):
         # Test possible non-numeric types for the argument x, including
@@ -254,13 +247,36 @@
                   CustomByteArray(b'100')]
 
         for x in values:
-            msg = 'x has type %s' % type(x).__name__
-            self.assertEquals(int(x), 100, msg=msg)
-            self.assertEquals(int(x, 2), 4, msg=msg)
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(int(x), 100, msg=msg)
+                self.assertEqual(int(x, 2), 4, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+
+        from collections import UserString
+        values = [UserString('100')]
+        for x in values:
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(int(x), 100, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+            self.assertRaises(TypeError, int, x, 2)
 
     def test_string_float(self):
         self.assertRaises(ValueError, int, '1.2')
 
+    def test_invalid_base(self):
+        for base in -1, 1, 37, 2**1000, -2**1000:
+            self.assertRaises(ValueError, int, '100', base)
+        for base in '10',:
+            self.assertRaises(TypeError, int, '100', base)
+        # XXX ?
+        self.assertEqual(int('100', 10.5), 100)
+
     def test_intconversion(self):
         # Test __int__()
         class ClassicMissingMethods:
diff -r 10656b0975b3 Objects/longobject.c
--- a/Objects/longobject.c      Mon Dec 24 13:16:47 2012 +0200
+++ b/Objects/longobject.c      Mon Dec 24 21:40:02 2012 +0200
@@ -4267,8 +4267,14 @@
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist,
                                      &x, &obase))
         return NULL;
-    if (x == NULL)
+    if (x == NULL) {
+        if (obase != NULL) {
+            PyErr_SetString(PyExc_TypeError,
+                            "int() missing string argument");
+            return NULL;
+        }
         return PyLong_FromLong(0L);
+    }
     if (obase == NULL)
         return PyNumber_Long(x);
 
@@ -4277,7 +4283,7 @@
         return NULL;
     if (overflow || (base != 0 && base < 2) || base > 36) {
         PyErr_SetString(PyExc_ValueError,
-                        "int() arg 2 must be >= 2 and <= 36");
+                        "int() base must be >= 2 and <= 36");
         return NULL;
     }
 
diff -r ffe091ebd5de Lib/test/test_int.py
--- a/Lib/test/test_int.py      Mon Dec 24 13:15:43 2012 +0200
+++ b/Lib/test/test_int.py      Mon Dec 24 21:43:43 2012 +0200
@@ -1,6 +1,7 @@
 import sys
 
 import unittest
+from test import support
 from test.support import run_unittest
 
 L = [
@@ -74,8 +75,6 @@
         self.assertEqual(x >> 1, x//2)
 
         self.assertRaises(ValueError, int, '123\0')
-        self.assertRaises(ValueError, int, '53', 40)
-
         # SF bug 1545497: embedded NULs were not detected with
         # explicit base
         self.assertRaises(ValueError, int, '123\0', 10)
@@ -85,7 +84,8 @@
         self.assertIsInstance(x, int)
 
 
-        self.assertRaises(TypeError, int, 1, 12)
+        self.assertRaises(TypeError, int, 1, 10)
+        self.assertRaises(TypeError, int, 1, 0)
 
         self.assertEqual(int('0o123', 0), 83)
         self.assertEqual(int('0x123', 16), 291)
@@ -100,10 +100,6 @@
         self.assertRaises(ValueError, int, "0b", 2)
         self.assertRaises(ValueError, int, "0b", 0)
 
-        # Bug #3236: Return small longs from PyLong_FromString
-        self.assertTrue(int("10") is 10)
-        self.assertTrue(int("-1") is -1)
-
         # SF bug 1334662: int(string, base) wrong answers
         # Various representations of 2**32 evaluated to 0
         # rather than 2**32 in previous versions
@@ -221,6 +217,67 @@
         self.assertEqual(int('2br45qc', 35), 4294967297)
         self.assertEqual(int('1z141z5', 36), 4294967297)
 
+    @support.cpython_only
+    def test_small_ints(self):
+        # Bug #3236: Return small longs from PyLong_FromString
+        self.assertIs(int("10"), 10)
+        self.assertIs(int("-1"), -1)
+
+    def test_no_args(self):
+        self.assertEqual(int(), 0)
+
+    def test_keyword_args(self):
+        # Test invoking int() using keyword arguments.
+        self.assertEqual(int(x=1.2), 1)
+        self.assertEqual(int('100', base=2), 4)
+        self.assertEqual(int(x='100', base=2), 4)
+        self.assertRaises(TypeError, int, base=10)
+        self.assertRaises(TypeError, int, base=0)
+
+    def test_non_numeric_input_types(self):
+        # Test possible non-numeric types for the argument x, including
+        # subclasses of the explicitly documented accepted types.
+        class CustomStr(str): pass
+        class CustomBytes(bytes): pass
+        class CustomByteArray(bytearray): pass
+
+        values = [b'100',
+                  bytearray(b'100'),
+                  CustomStr('100'),
+                  CustomBytes(b'100'),
+                  CustomByteArray(b'100')]
+
+        for x in values:
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(int(x), 100, msg=msg)
+                self.assertEqual(int(x, 2), 4, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+
+        from collections import UserString
+        values = [UserString('100')]
+        for x in values:
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(int(x), 100, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+            self.assertRaises(TypeError, int, x, 2)
+
+    def test_string_float(self):
+        self.assertRaises(ValueError, int, '1.2')
+
+    def test_invalid_base(self):
+        for base in -1, 1, 37, 2**1000, -2**1000:
+            self.assertRaises(ValueError, int, '100', base)
+        for base in '10',:
+            self.assertRaises(TypeError, int, '100', base)
+        # XXX ?
+        self.assertEqual(int('100', 10.5), 100)
+
     def test_intconversion(self):
         # Test __int__()
         class ClassicMissingMethods:
diff -r ffe091ebd5de Objects/longobject.c
--- a/Objects/longobject.c      Mon Dec 24 13:15:43 2012 +0200
+++ b/Objects/longobject.c      Mon Dec 24 21:43:43 2012 +0200
@@ -4130,8 +4130,14 @@
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist,
                                      &x, &obase))
         return NULL;
-    if (x == NULL)
+    if (x == NULL) {
+        if (obase != NULL) {
+            PyErr_SetString(PyExc_TypeError,
+                            "int() missing string argument");
+            return NULL;
+        }
         return PyLong_FromLong(0L);
+    }
     if (obase == NULL)
         return PyNumber_Long(x);
 
@@ -4140,7 +4146,7 @@
         return NULL;
     if (overflow || (base != 0 && base < 2) || base > 36) {
         PyErr_SetString(PyExc_ValueError,
-                        "int() arg 2 must be >= 2 and <= 36");
+                        "int() base must be >= 2 and <= 36");
         return NULL;
     }
 
diff -r 133f87a7dbf5 Lib/test/test_int.py
--- a/Lib/test/test_int.py      Mon Dec 24 13:17:59 2012 +0200
+++ b/Lib/test/test_int.py      Mon Dec 24 21:42:08 2012 +0200
@@ -95,7 +95,6 @@
         self.assertEqual(x >> 1, x//2)
 
         self.assertRaises(ValueError, int, '123\0')
-        self.assertRaises(ValueError, int, '53', 40)
 
         # SF bug 1545497: embedded NULs were not detected with
         # explicit base
@@ -109,7 +108,8 @@
             x = int(unichr(0x661) * 600)
             self.assertIsInstance(x, long)
 
-        self.assertRaises(TypeError, int, 1, 12)
+        self.assertRaises(TypeError, int, 1, 10)
+        self.assertRaises(TypeError, int, 1, 0)
 
         self.assertEqual(int('0123', 0), 83)
         self.assertEqual(int('0x123', 16), 291)
@@ -316,18 +316,26 @@
         self.assertEqual(int(float(2**54+10)), 2**54+8)
         self.assertEqual(int(float(2**54+11)), 2**54+12)
 
+    @test_support.cpython_only
+    def test_small_ints(self):
+        # Bug #3236: Return small longs from PyLong_FromString
+        self.assertTrue(int("10") is 10)
+        self.assertTrue(int("-1") is -1)
+
     def test_no_args(self):
-        self.assertEquals(int(), 0)
+        self.assertEqual(int(), 0)
 
     def test_keyword_args(self):
         # Test invoking int() using keyword arguments.
-        self.assertEquals(int(x=1.2), 1)
-        self.assertEquals(int('100', base=2), 4)
-        self.assertEquals(int(x='100', base=2), 4)
+        self.assertEqual(int(x=1.2), 1)
+        self.assertEqual(int('100', base=2), 4)
+        self.assertEqual(int(x='100', base=2), 4)
+        self.assertRaises(TypeError, int, base=10)
+        self.assertRaises(TypeError, int, base=0)
 
-    def test_valid_non_numeric_input_types_for_x(self):
-        # Test possible valid non-numeric types for x, including subclasses
-        # of the allowed built-in types.
+    def test_non_numeric_input_types(self):
+        # Test possible non-numeric types for the argument x, including
+        # subclasses of the explicitly documented accepted types.
         class CustomStr(str): pass
         values = ['100', CustomStr('100')]
 
@@ -336,38 +344,35 @@
             values += [unicode('100'), CustomUnicode(unicode('100'))]
 
         for x in values:
-            msg = 'x has value %s and type %s' % (x, type(x).__name__)
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
             try:
-                self.assertEquals(int(x), 100, msg=msg)
-                self.assertEquals(int(x, 2), 4, msg=msg)
-            except TypeError, err:
+                self.assertEqual(int(x), 100, msg=msg)
+                self.assertEqual(int(x, 2), 4, msg=msg)
+            except TypeError as err:
                 raise AssertionError('For %s got TypeError: %s' %
                                      (type(x).__name__, err))
 
-    def test_error_on_string_float_for_x(self):
+        from UserString import UserString, MutableString
+        values = [bytearray('100'), UserString('100'), MutableString('100')]
+        for x in values:
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(int(x), 100, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+            self.assertRaises(TypeError, int, x, 2)
+
+    def test_string_float(self):
         self.assertRaises(ValueError, int, '1.2')
 
-    def test_error_on_bytearray_for_x(self):
-        self.assertRaises(TypeError, int, bytearray('100'), 2)
-
-    def test_error_on_invalid_int_bases(self):
-        for base in [-1, 1, 1000]:
+    def test_invalid_base(self):
+        for base in -1, 1, 37:
             self.assertRaises(ValueError, int, '100', base)
-
-    def test_error_on_string_base(self):
-        self.assertRaises(TypeError, int, 100, base='foo')
-        # Include the following because in contrast CPython raises no error
-        # for bad integer bases when x is not given.
-        self.assertRaises(TypeError, int, base='foo')
-
-    # For example, PyPy 1.9.0 raised TypeError for these cases because it
-    # expects x to be a string if base is given.
-    @test_support.cpython_only
-    def test_int_base_without_x_returns_0(self):
-        self.assertEquals(int(base=6), 0)
-        # Even invalid bases don't raise an exception.
-        self.assertEquals(int(base=1), 0)
-        self.assertEquals(int(base=1000), 0)
+        for base in 2**1000, -2**1000:
+            self.assertRaises(OverflowError, int, '100', base)
+        for base in 10.5, '10':
+            self.assertRaises(TypeError, int, '100', base)
 
     def test_intconversion(self):
         # Test __int__()
diff -r 133f87a7dbf5 Lib/test/test_long.py
--- a/Lib/test/test_long.py     Mon Dec 24 13:17:59 2012 +0200
+++ b/Lib/test/test_long.py     Mon Dec 24 21:42:08 2012 +0200
@@ -353,8 +353,8 @@
                         pass
 
         self.assertRaises(ValueError, long, '123\0')
-        self.assertRaises(ValueError, long, '53', 40)
-        self.assertRaises(TypeError, long, 1, 12)
+        self.assertRaises(TypeError, long, 1, 10)
+        self.assertRaises(TypeError, long, 1, 0)
 
         # tests with base 0
         self.assertEqual(long(' 0123  ', 0), 83)
@@ -452,6 +452,57 @@
         self.assertEqual(long('2br45qc', 35), 4294967297)
         self.assertEqual(long('1z141z5', 36), 4294967297)
 
+    def test_no_args(self):
+        self.assertEqual(long(), 0)
+
+    def test_keyword_args(self):
+        # Test invoking long() using keyword arguments.
+        self.assertEqual(long(x=1.2), 1)
+        self.assertEqual(long('100', base=2), 4)
+        self.assertEqual(long(x='100', base=2), 4)
+        self.assertRaises(TypeError, long, base=10)
+        self.assertRaises(TypeError, long, base=0)
+
+    def test_non_numeric_input_types(self):
+        # Test possible non-numeric types for the argument x, including
+        # subclasses of the explicitly documented accepted types.
+        class CustomStr(str): pass
+        values = ['100', CustomStr('100')]
+
+        if test_support.have_unicode:
+            class CustomUnicode(unicode): pass
+            values += [unicode('100'), CustomUnicode(unicode('100'))]
+
+        for x in values:
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(long(x), 100, msg=msg)
+                self.assertEqual(long(x, 2), 4, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+
+        from UserString import UserString, MutableString
+        values = [bytearray('100'), UserString('100'), MutableString('100')]
+        for x in values:
+            msg = 'x has value %r and type %s' % (x, type(x).__name__)
+            try:
+                self.assertEqual(long(x), 100, msg=msg)
+            except TypeError as err:
+                raise AssertionError('For %s got TypeError: %s' %
+                                     (type(x).__name__, err))
+            self.assertRaises(TypeError, long, x, 2)
+
+    def test_string_float(self):
+        self.assertRaises(ValueError, long, '1.2')
+
+    def test_invalid_base(self):
+        for base in -1, 1, 37:
+            self.assertRaises(ValueError, long, '100', base)
+        for base in 2**1000, -2**1000:
+            self.assertRaises(OverflowError, long, '100', base)
+        for base in 10.5, '10':
+            self.assertRaises(TypeError, long, '100', base)
 
     def test_conversion(self):
         # Test __long__()
diff -r 133f87a7dbf5 Objects/intobject.c
--- a/Objects/intobject.c       Mon Dec 24 13:17:59 2012 +0200
+++ b/Objects/intobject.c       Mon Dec 24 21:42:08 2012 +0200
@@ -1059,8 +1059,14 @@
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
                                      &x, &base))
         return NULL;
-    if (x == NULL)
+    if (x == NULL) {
+        if (base != -909) {
+            PyErr_SetString(PyExc_TypeError,
+                            "int() missing string argument");
+            return NULL;
+        }
         return PyInt_FromLong(0L);
+    }
     if (base == -909)
         return PyNumber_Int(x);
     if (PyString_Check(x)) {
diff -r 133f87a7dbf5 Objects/longobject.c
--- a/Objects/longobject.c      Mon Dec 24 13:17:59 2012 +0200
+++ b/Objects/longobject.c      Mon Dec 24 21:42:08 2012 +0200
@@ -3987,8 +3987,14 @@
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
                                      &x, &base))
         return NULL;
-    if (x == NULL)
+    if (x == NULL) {
+        if (base != -909) {
+            PyErr_SetString(PyExc_TypeError,
+                            "long() missing string argument");
+            return NULL;
+        }
         return PyLong_FromLong(0L);
+    }
     if (base == -909)
         return PyNumber_Long(x);
     else if (PyString_Check(x)) {
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to