Skip to content

Commit 18f6dfa

Browse files
committed
ENH: add no-op fallback in np.byteswap for types with no byte order
1 parent 25f61bc commit 18f6dfa

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎numpy/_core/src/multiarray/methods.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,32 @@ PyArray_Byteswap(PyArrayObject *self, npy_bool inplace)
533533
PyArrayIterObject *it;
534534

535535
copyswapn = PyDataType_GetArrFuncs(PyArray_DESCR(self))->copyswapn;
536+
if (copyswapn == NULL) {
537+
/*
538+
* If the descriptor's byteorder is '|', byte order does not apply to
539+
* the dtype and byteswapping is a no-op. Otherwise the dtype declared
540+
* byte-order-sensitive storage and numpy cannot know how to swap its
541+
* bytes.
542+
*
543+
* TODO: add a way for new-style dtypes to ask to be byteswapped,
544+
* perhaps by casting to the opposite byteorder?
545+
*/
546+
if (PyArray_DESCR(self)->byteorder == NPY_IGNORE) {
547+
if (inplace) {
548+
if (PyArray_FailUnlessWriteable(
549+
self, "array to be byte-swapped") < 0) {
550+
return NULL;
551+
}
552+
Py_INCREF(self);
553+
return (PyObject *)self;
554+
}
555+
return PyArray_NewCopy(self, NPY_ANYORDER);
556+
}
557+
PyErr_Format(PyExc_TypeError,
558+
"dtype %S does not support byteswapping",
559+
(PyObject *)PyArray_DESCR(self));
560+
return NULL;
561+
}
536562
if (inplace) {
537563
if (PyArray_FailUnlessWriteable(self, "array to be byte-swapped") < 0) {
538564
return NULL;

‎numpy/_core/tests/test_custom_dtypes.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ def _get_array(self, scaling, aligned=True):
2424
a *= 1. / scaling # the casting code also uses the reciprocal.
2525
return a.view(SF(scaling))
2626

27+
def test_byteswap(self):
28+
# sfloat previously crashed since it does not fill the legacy
29+
# copyswapn slot; its byteorder is '|', declaring that byte order
30+
# does not apply to it, so byteswapping is a no-op
31+
a = self._get_array(1.)
32+
swapped = a.byteswap()
33+
assert swapped is not a
34+
assert_array_equal(swapped.view(np.float64), a.view(np.float64))
35+
36+
res = a.byteswap(inplace=True)
37+
assert res is a
38+
assert_array_equal(a.view(np.float64), [1., 2., 3.])
39+
2740
def test_sfloat_rescaled(self):
2841
sf = SF(1.)
2942
sf2 = sf.scaled_by(2.)

‎numpy/_core/tests/test_stringdtype.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,27 @@ def test_resize_method(string_list):
785785
assert_array_equal(sarr, np.array(string_list + [''] * 3, dtype="T"))
786786

787787

788+
def test_byteswap(dtype):
789+
# byteswap previously crashed since StringDType did not fill the
790+
# legacy copyswapn slot; byte order does not apply to stringdtype
791+
# so byteswapping is a no-op, as for "S" and object dtypes
792+
arr = np.array(["hello", "world"], dtype=dtype)
793+
swapped = arr.byteswap()
794+
assert swapped is not arr
795+
assert_array_equal(swapped, arr)
796+
# the result is an independent copy
797+
swapped[0] = "goodbye"
798+
assert arr[0] == "hello"
799+
800+
res = arr.byteswap(inplace=True)
801+
assert res is arr
802+
assert_array_equal(res, ["hello", "world"])
803+
804+
arr.flags.writeable = False
805+
with pytest.raises(ValueError, match="array to be byte-swapped"):
806+
arr.byteswap(inplace=True)
807+
808+
788809
def test_place(dtype):
789810
# stringdtype has no legacy copyswap arrfunc, so np.place copies the
790811
# values through its cast-machinery fallback

0 commit comments

Comments
 (0)