Skip to content

Commit 25f0338

Browse files
committed
error handling
1 parent ea0a489 commit 25f0338

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

quaddtype/numpy_quaddtype/src/dtype.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ quadprec_scanfunc(FILE *fp, void *dptr, char *ignore, PyArray_Descr *descr_gener
356356
/* Convert string to quad precision */
357357
char *endptr;
358358
quad_value val;
359-
cstring_to_quad(buffer, descr->backend, &val, &endptr);
360-
if (endptr == buffer) {
359+
int err = cstring_to_quad(buffer, descr->backend, &val, &endptr, true);
360+
if (err < 0) {
361361
return 0; /* Return 0 on parse error (no items read) */
362362
}
363363
if (descr->backend == BACKEND_SLEEF) {
@@ -375,8 +375,8 @@ quadprec_fromstr(char *s, void *dptr, char **endptr, PyArray_Descr *descr_generi
375375
{
376376
QuadPrecDTypeObject *descr = (QuadPrecDTypeObject *)descr_generic;
377377
quad_value val;
378-
cstring_to_quad(s, descr->backend, &val, endptr);
379-
if (*endptr == s) {
378+
int err = cstring_to_quad(s, descr->backend, &val, endptr, false);
379+
if (err < 0) {
380380
return -1;
381381
}
382382
if(descr->backend == BACKEND_SLEEF) {

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
197197
else if (PyUnicode_Check(value)) {
198198
const char *s = PyUnicode_AsUTF8(value);
199199
char *endptr = NULL;
200-
cstring_to_quad(s, backend, &self->value, &endptr);
201-
if (*endptr != '\0' || endptr == s) {
200+
int err = cstring_to_quad(s, backend, &self->value, &endptr, true);
201+
if (err < 0) {
202202
PyErr_SetString(PyExc_ValueError, "Unable to parse string to QuadPrecision");
203203
Py_DECREF(self);
204204
return NULL;
@@ -211,8 +211,8 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
211211
return NULL;
212212
}
213213
char *endptr = NULL;
214-
cstring_to_quad(s, backend, &self->value, &endptr);
215-
if (*endptr != '\0' || endptr == s) {
214+
int err = cstring_to_quad(s, backend, &self->value, &endptr, true);
215+
if (err < 0) {
216216
PyErr_SetString(PyExc_ValueError, "Unable to parse bytes to QuadPrecision");
217217
Py_DECREF(self);
218218
return NULL;
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#include "utilities.h"
22
#include <stdlib.h>
33

4-
void cstring_to_quad(const char *str, QuadBackendType backend, quad_value *out_value, char **endptr)
4+
int cstring_to_quad(const char *str, QuadBackendType backend, quad_value *out_value,
5+
char **endptr, bool require_full_parse)
56
{
6-
if(backend == BACKEND_SLEEF)
7-
{
7+
if(backend == BACKEND_SLEEF) {
88
out_value->sleef_value = Sleef_strtoq(str, endptr);
9-
}
10-
else
11-
{
9+
} else {
1210
out_value->longdouble_value = strtold(str, endptr);
1311
}
12+
if(*endptr == str)
13+
return -1; // parse error - nothing was parsed
14+
15+
// If full parse is required
16+
if(require_full_parse && **endptr != '\0')
17+
return -1; // parse error - characters remain to be converted
18+
19+
return 0; // success
1420
}

quaddtype/numpy_quaddtype/src/utilities.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "quad_common.h"
55
#include <sleef.h>
66
#include <sleefquad.h>
7+
#include <stdbool.h>
78

8-
void cstring_to_quad(const char *str, QuadBackendType backend, quad_value *out_value, char **endptr);
9+
int cstring_to_quad(const char *str, QuadBackendType backend, quad_value *out_value, char **endptr, bool require_full_parse);
910

1011
#endif

0 commit comments

Comments
 (0)