Skip to content

Commit 6234a72

Browse files
Icxoluadamreichold
authored andcommitted
deprecate PyArray::from_iter
1 parent e4222ba commit 6234a72

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

benches/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn from_iter(bencher: &mut Bencher, size: usize) {
6666
iter_with_gil(bencher, |py| {
6767
let iter = black_box(Iter(0..size));
6868

69-
PyArray1::from_iter(py, iter);
69+
PyArray1::from_iter_bound(py, iter);
7070
});
7171
}
7272

src/array.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,18 @@ impl<T: Element> PyArray<T, Ix1> {
11061106
vec.into_pyarray_bound(py).into_gil_ref()
11071107
}
11081108

1109+
/// Deprecated form of [`PyArray<T, Ix1>::from_iter_bound`]
1110+
#[deprecated(
1111+
since = "0.21.0",
1112+
note = "will be replaced by PyArray::from_iter_bound in the future"
1113+
)]
1114+
pub fn from_iter<'py, I>(py: Python<'py>, iter: I) -> &'py Self
1115+
where
1116+
I: IntoIterator<Item = T>,
1117+
{
1118+
Self::from_iter_bound(py, iter).into_gil_ref()
1119+
}
1120+
11091121
/// Construct a one-dimensional array from an [`Iterator`].
11101122
///
11111123
/// If no reliable [`size_hint`][Iterator::size_hint] is available,
@@ -1114,20 +1126,20 @@ impl<T: Element> PyArray<T, Ix1> {
11141126
/// # Example
11151127
///
11161128
/// ```
1117-
/// use numpy::PyArray;
1129+
/// use numpy::{PyArray, PyArrayMethods};
11181130
/// use pyo3::Python;
11191131
///
11201132
/// Python::with_gil(|py| {
1121-
/// let pyarray = PyArray::from_iter(py, "abcde".chars().map(u32::from));
1133+
/// let pyarray = PyArray::from_iter_bound(py, "abcde".chars().map(u32::from));
11221134
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[97, 98, 99, 100, 101]);
11231135
/// });
11241136
/// ```
1125-
pub fn from_iter<'py, I>(py: Python<'py>, iter: I) -> &'py Self
1137+
pub fn from_iter_bound<I>(py: Python<'_>, iter: I) -> Bound<'_, Self>
11261138
where
11271139
I: IntoIterator<Item = T>,
11281140
{
11291141
let data = iter.into_iter().collect::<Vec<_>>();
1130-
data.into_pyarray_bound(py).into_gil_ref()
1142+
data.into_pyarray_bound(py)
11311143
}
11321144
}
11331145

@@ -1288,13 +1300,14 @@ impl<T: Element, D> PyArray<T, D> {
12881300
/// # Example
12891301
///
12901302
/// ```
1303+
/// use numpy::prelude::*;
12911304
/// use numpy::{npyffi::NPY_ORDER, PyArray};
12921305
/// use pyo3::Python;
12931306
/// use ndarray::array;
12941307
///
12951308
/// Python::with_gil(|py| {
12961309
/// let array =
1297-
/// PyArray::from_iter(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap();
1310+
/// PyArray::from_iter_bound(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap();
12981311
///
12991312
/// assert_eq!(array.readonly().as_array(), array![[0, 3, 6], [1, 4, 7], [2, 5, 8]]);
13001313
/// assert!(array.is_fortran_contiguous());
@@ -1830,13 +1843,14 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
18301843
/// # Example
18311844
///
18321845
/// ```
1846+
/// use numpy::prelude::*;
18331847
/// use numpy::{npyffi::NPY_ORDER, PyArray};
18341848
/// use pyo3::Python;
18351849
/// use ndarray::array;
18361850
///
18371851
/// Python::with_gil(|py| {
18381852
/// let array =
1839-
/// PyArray::from_iter(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap();
1853+
/// PyArray::from_iter_bound(py, 0..9).reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER).unwrap();
18401854
///
18411855
/// assert_eq!(array.readonly().as_array(), array![[0, 3, 6], [1, 4, 7], [2, 5, 8]]);
18421856
/// assert!(array.is_fortran_contiguous());

tests/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ fn get_works() {
523523
#[test]
524524
fn reshape() {
525525
Python::with_gil(|py| {
526-
let array = PyArray::from_iter(py, 0..9)
526+
let array = PyArray::from_iter_bound(py, 0..9)
527527
.reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER)
528528
.unwrap();
529529

tests/to_py.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn to_pyarray_array() {
5252
#[test]
5353
fn iter_to_pyarray() {
5454
Python::with_gil(|py| {
55-
let arr = PyArray::from_iter(py, (0..10).map(|x| x * x));
55+
let arr = PyArray::from_iter_bound(py, (0..10).map(|x| x * x));
5656

5757
assert_eq!(
5858
arr.readonly().as_slice().unwrap(),
@@ -64,7 +64,7 @@ fn iter_to_pyarray() {
6464
#[test]
6565
fn long_iter_to_pyarray() {
6666
Python::with_gil(|py| {
67-
let arr = PyArray::from_iter(py, 0_u32..512);
67+
let arr = PyArray::from_iter_bound(py, 0_u32..512);
6868

6969
assert_eq!(
7070
arr.readonly().as_slice().unwrap(),

0 commit comments

Comments
 (0)