summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r--Lib/test/test_int.py39
1 files changed, 37 insertions, 2 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index 671b20a..c35a42f 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -2,7 +2,6 @@ import sys
import unittest
from test import support
-from test.support import run_unittest
L = [
('0', 0),
@@ -226,6 +225,9 @@ class IntTestCases(unittest.TestCase):
self.assertIs(int(b'10'), 10)
self.assertIs(int(b'-1'), -1)
+ def test_no_args(self):
+ self.assertEqual(int(), 0)
+
def test_keyword_args(self):
# Test invoking int() using keyword arguments.
self.assertEqual(int(x=1.2), 1)
@@ -234,6 +236,27 @@ class IntTestCases(unittest.TestCase):
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:
@@ -318,6 +341,18 @@ class IntTestCases(unittest.TestCase):
self.fail("Failed to raise TypeError with %s" %
((base, trunc_result_base),))
+ # Regression test for bugs.python.org/issue16060.
+ class BadInt(trunc_result_base):
+ def __int__(self):
+ return 42.0
+
+ class TruncReturnsBadInt(base):
+ def __trunc__(self):
+ return BadInt()
+
+ with self.assertRaises(TypeError):
+ int(TruncReturnsBadInt())
+
def test_error_message(self):
testlist = ('\xbd', '123\xbd', ' 123 456 ')
for s in testlist:
@@ -329,7 +364,7 @@ class IntTestCases(unittest.TestCase):
self.fail("Expected int(%r) to raise a ValueError", s)
def test_main():
- run_unittest(IntTestCases)
+ support.run_unittest(IntTestCases)
if __name__ == "__main__":
test_main()