Skip to content

Commit 0e0614c

Browse files
committed
change here
1 parent d76ef3f commit 0e0614c

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

quaddtype/numpy_quaddtype/src/casts.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,21 @@ unicode_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self), PyArray_DTypeMet
181181
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2],
182182
npy_intp *view_offset)
183183
{
184-
Py_INCREF(given_descrs[0]);
185-
loop_descrs[0] = given_descrs[0];
184+
if (!PyArray_ISNBO(given_descrs[0]->byteorder)) {
185+
loop_descrs[0] = PyArray_DescrNewByteorder(given_descrs[0], NPY_NATIVE);
186+
if (loop_descrs[0] == nullptr) {
187+
return (NPY_CASTING)-1;
188+
}
189+
}
190+
else {
191+
Py_INCREF(given_descrs[0]);
192+
loop_descrs[0] = given_descrs[0];
193+
}
186194

187195
if (given_descrs[1] == NULL) {
188196
loop_descrs[1] = (PyArray_Descr *)new_quaddtype_instance(BACKEND_SLEEF);
189197
if (loop_descrs[1] == nullptr) {
198+
Py_DECREF(loop_descrs[0]);
190199
return (NPY_CASTING)-1;
191200
}
192201
}

quaddtype/tests/test_quaddtype.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,20 @@ def test_cast_string_to_quad_roundtrip(self, input_val, strtype):
601601

602602
class TestStringParsingEdgeCases:
603603
"""Test edge cases in NumPyOS_ascii_strtoq string parsing"""
604-
604+
@pytest.mark.parametrize("input_str", ['3.14', '-2.71', '0.0', '1e10', '-1e-10'])
605+
@pytest.mark.parametrize("byte_order", ['<', '>'])
606+
def test_numeric_string_parsing(self, input_str, byte_order):
607+
"""Test that numeric strings are parsed correctly regardless of byte order"""
608+
strtype = np.dtype(f'{byte_order}U20')
609+
arr = np.array([input_str], dtype=strtype)
610+
result = arr.astype(QuadPrecDType())
611+
612+
expected = np.array(input_str, dtype=np.float64)
613+
614+
np.testing.assert_allclose(result, expected,
615+
err_msg=f"Failed parsing '{input_str}' with byte order '{byte_order}'")
616+
617+
605618
@pytest.mark.parametrize("input_str,expected_sign", [
606619
("inf", 1),
607620
("+inf", 1),

0 commit comments

Comments
 (0)