On 06/06/2017 11:22 AM, Skip Montanaro wrote: > On Tue, Jun 6, 2017 at 12:27 PM, Chris Angelico <ros...@gmail.com> wrote: >> Or perhaps showing the repr of the string would be clearer. > > This would definitely be better, I think, and require no special > casing of the empty string. Still, I doubt this would ever be > backported for fear of breaking code might be out there somewhere > which relies on the current behavior. At best, it might be added to > the next 3.x release (3.7?). > > Skip > Does it make sense to open a bug report? I'm always a bit intimidated by contributing to python, but the following patch does seem to work (against a new clone of the master branch):
---------------------- $ git diff diff --git a/Python/pystrtod.c b/Python/pystrtod.c index 64d0c52..b3d84e4 100644 --- a/Python/pystrtod.c +++ b/Python/pystrtod.c @@ -355,9 +355,15 @@ PyOS_string_to_double(const char *s, "could not convert string to float: " "%.200s", s); else if (fail_pos == s) - PyErr_Format(PyExc_ValueError, - "could not convert string to float: " - "%.200s", s); + if (*s == 0) { + PyErr_Format(PyExc_ValueError, + "could not convert string to float: ''"); + } + else { + PyErr_Format(PyExc_ValueError, + "could not convert string to float: " + "%.200s", s); + } else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception) PyErr_Format(overflow_exception, "value too large to convert to float: " ---------------------- My changes feel a bit hacky. I wanted to just drop a straight repr() in, but I didn't want to change the code too much since I assume the string formatting is already there for a reason (e.g. "%.200s"). In any case, that patch seems to be working at first glance: >>> float('') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: could not convert string to float: '' >>> float('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: could not convert string to float: 'a' >>> float('1') 1.0 Cheers, Thomas -- https://mail.python.org/mailman/listinfo/python-list