diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-05-03 20:33:40 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-05-03 20:33:40 (GMT) |
commit | 725bfd8489e444aedd8dfd686a27ffc308657155 (patch) | |
tree | 99d4e0cc2953794a67a5cff491e1897723a119ff /Objects | |
parent | 75930f85df76472686a8c4eb587a2a70562f61fe (diff) | |
download | cpython-725bfd8489e444aedd8dfd686a27ffc308657155.zip cpython-725bfd8489e444aedd8dfd686a27ffc308657155.tar.gz cpython-725bfd8489e444aedd8dfd686a27ffc308657155.tar.bz2 |
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
PyOS_double_to_string, and deprecate PyOS_ascii_strtod and PyOS_ascii_atof.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/complexobject.c | 32 | ||||
-rw-r--r-- | Objects/floatobject.c | 36 |
2 files changed, 24 insertions, 44 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 691809f..4dd6151 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -799,25 +799,26 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) */ /* first look for forms starting with <float> */ - errno = 0; - z = PyOS_ascii_strtod(s, &end); - if (end == s && errno == ENOMEM) - return PyErr_NoMemory(); - if (errno == ERANGE && fabs(z) >= 1.0) - goto overflow; - + z = PyOS_string_to_double(s, &end, PyExc_OverflowError); + if (z == -1.0 && PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_ValueError)) + PyErr_Clear(); + else + return NULL; + } if (end != s) { /* all 4 forms starting with <float> land here */ s = end; if (*s == '+' || *s == '-') { /* <float><signed-float>j | <float><sign>j */ x = z; - errno = 0; - y = PyOS_ascii_strtod(s, &end); - if (end == s && errno == ENOMEM) - return PyErr_NoMemory(); - if (errno == ERANGE && fabs(y) >= 1.0) - goto overflow; + y = PyOS_string_to_double(s, &end, PyExc_OverflowError); + if (y == -1.0 && PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_ValueError)) + PyErr_Clear(); + else + return NULL; + } if (end != s) /* <float><signed-float>j */ s = end; @@ -877,11 +878,6 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) PyErr_SetString(PyExc_ValueError, "complex() arg is a malformed string"); return NULL; - - overflow: - PyErr_SetString(PyExc_OverflowError, - "complex() arg overflow"); - return NULL; } static PyObject * diff --git a/Objects/floatobject.c b/Objects/floatobject.c index fdca3be..0619877 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -193,36 +193,20 @@ PyFloat_FromString(PyObject *v) /* We don't care about overflow or underflow. If the platform * supports them, infinities and signed zeroes (on underflow) are * fine. */ - errno = 0; - PyFPE_START_PROTECT("strtod", goto error) - x = PyOS_ascii_strtod(s, (char **)&end); - PyFPE_END_PROTECT(x) - if (end == s) { - if (errno == ENOMEM) - PyErr_NoMemory(); - else { - PyOS_snprintf(buffer, sizeof(buffer), - "invalid literal for float(): %.200s", s); - PyErr_SetString(PyExc_ValueError, buffer); - } + x = PyOS_string_to_double(s, (char **)&end, NULL); + if (x == -1.0 && PyErr_Occurred()) goto error; - } - /* Since end != s, the platform made *some* kind of sense out - of the input. Trust it. */ while (*end && isspace(Py_CHARMASK(*end))) end++; - if (end != last) { - if (*end == '\0') - PyErr_SetString(PyExc_ValueError, - "null byte in argument for float()"); - else { - PyOS_snprintf(buffer, sizeof(buffer), - "invalid literal for float(): %.200s", s); - PyErr_SetString(PyExc_ValueError, buffer); - } - goto error; + if (end == last) + result = PyFloat_FromDouble(x); + else { + PyOS_snprintf(buffer, sizeof(buffer), + "invalid literal for float(): %.200s", s); + PyErr_SetString(PyExc_ValueError, buffer); + result = NULL; } - result = PyFloat_FromDouble(x); + error: if (s_buffer) PyMem_FREE(s_buffer); |