Skip to content

bpo-29773: Add more cases for testing string to float conversion errors. #580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,27 @@ def test_float_memoryview(self):
self.assertEqual(float(memoryview(b'12.34')[1:4]), 2.3)

def test_error_message(self):
testlist = ('\xbd', '123\xbd', ' 123 456 ')
for s in testlist:
try:
def check(s):
with self.assertRaises(ValueError, msg='float(%r)' % (s,)) as cm:
float(s)
except ValueError as e:
self.assertIn(s.strip(), e.args[0])
else:
self.fail("Expected int(%r) to raise a ValueError", s)

self.assertEqual(str(cm.exception),
'could not convert string to float: %r' % (s,))

check('\xbd')
check('123\xbd')
check(' 123 456 ')
check(b' 123 456 ')

# non-ascii digits (error came from non-digit '!')
check('\u0663\u0661\u0664!')
# embedded NUL
check('123\x00')
check('123\x00 245')
check('123\x00245')
# byte string with embedded NUL
check(b'123\x00')
# non-UTF-8 byte string
check(b'123\xa0')

@support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
def test_float_with_comma(self):
Expand Down