From e0dd41c0a7a4ec75f6f00dc6ef33f67964ea14b3 Mon Sep 17 00:00:00 2001 From: Mark Wiebe Date: Thu, 6 Dec 2012 15:02:37 -0800 Subject: [PATCH 1/2] BUG: Attempt to fix sparc segfault (gh-2668) --- numpy/core/src/multiarray/dtype_transfer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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); From c95da7d48b2b6ff135b15bb9cd169e7e7bc692ef Mon Sep 17 00:00:00 2001 From: Mark Wiebe Date: Sun, 9 Dec 2012 13:26:47 -0800 Subject: [PATCH 2/2] TST: Add a test for bug gh-2668 This is the test attached to the bug from Matthew Brett. I tried to make a simpler test, but it didn't repro the problem. --- numpy/core/tests/test_regression.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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()