diff options
-rw-r--r-- | Lib/test/test_float.py | 4 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2022-08-04-18-46-54.gh-issue-95605.FbpCoG.rst | 2 | ||||
-rw-r--r-- | Objects/floatobject.c | 9 |
3 files changed, 14 insertions, 1 deletions
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 29f7756..4c4a8f9 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -135,6 +135,10 @@ class GeneralFloatCases(unittest.TestCase): 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!') diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-04-18-46-54.gh-issue-95605.FbpCoG.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-04-18-46-54.gh-issue-95605.FbpCoG.rst new file mode 100644 index 0000000..49441c6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-04-18-46-54.gh-issue-95605.FbpCoG.rst @@ -0,0 +1,2 @@ +Fix misleading contents of error message when converting an all-whitespace +string to :class:`float`. 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--; } |