Skip to content

Commit dacbe9e

Browse files
committed
BUG: Stricter check for output arrays
Ensure output arrays are contiguous and well behaved
1 parent e2ad558 commit dacbe9e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

randomstate/array_fillers.pxi.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ cdef object {{ctype}}_fill(aug_state* state, void *func, object size, object loc
2626
if out_array.dtype != np.{{nptype}}:
2727
raise TypeError('Supplied output array has the wrong type.'
2828
'Expected {{nptype}}, got {0}'.format(out_array.dtype))
29-
if not (np.PyArray_CHKFLAGS(out_array, np.NPY_C_CONTIGUOUS) or
30-
np.PyArray_CHKFLAGS(out_array, np.NPY_F_CONTIGUOUS)):
31-
raise ValueError('Supplied output array is not C contiguous')
29+
if not (np.PyArray_CHKFLAGS(out_array, np.NPY_CARRAY) or
30+
np.PyArray_CHKFLAGS(out_array, np.NPY_FARRAY)):
31+
raise ValueError('Supplied output array is not contiguous, writable or aligned.')
3232
if size is not None:
3333
# TODO: enable this !!! if tuple(size) != out_array.shape:
3434
raise ValueError('size and shape of the supplied output are different')

randomstate/tests/test_smoke.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,13 @@ def test_output_filling_exponential(self):
632632
direct = rs.standard_exponential(size=size, dtype=np.float32)
633633
assert_equal(direct, existing)
634634

635+
def test_output_fill_error(self):
636+
rs = self.rs
637+
size = (31, 7, 97)
638+
existing = np.empty(size)
639+
assert_raises(TypeError, rs.standard_normal, out=existing, dtype=np.float32)
640+
assert_raises(ValueError, rs.standard_normal, out=existing[::3])
641+
635642

636643
class TestMT19937(RNG):
637644
@classmethod

0 commit comments

Comments
 (0)