You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The ValueError for converting an all-whitespace string to a float misleadingly displays it as the empty string #95605
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)
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.
Activity
mdickinson commentedon Aug 4, 2022
Thanks for the report. This is caused by an unfortunate interaction between
float_from_string_innerandPyOS_string_to_double. (ThePyOS_string_to_doubleAPI 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_doublecall, ifend == lastinfloat_from_string_inner, then either the parse was successful, or the string being parsed was empty; conversely, if the parse was successful thenend == last. So when we raise onend != last, we catch only parse failures on nonempty strings. The parse failure on an empty string just propagates theValueErroralready raised inPyOS_string_to_double, which has an inferior error message.#95665 should fix this.
gh-95605: Fix `float(s)` error message when `s` contains only whitesp…
pythongh-95605: Fix `float(s)` error message when `s` contains only w…
pythongh-95605: Fix `float(s)` error message when `s` contains only w…
gh-95605: Fix `float(s)` error message when `s` contains only whitesp…
gh-95605: Fix `float(s)` error message when `s` contains only whitesp…