8000 Merge pull request #2794 from mwiebe/complex_copy_misalignment · numpy/numpy@4f7d3ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f7d3ff

Browse files
committed
Merge pull request #2794 from mwiebe/complex_copy_misalignment
BUG: Attempt to fix sparc segfault (gh-2668)
2 parents 5db0088 + c95da7d commit 4f7d3ff

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

numpy/core/src/multiarray/dtype_transfer.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,6 +3582,13 @@ PyArray_GetDTypeTransferFunction(int aligned,
35823582
PyArray_ISNBO(dst_dtype->byteorder)) {
35833583

35843584
if (PyArray_EquivTypenums(src_type_num, dst_type_num)) {
3585+
/*
3586+
* For complex numbers, the alignment is smaller than the
3587+
* type size, so we turn off the aligned flag then.
3588+
*/
3589+
if (src_dtype->kind == 'c' || dst_dtype->kind == 'c') {
3590+
aligned = 0;
3591+
}
35853592
*out_stransfer = PyArray_GetStridedCopyFn(aligned,
35863593
src_stride, dst_stride,
35873594
src_itemsize);
@@ -3678,6 +3685,13 @@ PyArray_GetDTypeTransferFunction(int aligned,
36783685
/* This is a straight copy */
36793686
if (src_itemsize == 1 || PyArray_ISNBO(src_dtype->byteorder) ==
36803687
PyArray_ISNBO(dst_dtype->byteorder)) {
3688+
/*
3689+
* For complex numbers, the alignment is smaller than the
3690+
* type size, so we turn off the aligned flag then.
3691+
*/
3692+
if (src_dtype->kind == 'c' || dst_dtype->kind == 'c') {
3693+
aligned = 0;
3694+
}
36813695
*out_stransfer = PyArray_GetStridedCopyFn(aligned,
36823696
src_stride, dst_stride,
36833697
src_itemsize);

numpy/core/tests/test_regression.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,5 +1812,22 @@ def test_searchsorted_wrong_dtype(self):
18121812
a = np.recarray((2, ), dtype)
18131813
assert_raises(TypeError, np.searchsorted, a, 1)
18141814

1815+
def test_complex64_alignment(self):
1816+
# Issue gh-2668 (trac 2076), segfault on sparc due to misalignment
1817+
dtt = np.complex64
1818+
arr = np.arange(10, dtype=dtt)
1819+
# 2D array
1820+
arr2 = np.reshape(arr, (2, 5))
1821+
# Fortran write followed by (C or F) read caused bus error
1822+
data_str = arr2.tostring('F')
1823+
data_back = np.ndarray(arr2.shape,
1824+
arr2.dtype,
1825+
buffer=data_str,
1826+
order='F')
1827+
assert_array_equal(arr2, data_back)
1828+
1829+
1830+
1831+
18151832
if __name__ == "__main__":
18161833
run_module_suite()

0 commit comments

Comments
 (0)
0