Skip to content

Commit 3375e70

Browse files
committed
byte support to scalar
1 parent 8487df3 commit 3375e70

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
219219
return NULL;
220220
}
221221
}
222+
else if (PyBytes_Check(value)) {
223+
const char *s = PyBytes_AsString(value);
224+
if (s == NULL) {
225+
Py_DECREF(self);
226+
return NULL;
227+
}
228+
char *endptr = NULL;
229+
if (backend == BACKEND_SLEEF) {
230+
self->value.sleef_value = Sleef_strtoq(s, &endptr);
231+
}
232+
else {
233+
self->value.longdouble_value = strtold(s, &endptr);
234+
}
235+
if (*endptr != '\0' || endptr == s) {
236+
PyErr_SetString(PyExc_ValueError, "Unable to parse bytes to QuadPrecision");
237+
Py_DECREF(self);
238+
return NULL;
239+
}
240+
}
222241
else if (PyLong_Check(value)) {
223242
return quad_from_py_int(value, backend, self);
224243
}
@@ -242,21 +261,21 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
242261
const char *type_cstr = PyUnicode_AsUTF8(type_str);
243262
if (type_cstr != NULL) {
244263
PyErr_Format(PyExc_TypeError,
245-
"QuadPrecision value must be a quad, float, int, string, array or sequence, but got %s "
264+
"QuadPrecision value must be a quad, float, int, string, bytes, array or sequence, but got %s "
246265
"instead",
247266
type_cstr);
248267
}
249268
else {
250269
PyErr_SetString(
251270
PyExc_TypeError,
252-
"QuadPrecision value must be a quad, float, int, string, array or sequence, but got an "
271+
"QuadPrecision value must be a quad, float, int, string, bytes, array or sequence, but got an "
253272
"unknown type instead");
254273
}
255274
Py_DECREF(type_str);
256275
}
257276
else {
258277
PyErr_SetString(PyExc_TypeError,
259-
"QuadPrecision value must be a quad, float, int, string, array or sequence, but got an "
278+
"QuadPrecision value must be a quad, float, int, string, bytes, array or sequence, but got an "
260279
"unknown type instead");
261280
}
262281
Py_DECREF(self);

0 commit comments

Comments
 (0)