diff --git a/benches/array.rs b/benches/array.rs index 27ebe9e0b..6619c4e27 100644 --- a/benches/array.rs +++ b/benches/array.rs @@ -66,7 +66,7 @@ fn from_iter(bencher: &mut Bencher, size: usize) { iter_with_gil(bencher, |py| { let iter = black_box(Iter(0..size)); - PyArray1::from_iter(py, iter); + PyArray1::from_iter_bound(py, iter); }); } diff --git a/src/array.rs b/src/array.rs index 0d6e60e68..7a837c973 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1106,6 +1106,18 @@ impl PyArray { vec.into_pyarray_bound(py).into_gil_ref() } + /// Deprecated form of [`PyArray::from_iter_bound`] + #[deprecated( + since = "0.21.0", + note = "will be replaced by PyArray::from_iter_bound in the future" + )] + pub fn from_iter<'py, I>(py: Python<'py>, iter: I) -> &'py Self + where + I: IntoIterator, + { + Self::from_iter_bound(py, iter).into_gil_ref() + } + /// Construct a one-dimensional array from an [`Iterator`]. /// /// If no reliable [`size_hint`][Iterator::size_hint] is available, @@ -1114,20 +1126,20 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let pyarray = PyArray::from_iter(py, "abcde".chars().map(u32::from)); + /// let pyarray = PyArray::from_iter_bound(py, "abcde".chars().map(u32::from)); /// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[97, 98, 99, 100, 101]); /// }); /// ``` - pub fn from_iter<'py, I>(py: Python<'py>, iter: I) -> &'py Self + pub fn from_iter_bound(py: Python<'_>, iter: I) -> Bound<'_, Self> where I: IntoIterator, { let data = iter.into_iter().collect::>(); - data.into_pyarray_bound(py).into_gil_ref() + data.into_pyarray_bound(py) } } @@ -1288,13 +1300,14 @@ impl PyArray { /// # Example /// /// ``` + /// use numpy::prelude::*; /// use numpy::{npyffi::NPY_ORDER, PyArray}; /// use pyo3::Python; /// use ndarray::array; /// /// Python::with_gil(|py| { /// let array = - /// PyArray::from_iter(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap(); + /// PyArray::from_iter_bound(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap(); /// /// assert_eq!(array.readonly().as_array(), array![[0, 3, 6], [1, 4, 7], [2, 5, 8]]); /// assert!(array.is_fortran_contiguous()); @@ -1830,13 +1843,14 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> { /// # Example /// /// ``` + /// use numpy::prelude::*; /// use numpy::{npyffi::NPY_ORDER, PyArray}; /// use pyo3::Python; /// use ndarray::array; /// /// Python::with_gil(|py| { /// let array = - /// PyArray::from_iter(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap(); + /// PyArray::from_iter_bound(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap(); /// /// assert_eq!(array.readonly().as_array(), array![[0, 3, 6], [1, 4, 7], [2, 5, 8]]); /// assert!(array.is_fortran_contiguous()); diff --git a/tests/array.rs b/tests/array.rs index 26cfbf62c..79b5a3314 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -523,7 +523,7 @@ fn get_works() { #[test] fn reshape() { Python::with_gil(|py| { - let array = PyArray::from_iter(py, 0..9) + let array = PyArray::from_iter_bound(py, 0..9) .reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER) .unwrap(); diff --git a/tests/to_py.rs b/tests/to_py.rs index 4ab5371bf..5181aa63a 100644 --- a/tests/to_py.rs +++ b/tests/to_py.rs @@ -52,7 +52,7 @@ fn to_pyarray_array() { #[test] fn iter_to_pyarray() { Python::with_gil(|py| { - let arr = PyArray::from_iter(py, (0..10).map(|x| x * x)); + let arr = PyArray::from_iter_bound(py, (0..10).map(|x| x * x)); assert_eq!( arr.readonly().as_slice().unwrap(), @@ -64,7 +64,7 @@ fn iter_to_pyarray() { #[test] fn long_iter_to_pyarray() { Python::with_gil(|py| { - let arr = PyArray::from_iter(py, 0_u32..512); + let arr = PyArray::from_iter_bound(py, 0_u32..512); assert_eq!( arr.readonly().as_slice().unwrap(),