Permalink
Browse files

Merge pull request #2794 from mwiebe/complex_copy_misalignment

BUG: Attempt to fix sparc segfault (gh-2668)
  • Loading branch information...
certik committed Dec 14, 2012
2 parents 5db0088 + c95da7d commit 4f7d3ff253888b9508f1ec06b8fea6ce1d92b502
Showing with 31 additions and 0 deletions.
  1. +14 −0 numpy/core/src/multiarray/dtype_transfer.c
  2. +17 −0 numpy/core/tests/test_regression.py
@@ -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);
@@ -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()

0 comments on commit 4f7d3ff

Please sign in to comment.