@@ -533,23 +533,44 @@ cdef validate_output_shape(iter_shape, np.ndarray output):
533533 )
534534
535535
536- cdef check_output (object out , object dtype , object size ):
536+ cdef check_output (object out , object dtype , object size , bint require_c_array ):
537+ """
538+ Check user-supplied output array properties and shape
539+
540+ Parameters
541+ ----------
542+ out : {ndarray, None}
543+ The array to check. If None, returns immediately.
544+ dtype : dtype
545+ The required dtype of out.
546+ size : {None, int, tuple[int]}
547+ The size passed. If out is an ndarray, verifies that the shape of out
548+ matches size.
549+ require_c_array : bool
550+ Whether out must be a C-array. If False, out can be either C- or F-
551+ ordered. If True, must be C-ordered. In either case, must be
552+ contiguous, writable, aligned and in native byte-order.
553+ """
537554 if out is None :
538555 return
539556 cdef np .ndarray out_array = < np .ndarray > out
540- if not (np .PyArray_CHKFLAGS (out_array , api .NPY_ARRAY_CARRAY ) or
541- np .PyArray_CHKFLAGS (out_array , api .NPY_ARRAY_FARRAY )):
542- raise ValueError ("Supplied output array is not contiguous, writable or aligned." )
557+ if not (np .PyArray_ISCARRAY (out_array ) or
558+ (np .PyArray_ISFARRAY (out_array ) and not require_c_array )):
559+ req = "C-" if require_c_array else ""
560+ raise ValueError (
561+ f'Supplied output array must be { req } contiguous, writable, '
562+ f'aligned, and in machine byte-order.'
563+ )
543564 if out_array .dtype != dtype :
544- raise TypeError (" Supplied output array has the wrong type. "
545- " Expected {0}, got {0}" .format (dtype , out_array .dtype ))
565+ raise TypeError (' Supplied output array has the wrong type. '
566+ ' Expected {0}, got {1}' .format (np . dtype ( dtype ) , out_array .dtype ))
546567 if size is not None :
547568 try :
548569 tup_size = tuple (size )
549570 except TypeError :
550571 tup_size = tuple ([size ])
551572 if tup_size != out .shape :
552- raise ValueError (" size must match out.shape when used together" )
573+ raise ValueError (' size must match out.shape when used together' )
553574
554575
555576cdef object double_fill (void * func , bitgen_t * state , object size , object lock , object out ):
@@ -565,7 +586,7 @@ cdef object double_fill(void *func, bitgen_t *state, object size, object lock, o
565586 return out_val
566587
567588 if out is not None :
568- check_output (out , np .float64 , size )
589+ check_output (out , np .float64 , size , False )
569590 out_array = < np .ndarray > out
570591 else :
571592 out_array = < np .ndarray > np .empty (size , np .double )
@@ -587,7 +608,7 @@ cdef object float_fill(void *func, bitgen_t *state, object size, object lock, ob
587608 return random_func (state )
588609
589610 if out is not None :
590- check_output (out , np .float32 , size )
611+ check_output (out , np .float32 , size , False )
591612 out_array = < np .ndarray > out
592613 else :
593614 out_array = < np .ndarray > np .empty (size , np .float32 )
@@ -610,7 +631,7 @@ cdef object float_fill_from_double(void *func, bitgen_t *state, object size, obj
610631 return < float > random_func (state )
611632
612633 if out is not None :
613- check_output (out , np .float32 , size )
634+ check_output (out , np .float32 , size , False )
614635 out_array = < np .ndarray > out
615636 else :
616637 out_array = < np .ndarray > np .empty (size , np .float32 )
@@ -826,7 +847,7 @@ cdef object cont(void *func, void *state, object size, object lock, int narg,
826847 cdef np .ndarray a_arr , b_arr , c_arr
827848 cdef double _a = 0.0 , _b = 0.0 , _c = 0.0
828849 cdef bint is_scalar = True
829- check_output (out , np .float64 , size )
850+ check_output (out , np .float64 , size , narg > 0 )
830851 if narg > 0 :
831852 a_arr = < np .ndarray > np .PyArray_FROM_OTF (a , np .NPY_DOUBLE , api .NPY_ARRAY_ALIGNED )
832853 is_scalar = is_scalar and np .PyArray_NDIM (a_arr ) == 0
@@ -1276,7 +1297,7 @@ cdef object cont_f(void *func, bitgen_t *state, object size, object lock,
12761297 cdef float _a
12771298 cdef bint is_scalar = True
12781299 cdef int requirements = api .NPY_ARRAY_ALIGNED | api .NPY_ARRAY_FORCECAST
1279- check_output (out , np .float32 , size )
1300+ check_output (out , np .float32 , size , True )
12801301 a_arr = < np .ndarray > np .PyArray_FROMANY (a , np .NPY_FLOAT32 , 0 , 0 , requirements )
12811302 is_scalar = np .PyArray_NDIM (a_arr ) == 0
12821303
0 commit comments