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