@@ -155,7 +155,7 @@ impl<T, D> PyArray<T, D> {
155
155
| self . check_flag ( npyffi:: NPY_ARRAY_F_CONTIGUOUS )
156
156
}
157
157
158
- pub fn is_forran_contiguous ( & self ) -> bool {
158
+ pub fn is_fotran_contiguous ( & self ) -> bool {
159
159
self . check_flag ( npyffi:: NPY_ARRAY_F_CONTIGUOUS )
160
160
}
161
161
@@ -180,7 +180,7 @@ impl<T, D> PyArray<T, D> {
180
180
/// }
181
181
/// let gil = Python::acquire_gil();
182
182
/// let array = return_py_array();
183
- /// assert_eq!(array.as_ref(gil.python()).as_slice(), &[0, 0, 0, 0, 0]);
183
+ /// assert_eq!(array.as_ref(gil.python()).as_slice().unwrap() , &[0, 0, 0, 0, 0]);
184
184
/// # }
185
185
/// ```
186
186
pub fn to_owned ( & self ) -> Py < Self > {
@@ -413,13 +413,24 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
413
413
}
414
414
415
415
/// Get the immutable view of the internal data of `PyArray`, as slice.
416
+ ///
417
+ /// Returns `ErrorKind::NotContiguous` if the internal array is not contiguous.
416
418
/// # Example
417
419
/// ```
418
420
/// # extern crate pyo3; extern crate numpy; fn main() {
419
- /// use numpy::PyArray;
421
+ /// use numpy::{PyArray, PyArray1};
422
+ /// use pyo3::types::IntoPyDict;
420
423
/// let gil = pyo3::Python::acquire_gil();
421
- /// let py_array = PyArray::arange(gil.python(), 0, 4, 1).reshape([2, 2]).unwrap();
422
- /// assert_eq!(py_array.as_slice(), &[0, 1, 2, 3]);
424
+ /// let py = gil.python();
425
+ /// let py_array = PyArray::arange(py, 0, 4, 1).reshape([2, 2]).unwrap();
426
+ /// assert_eq!(py_array.as_slice().unwrap(), &[0, 1, 2, 3]);
427
+ /// let locals = [("np", numpy::get_array_module(py).unwrap())].into_py_dict(py);
428
+ /// let not_contiguous: &PyArray1<f32> = py
429
+ /// .eval("np.zeros(10)[::2]", Some(locals), None)
430
+ /// .unwrap()
431
+ /// .downcast_ref()
432
+ /// .unwrap();
433
+ /// assert!(not_contiguous.as_slice().is_err());
423
434
/// # }
424
435
/// ```
425
436
pub fn as_slice ( & self ) -> Result < & [ T ] , ErrorKind > {
@@ -654,7 +665,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
654
665
/// let gil = pyo3::Python::acquire_gil();
655
666
/// let array = [1, 2, 3, 4, 5];
656
667
/// let pyarray = PyArray::from_slice(gil.python(), &array);
657
- /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
668
+ /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
658
669
/// # }
659
670
/// ```
660
671
pub fn from_slice < ' py > ( py : Python < ' py > , slice : & [ T ] ) -> & ' py Self {
@@ -675,7 +686,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
675
686
/// let gil = pyo3::Python::acquire_gil();
676
687
/// let vec = vec![1, 2, 3, 4, 5];
677
688
/// let pyarray = PyArray::from_vec(gil.python(), vec);
678
- /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
689
+ /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
679
690
/// # }
680
691
/// ```
681
692
pub fn from_vec < ' py > ( py : Python < ' py > , vec : Vec < T > ) -> & ' py Self {
@@ -693,7 +704,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
693
704
/// let gil = pyo3::Python::acquire_gil();
694
705
/// let vec = vec![1, 2, 3, 4, 5];
695
706
/// let pyarray = PyArray::from_iter(gil.python(), vec.iter().map(|&x| x));
696
- /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
707
+ /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
697
708
/// # }
698
709
/// ```
699
710
pub fn from_exact_iter ( py : Python , iter : impl ExactSizeIterator < Item = T > ) -> & Self {
@@ -720,7 +731,7 @@ impl<T: TypeNum> PyArray<T, Ix1> {
720
731
/// let gil = pyo3::Python::acquire_gil();
721
732
/// let set: BTreeSet<u32> = [4, 3, 2, 5, 1].into_iter().cloned().collect();
722
733
/// let pyarray = PyArray::from_iter(gil.python(), set);
723
- /// assert_eq!(pyarray.as_slice(), &[1, 2, 3, 4, 5]);
734
+ /// assert_eq!(pyarray.as_slice().unwrap() , &[1, 2, 3, 4, 5]);
724
735
/// # }
725
736
/// ```
726
737
pub fn from_iter ( py : Python , iter : impl IntoIterator < Item = T > ) -> & Self {
@@ -895,7 +906,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
895
906
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
896
907
/// let pyarray_i = PyArray::<i64, _>::new(gil.python(), [3], false);
897
908
/// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
898
- /// assert_eq!(pyarray_i.as_slice(), &[2, 3, 4]);
909
+ /// assert_eq!(pyarray_i.as_slice().unwrap() , &[2, 3, 4]);
899
910
/// # }
900
911
pub fn copy_to < U : TypeNum > ( & self , other : & PyArray < U , D > ) -> Result < ( ) , ErrorKind > {
901
912
let self_ptr = self . as_array_ptr ( ) ;
@@ -916,7 +927,7 @@ impl<T: TypeNum, D> PyArray<T, D> {
916
927
/// let gil = pyo3::Python::acquire_gil();
917
928
/// let pyarray_f = PyArray::arange(gil.python(), 2.0, 5.0, 1.0);
918
929
/// let pyarray_i = pyarray_f.cast::<i32>(false).unwrap();
919
- /// assert_eq!(pyarray_i.as_slice(), &[2, 3, 4]);
930
+ /// assert_eq!(pyarray_i.as_slice().unwrap() , &[2, 3, 4]);
920
931
/// # }
921
932
pub fn cast < ' py , U : TypeNum > (
922
933
& ' py self ,
@@ -1003,9 +1014,9 @@ impl<T: TypeNum + AsPrimitive<f64>> PyArray<T, Ix1> {
1003
1014
/// use numpy::PyArray;
1004
1015
/// let gil = pyo3::Python::acquire_gil();
1005
1016
/// let pyarray = PyArray::arange(gil.python(), 2.0, 4.0, 0.5);
1006
- /// assert_eq!(pyarray.as_slice(), &[2.0, 2.5, 3.0, 3.5]);
1017
+ /// assert_eq!(pyarray.as_slice().unwrap() , &[2.0, 2.5, 3.0, 3.5]);
1007
1018
/// let pyarray = PyArray::arange(gil.python(), -2, 4, 3);
1008
- /// assert_eq!(pyarray.as_slice(), &[-2, 1]);
1019
+ /// assert_eq!(pyarray.as_slice().unwrap() , &[-2, 1]);
1009
1020
/// # }
1010
1021
pub fn arange < ' py > ( py : Python < ' py > , start : T , stop : T , step : T ) -> & ' py Self {
1011
1022
unsafe {
0 commit comments