Commit c137916c authored by Andrew Brown's avatar Andrew Brown Committed by GitHub

Add error handling to __Pyx__PyNumber_Float() (GH-4144)

The implementation of __Pyx__PyNumber_Float() wasn't checking whether
errors occurred on the calls to __Pyx_PyUnicode_AsDouble(),
__Pyx_PyBites_AsDouble(), and __Pyx_PyByteArray_AsDouble(). Errors from
those functions were now properly propagated to the caller.

Closes https://github.com/cython/cython/issues/4143
parent 0e80efb8
......@@ -475,12 +475,16 @@ static CYTHON_INLINE PyObject* __Pyx__PyNumber_Float(PyObject* obj); /* proto */
static CYTHON_INLINE PyObject* __Pyx__PyNumber_Float(PyObject* obj) {
// obj is PyFloat is handled in the calling macro
double val;
if (PyUnicode_CheckExact(obj)) {
return PyFloat_FromDouble(__Pyx_PyUnicode_AsDouble(obj));
val = __Pyx_PyUnicode_AsDouble(obj);
return unlikely(val == -1 && PyErr_Occurred()) ? NULL : PyFloat_FromDouble(val);
} else if (PyBytes_CheckExact(obj)) {
return PyFloat_FromDouble(__Pyx_PyBytes_AsDouble(obj));
val = __Pyx_PyBytes_AsDouble(obj);
return unlikely(val == -1 && PyErr_Occurred()) ? NULL : PyFloat_FromDouble(val);
} else if (PyByteArray_CheckExact(obj)) {
return PyFloat_FromDouble(__Pyx_PyByteArray_AsDouble(obj));
val = __Pyx_PyByteArray_AsDouble(obj);
return unlikely(val == -1 && PyErr_Occurred()) ? NULL : PyFloat_FromDouble(val);
} else {
return PyNumber_Float(obj);
}
......
......@@ -234,3 +234,22 @@ def from_unicode_literals():
(123.0, 123.23, 123.45, 1e+100, 123.23)
"""
return float(u"123"), float(u"123.23"), float(fix_underscores(u"12_3.4_5")), float(u"1e100"), float(u"123.23\N{PUNCTUATION SPACE}")
def catch_valueerror(val):
"""
>>> catch_valueerror("foo")
False
>>> catch_valueerror(u"foo")
False
>>> catch_valueerror(b"foo")
False
>>> catch_valueerror(bytearray(b"foo"))
False
>>> catch_valueerror("-1")
-1.0
"""
try:
return float(val)
except ValueError:
return False
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment