|
1 | 1 | import sys |
2 | 2 |
|
3 | 3 | import unittest |
4 | | -from test.support import run_unittest |
| 4 | +from test import support |
5 | 5 |
|
6 | 6 | L = [ |
7 | 7 | ('0', 0), |
@@ -221,6 +221,46 @@ def test_basic(self): |
221 | 221 | self.assertEqual(int('2br45qc', 35), 4294967297) |
222 | 222 | self.assertEqual(int('1z141z5', 36), 4294967297) |
223 | 223 |
|
| 224 | + def test_no_args(self): |
| 225 | + self.assertEquals(int(), 0) |
| 226 | + |
| 227 | + def test_keyword_args(self): |
| 228 | + # Test invoking int() using keyword arguments. |
| 229 | + self.assertEquals(int(x=1.2), 1) |
| 230 | + self.assertEquals(int('100', base=2), 4) |
| 231 | + self.assertEquals(int(x='100', base=2), 4) |
| 232 | + |
| 233 | + # For example, PyPy 1.9.0 raised TypeError for these cases because it |
| 234 | + # expects x to be a string if base is given. |
| 235 | + @support.cpython_only |
| 236 | + def test_base_arg_with_no_x_arg(self): |
| 237 | + self.assertEquals(int(base=6), 0) |
| 238 | + # Even invalid bases don't raise an exception. |
| 239 | + self.assertEquals(int(base=1), 0) |
| 240 | + self.assertEquals(int(base=1000), 0) |
| 241 | + self.assertEquals(int(base='foo'), 0) |
| 242 | + |
| 243 | + def test_non_numeric_input_types(self): |
| 244 | + # Test possible non-numeric types for the argument x, including |
| 245 | + # subclasses of the explicitly documented accepted types. |
| 246 | + class CustomStr(str): pass |
| 247 | + class CustomBytes(bytes): pass |
| 248 | + class CustomByteArray(bytearray): pass |
| 249 | + |
| 250 | + values = [b'100', |
| 251 | + bytearray(b'100'), |
| 252 | + CustomStr('100'), |
| 253 | + CustomBytes(b'100'), |
| 254 | + CustomByteArray(b'100')] |
| 255 | + |
| 256 | + for x in values: |
| 257 | + msg = 'x has type %s' % type(x).__name__ |
| 258 | + self.assertEquals(int(x), 100, msg=msg) |
| 259 | + self.assertEquals(int(x, 2), 4, msg=msg) |
| 260 | + |
| 261 | + def test_string_float(self): |
| 262 | + self.assertRaises(ValueError, int, '1.2') |
| 263 | + |
224 | 264 | def test_intconversion(self): |
225 | 265 | # Test __int__() |
226 | 266 | class ClassicMissingMethods: |
@@ -328,7 +368,7 @@ def test_error_message(self): |
328 | 368 | self.fail("Expected int(%r) to raise a ValueError", s) |
329 | 369 |
|
330 | 370 | def test_main(): |
331 | | - run_unittest(IntTestCases) |
| 371 | + support.run_unittest(IntTestCases) |
332 | 372 |
|
333 | 373 | if __name__ == "__main__": |
334 | 374 | test_main() |
0 commit comments