Skip to content

Commit 5fad493

Browse files
[3.5] bpo-29773: Add more cases for testing string to float conversion errors. (#587)
(cherry picked from commit 9e6ac83)
1 parent 952b7cb commit 5fad493

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

Lib/test/test_float.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,27 @@ def test_float_memoryview(self):
9898
self.assertEqual(float(memoryview(b'12.34')[1:4]), 2.3)
9999

100100
def test_error_message(self):
101-
testlist = ('\xbd', '123\xbd', ' 123 456 ')
102-
for s in testlist:
103-
try:
101+
def check(s):
102+
with self.assertRaises(ValueError, msg='float(%r)' % (s,)) as cm:
104103
float(s)
105-
except ValueError as e:
106-
self.assertIn(s.strip(), e.args[0])
107-
else:
108-
self.fail("Expected int(%r) to raise a ValueError", s)
109-
104+
self.assertEqual(str(cm.exception),
105+
'could not convert string to float: %r' % (s,))
106+
107+
check('\xbd')
108+
check('123\xbd')
109+
check(' 123 456 ')
110+
check(b' 123 456 ')
111+
112+
# non-ascii digits (error came from non-digit '!')
113+
check('\u0663\u0661\u0664!')
114+
# embedded NUL
115+
check('123\x00')
116+
check('123\x00 245')
117+
check('123\x00245')
118+
# byte string with embedded NUL
119+
check(b'123\x00')
120+
# non-UTF-8 byte string
121+
check(b'123\xa0')
110122

111123
@support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
112124
def test_float_with_comma(self):

0 commit comments

Comments
 (0)