summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-08-10 18:57:55 (GMT)
committerGitHub <noreply@github.com>2022-08-10 18:57:55 (GMT)
commitb4f968e0947c0c090c5cf7d8b9ed614f9050a0f4 (patch)
treeb6a0785285615ab59385529e81b14e9270ed74d1 /Objects
parent346aa78af4145135465d68a3b647d55e7366d0b2 (diff)
downloadcpython-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.c9
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--;
}