Skip to content

Commit

Permalink
Merge pull request numpy#2794 from mwiebe/complex_copy_misalignment
Browse files Browse the repository at this point in the history
BUG: Attempt to fix sparc segfault (numpygh-2668)
  • Loading branch information
certik committed Dec 14, 2012
2 parents 5db0088 + c95da7d commit 4f7d3ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions numpy/core/src/multiarray/dtype_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.