diff --git a/numpy/core/src/multiarray/dtype_transfer.c b/numpy/core/src/multiarray/dtype_transfer.c index 01cb93562f2e..55027e3fa872 100644 --- a/numpy/core/src/multiarray/dtype_transfer.c +++ b/numpy/core/src/multiarray/dtype_transfer.c @@ -3582,6 +3582,13 @@ PyArray_GetDTypeTransferFunction(int aligned, PyArray_ISNBO(dst_dtype->byteorder)) { if (PyArray_EquivTypenums(src_type_num, dst_type_num)) { + /* + * For complex numbers, the alignment is smaller than the + * type size, so we turn off the aligned flag then. + */ + if (src_dtype->kind == 'c' || dst_dtype->kind == 'c') { + aligned = 0; + } *out_stransfer = PyArray_GetStridedCopyFn(aligned, src_stride, dst_stride, src_itemsize); @@ -3678,6 +3685,13 @@ PyArray_GetDTypeTransferFunction(int aligned, /* This is a straight copy */ if (src_itemsize == 1 || PyArray_ISNBO(src_dtype->byteorder) == PyArray_ISNBO(dst_dtype->byteorder)) { + /* + * For complex numbers, the alignment is smaller than the + * type size, so we turn off the aligned flag then. + */ + if (src_dtype->kind == 'c' || dst_dtype->kind == 'c') { + aligned = 0; + } *out_stransfer = PyArray_GetStridedCopyFn(aligned, src_stride, dst_stride, src_itemsize); diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 02b58a9a6385..adc991bd99c7 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1812,5 +1812,22 @@ def test_searchsorted_wrong_dtype(self): a = np.recarray((2, ), dtype) assert_raises(TypeError, np.searchsorted, a, 1) + def test_complex64_alignment(self): + # Issue gh-2668 (trac 2076), segfault on sparc due to misalignment + dtt = np.complex64 + arr = np.arange(10, dtype=dtt) + # 2D array + arr2 = np.reshape(arr, (2, 5)) + # Fortran write followed by (C or F) read caused bus error + data_str = arr2.tostring('F') + data_back = np.ndarray(arr2.shape, + arr2.dtype, + buffer=data_str, + order='F') + assert_array_equal(arr2, data_back) + + + + if __name__ == "__main__": run_module_suite()