Skip to content

Commit

Permalink
deprecate PyArray::from_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 25, 2024
1 parent 2817305 commit ac40b93
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
26 changes: 20 additions & 6 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,18 @@ impl<T: Element> PyArray<T, Ix1> {
vec.into_pyarray_bound(py).into_gil_ref()
}

/// Deprecated form of [`PyArray<T, Ix1>::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<Item = T>,
{
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,
Expand All @@ -1114,20 +1126,20 @@ impl<T: Element> PyArray<T, Ix1> {
/// # 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<I>(py: Python<'_>, iter: I) -> Bound<'_, Self>
where
I: IntoIterator<Item = T>,
{
let data = iter.into_iter().collect::<Vec<_>>();
data.into_pyarray_bound(py).into_gil_ref()
data.into_pyarray_bound(py)
}
}

Expand Down Expand Up @@ -1288,13 +1300,14 @@ impl<T: Element, D> PyArray<T, D> {
/// # 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());
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions tests/to_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit ac40b93

Please sign in to comment.