Skip to content

Commit 9e6ac83

Browse files
bpo-29773: Add more cases for testing string to float conversion errors. (python#580)
1 parent 0f6d733 commit 9e6ac83

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

Lib/test/test_float.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,27 @@ def test_float_memoryview(self):
119119
self.assertEqual(float(memoryview(b'12.34')[1:4]), 2.3)
120120

121121
def test_error_message(self):
122-
testlist = ('\xbd', '123\xbd', ' 123 456 ')
123-
for s in testlist:
124-
try:
122+
def check(s):
123+
with self.assertRaises(ValueError, msg='float(%r)' % (s,)) as cm:
125124
float(s)
126-
except ValueError as e:
127-
self.assertIn(s.strip(), e.args[0])
128-
else:
129-
self.fail("Expected int(%r) to raise a ValueError", s)
130-
125+
self.assertEqual(str(cm.exception),
126+
'could not convert string to float: %r' % (s,))
127+
128+
check('\xbd')
129+
check('123\xbd')
130+
check(' 123 456 ')
131+
check(b' 123 456 ')
132+
133+
# non-ascii digits (error came from non-digit '!')
134+
check('\u0663\u0661\u0664!')
135+
# embedded NUL
136+
check('123\x00')
137+
check('123\x00 245')
138+
check('123\x00245')
139+
# byte string with embedded NUL
140+
check(b'123\x00')
141+
# non-UTF-8 byte string
142+
check(b'123\xa0')
131143

132144
@support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
133145
def test_float_with_comma(self):

0 commit comments

Comments
 (0)