8000 gh-95605: Fix `float(s)` error message when `s` contains only whitespace by mdickinson · Pull Request #95665 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-95605: Fix float(s) error message when s contains only whitespace #95665

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
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ def check(s):
check('123\xbd')
check(' 123 456 ')
check(b' 123 456 ')
# all whitespace (cf. https://github.com/python/cpython/issues/95605)
check('')
check(' ')
check('\t \n')

# non-ascii digits (error came from non-digit '!')
check('\u0663\u0661\u0664!')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix misleading contents of error message when converting an all-whitespace
string to :class:`float`.
9 changes: 8 additions & 1 deletion Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,18 @@ float_from_string_inner(const char *s, Py_ssize_t len, void *obj)
double x;
const char *end;
const char *last = s + len;
/* strip space */
/* strip leading whitespace */
while (s < last && Py_ISSPACE(*s)) {
s++;
}
if (s == last) {
PyErr_Format(PyExc_ValueError,
"could not convert string to float: "
"%R", obj);
return NULL;
}

/* strip trailing whitespace */
while (s < last - 1 && Py_ISSPACE(last[-1])) {
last--;
}
Expand Down
0