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; }