diff -r 937f70110f9e Lib/test/test_int.py --- a/Lib/test/test_int.py Sun Dec 23 19:55:21 2012 +0200 +++ b/Lib/test/test_int.py Mon Dec 24 11:46:52 2012 +0200 @@ -221,6 +221,40 @@ self.assertEqual(int('2br45qc', 35), 4294967297) self.assertEqual(int('1z141z5', 36), 4294967297) + def test_no_args(self): + self.assertEquals(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) + + def test_base_arg_with_no_x_arg(self): + 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 type %s' % type(x).__name__ + self.assertEquals(int(x), 100, msg=msg) + self.assertEquals(int(x, 2), 4, msg=msg) + + def test_string_float(self): + self.assertRaises(ValueError, int, '1.2') + def test_intconversion(self): # Test __int__() class ClassicMissingMethods: diff -r 937f70110f9e Objects/longobject.c --- a/Objects/longobject.c Sun Dec 23 19:55:21 2012 +0200 +++ b/Objects/longobject.c Mon Dec 24 11:46:52 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; }