@@ -533,23 +533,44 @@ cdef validate_output_shape(iter_shape, np.ndarray output):
533
533
)
534
534
535
535
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
+ """
537
554
if out is None :
538
555
return
539
556
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
+ )
543
564
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 ))
546
567
if size is not None :
547
568
try :
548
569
tup_size = tuple (size )
549
570
except TypeError :
550
571
tup_size = tuple ([size ])
551
572
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' )
553
574
554
575
555
576
cdef 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
565
586
return out_val
566
587
567
588
if out is not None :
568
- check_output (out , np .float64 , size )
589
+ check_output (out , np .float64 , size , False )
569
590
out_array = < np .ndarray > out
570
591
else :
571
592
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
587
608
return random_func (state )
588
609
589
610
if out is not None :
590
- check_output (out , np .float32 , size )
611
+ check_output (out , np .float32 , size , False )
591
612
out_array = < np .ndarray > out
592
613
else :
593
614
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
610
631
return < float > random_func (state )
611
632
612
633
if out is not None :
613
- check_output (out , np .float32 , size )
634
+ check_output (out , np .float32 , size , False )
614
635
out_array = < np .ndarray > out
615
636
else :
616
637
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,
826
847
cdef np .ndarray a_arr , b_arr , c_arr
827
848
cdef double _a = 0.0 , _b = 0.0 , _c = 0.0
828
849
cdef bint is_scalar = True
829
- check_output (out , np .float64 , size )
850
+ check_output (out , np .float64 , size , narg > 0 )
830
851
if narg > 0 :
831
852
a_arr = < np .ndarray > np .PyArray_FROM_OTF (a , np .NPY_DOUBLE , api .NPY_ARRAY_ALIGNED )
832
853
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,
1276
1297
cdef float _a
1277
1298
cdef bint is_scalar = True
1278
1299
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 )
1280
1301
a_arr = < np .ndarray > np .PyArray_FROMANY (a , np .NPY_FLOAT32 , 0 , 0 , requirements )
1281
1302
is_scalar = np .PyArray_NDIM (a_arr ) == 0
1282
1303
0 commit comments