From: Serhiy Storchaka Date: Fri, 28 Dec 2012 08:02:42 +0000 (+0200) Subject: Issue #16761: Raise TypeError when int() called with base argument only. X-Git-Tag: v3.3.1rc1~467 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=00e284311565c2caeadd10faa75cbe261b71bdaf;p=thirdparty%2FPython%2Fcpython.git Issue #16761: Raise TypeError when int() called with base argument only. --- 00e284311565c2caeadd10faa75cbe261b71bdaf diff --cc Lib/test/test_int.py index 52705de438a2,671b20a58411..c35a42f59673 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@@ -233,38 -231,9 +233,30 @@@ class IntTestCases(unittest.TestCase) self.assertEqual(int(x=1.2), 1) self.assertEqual(int('100', base=2), 4) self.assertEqual(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.assertEqual(int(base=6), 0) - # Even invalid bases don't raise an exception. - self.assertEqual(int(base=1), 0) - self.assertEqual(int(base=1000), 0) - self.assertEqual(int(base='foo'), 0) + 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.assertEqual(int(x), 100, msg=msg) + self.assertEqual(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: