This PR fixes the error message from float(s) in the case where s contains only whitespace.
(cherry picked from commit
97e9cfa75a80b54a0630b7371f35e368a12749d1)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
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!')
--- /dev/null
+Fix misleading contents of error message when converting an all-whitespace
+string to :class:`float`.
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--;
}