Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f347cf9

Browse files
committed
merge heads
2 parents 73b969e + 8e42e8a commit f347cf9

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,8 @@ def test_input_tty_non_ascii_unicode_errors(self):
11721172
# Check stdin/stdout error handler is used when invoking GNU readline
11731173
self.check_input_tty("prompté", b"quux\xe9", "ascii")
11741174

1175+
# test_int(): see test_int.py for tests of built-in function int().
1176+
11751177
def test_repr(self):
11761178
self.assertEqual(repr(''), '\'\'')
11771179
self.assertEqual(repr(0), '0')

Lib/test/test_int.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
import unittest
4-
from test.support import run_unittest
4+
from test import support
55

66
L = [
77
('0', 0),
@@ -221,6 +221,46 @@ def test_basic(self):
221221
self.assertEqual(int('2br45qc', 35), 4294967297)
222222
self.assertEqual(int('1z141z5', 36), 4294967297)
223223

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+
224264
def test_intconversion(self):
225265
# Test __int__()
226266
class ClassicMissingMethods:
@@ -328,7 +368,7 @@ def test_error_message(self):
328368
self.fail("Expected int(%r) to raise a ValueError", s)
329369

330370
def test_main():
331-
run_unittest(IntTestCases)
371+
support.run_unittest(IntTestCases)
332372

333373
if __name__ == "__main__":
334374
test_main()

0 commit comments

Comments
 (0)