diff -r c390dc999fcc Lib/test/test_int.py --- a/Lib/test/test_int.py Sun Dec 23 20:09:01 2012 +0200 +++ b/Lib/test/test_int.py Mon Dec 24 12:08:38 2012 +0200 @@ -360,14 +360,9 @@ # 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) + def test_base_arg_with_no_x_arg(self): + self.assertRaises(TypeError, int, base=10) + self.assertRaises(TypeError, int, base=0) def test_intconversion(self): # Test __int__() diff -r c390dc999fcc Lib/test/test_long.py --- a/Lib/test/test_long.py Sun Dec 23 20:09:01 2012 +0200 +++ b/Lib/test/test_long.py Mon Dec 24 12:08:38 2012 +0200 @@ -452,6 +452,40 @@ self.assertEqual(long('2br45qc', 35), 4294967297) self.assertEqual(long('1z141z5', 36), 4294967297) + def test_no_args(self): + self.assertEquals(long(), 0) + + def test_keyword_args(self): + # Test invoking long() using keyword arguments. + self.assertEquals(long(x=1.2), 1) + self.assertEquals(long('100', base=2), 4) + self.assertEquals(long(x='100', base=2), 4) + + def test_base_arg_with_no_x_arg(self): + self.assertRaises(TypeError, long, base=10) + self.assertRaises(TypeError, long, 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. + 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 %s and type %s' % (x, type(x).__name__) + try: + self.assertEquals(long(x), 100, msg=msg) + self.assertEquals(long(x, 2), 4, msg=msg) + except TypeError, err: + raise AssertionError('For %s got TypeError: %s' % + (type(x).__name__, err)) + + def test_string_float(self): + self.assertRaises(ValueError, long, '1.2') def test_conversion(self): # Test __long__() diff -r c390dc999fcc Objects/intobject.c --- a/Objects/intobject.c Sun Dec 23 20:09:01 2012 +0200 +++ b/Objects/intobject.c Mon Dec 24 12:08:38 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 c390dc999fcc Objects/longobject.c --- a/Objects/longobject.c Sun Dec 23 20:09:01 2012 +0200 +++ b/Objects/longobject.c Mon Dec 24 12:08:38 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, + "int() missing string argument"); + return NULL; + } return PyLong_FromLong(0L); + } if (base == -909) return PyNumber_Long(x); else if (PyString_Check(x)) {