Skip to content

The ValueError for converting an all-whitespace string to a float misleadingly displays it as the empty string #95605

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

Closed
Zeturic opened this issue Aug 3, 2022 · 1 comment · Fixed by #95665
Labels
3.10 only security fixes 3.11 only security fixes 3.12 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@Zeturic
Copy link

Zeturic commented Aug 3, 2022

Bug report

Actual behavior:

>>> float('\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: ''

Expected behavior:

>>> float('\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '\n'

As long as the string isn't completely whitespace, it does display properly. For example:

>>> float('\nn')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '\nn'

In addition to newlines, the same thing can be observed with tabs and carriage returns, or any combination of the three. I didn't test any additional whitespace characters, though they would likely behave the same way.

Your environment

  • CPython versions tested on: 3.10.5
  • Operating system and architecture: Windows 10 (64-bit)
@mdickinson
Copy link
Member

mdickinson commented Aug 4, 2022

Thanks for the report. This is caused by an unfortunate interaction between float_from_string_inner and PyOS_string_to_double. (The PyOS_string_to_double API leaves something to be desired, with its multi-channel reporting of errors, but it's part of the limited API so we can't change it.)

Specifically, after the PyOS_string_to_double call, if end == last in float_from_string_inner, then either the parse was successful, or the string being parsed was empty; conversely, if the parse was successful then end == last. So when we raise on end != last, we catch only parse failures on nonempty strings. The parse failure on an empty string just propagates the ValueError already raised in PyOS_string_to_double, which has an inferior error message.

#95665 should fix this.

@mdickinson mdickinson added 3.11 only security fixes 3.10 only security fixes 3.12 only security fixes labels Aug 5, 2022
mdickinson added a commit that referenced this issue Aug 10, 2022
…ace (GH-95665)

This PR fixes the error message from float(s) in the case where s contains only whitespace.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 10, 2022
…hitespace (pythonGH-95665)

This PR fixes the error message from float(s) in the case where s contains only whitespace.
(cherry picked from commit 97e9cfa)

Co-authored-by: Mark Dickinson <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 10, 2022
…hitespace (pythonGH-95665)

This PR fixes the error message from float(s) in the case where s contains only whitespace.
(cherry picked from commit 97e9cfa)

Co-authored-by: Mark Dickinson <[email protected]>
mdickinson added a commit that referenced this issue Aug 10, 2022
…ace (GH-95665) (GH-95859)

This PR fixes the error message from float(s) in the case where s contains only whitespace.
(cherry picked from commit 97e9cfa)

Co-authored-by: Mark Dickinson <[email protected]>
mdickinson added a commit that referenced this issue Aug 11, 2022
…ace (GH-95665) (GH-95858)

This PR fixes the error message from float(s) in the case where s contains only whitespace.
(cherry picked from commit 97e9cfa)

Co-authored-by: Mark Dickinson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 only security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants