summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-08-03 18:14:05 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-08-03 18:14:05 (GMT)
commit579ddc2fd4fd618b72d8202d79d75d22e7e0b60a (patch)
treeb6ac773514cfbef73d62130a0d3810626fbc38e7 /Lib/test/test_int.py
parente633bed44a6354dd190c7e97e28c12dead787315 (diff)
parentf6d0aeeadce3f1aea240b900da5e1fbb430257b2 (diff)
downloadcpython-579ddc2fd4fd618b72d8202d79d75d22e7e0b60a.zip
cpython-579ddc2fd4fd618b72d8202d79d75d22e7e0b60a.tar.gz
cpython-579ddc2fd4fd618b72d8202d79d75d22e7e0b60a.tar.bz2
Issue #16741: Fix an error reporting in int().
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r--Lib/test/test_int.py47
1 files changed, 31 insertions, 16 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index 4921230..4e00622 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -73,14 +73,6 @@ class IntTestCases(unittest.TestCase):
x = -1-sys.maxsize
self.assertEqual(x >> 1, x//2)
- self.assertRaises(ValueError, int, '123\0')
- self.assertRaises(ValueError, int, '53', 40)
-
- # SF bug 1545497: embedded NULs were not detected with
- # explicit base
- self.assertRaises(ValueError, int, '123\0', 10)
- self.assertRaises(ValueError, int, '123\x00 245', 20)
-
x = int('1' * 600)
self.assertIsInstance(x, int)
@@ -401,14 +393,37 @@ class IntTestCases(unittest.TestCase):
int(TruncReturnsBadInt())
def test_error_message(self):
- testlist = ('\xbd', '123\xbd', ' 123 456 ')
- for s in testlist:
- try:
- int(s)
- except ValueError as e:
- self.assertIn(s.strip(), e.args[0])
- else:
- self.fail("Expected int(%r) to raise a ValueError", s)
+ def check(s, base=None):
+ with self.assertRaises(ValueError,
+ msg="int(%r, %r)" % (s, base)) as cm:
+ if base is None:
+ int(s)
+ else:
+ int(s, base)
+ self.assertEqual(cm.exception.args[0],
+ "invalid literal for int() with base %d: %r" %
+ (10 if base is None else base, s))
+
+ check('\xbd')
+ check('123\xbd')
+ check(' 123 456 ')
+
+ check('123\x00')
+ # SF bug 1545497: embedded NULs were not detected with explicit base
+ check('123\x00', 10)
+ check('123\x00 245', 20)
+ check('123\x00 245', 16)
+ check('123\x00245', 20)
+ check('123\x00245', 16)
+ # byte string with embedded NUL
+ check(b'123\x00')
+ check(b'123\x00', 10)
+ # non-UTF-8 byte string
+ check(b'123\xbd')
+ check(b'123\xbd', 10)
+ # lone surrogate in Unicode string
+ check('123\ud800')
+ check('123\ud800', 10)
def test_main():
support.run_unittest(IntTestCases)