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)) {