diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-08-10 18:57:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-10 18:57:55 (GMT) |
commit | b4f968e0947c0c090c5cf7d8b9ed614f9050a0f4 (patch) | |
tree | b6a0785285615ab59385529e81b14e9270ed74d1 /Objects | |
parent | 346aa78af4145135465d68a3b647d55e7366d0b2 (diff) | |
download | cpython-b4f968e0947c0c090c5cf7d8b9ed614f9050a0f4.zip cpython-b4f968e0947c0c090c5cf7d8b9ed614f9050a0f4.tar.gz cpython-b4f968e0947c0c090c5cf7d8b9ed614f9050a0f4.tar.bz2 |
gh-95605: Fix `float(s)` error message when `s` contains only whitespace (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 97e9cfa75a80b54a0630b7371f35e368a12749d1)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/floatobject.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 5af2678..064ba2e 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -150,11 +150,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--; } |