Skip to content

Commit

Permalink
deprecate PyArray::from_vec
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 25, 2024
1 parent c721be1 commit 7dd1d4d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
4 changes: 2 additions & 2 deletions benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn from_vec2(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let vec2 = black_box(&vec2);

PyArray2::from_vec2(py, vec2).unwrap();
PyArray2::from_vec2_bound(py, vec2).unwrap();
});
}

Expand All @@ -166,7 +166,7 @@ fn from_vec3(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let vec3 = black_box(&vec3);

PyArray3::from_vec3(py, vec3).unwrap();
PyArray3::from_vec3_bound(py, vec3).unwrap();
});
}

Expand Down
64 changes: 49 additions & 15 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,23 +1099,33 @@ impl<T: Element> PyArray<T, Ix1> {
}
}

/// Deprecated form of [`PyArray<T, Ix1>::from_vec_bound`]
#[deprecated(
since = "0.21.0",
note = "will be replaced by `PyArray::from_vec_bound` in the future"
)]
#[inline(always)]
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
Self::from_vec_bound(py, vec).into_gil_ref()
}

/// Construct a one-dimensional array from a [`Vec<T>`][Vec].
///
/// # Example
///
/// ```
/// use numpy::PyArray;
/// use numpy::{PyArray, PyArrayMethods};
/// use pyo3::Python;
///
/// Python::with_gil(|py| {
/// let vec = vec![1, 2, 3, 4, 5];
/// let pyarray = PyArray::from_vec(py, vec);
/// let pyarray = PyArray::from_vec_bound(py, vec);
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
/// });
/// ```
#[inline(always)]
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
vec.into_pyarray_bound(py).into_gil_ref()
pub fn from_vec_bound(py: Python<'_>, vec: Vec<T>) -> Bound<'_, Self> {
vec.into_pyarray_bound(py)
}

/// Deprecated form of [`PyArray<T, Ix1>::from_iter_bound`]
Expand Down Expand Up @@ -1156,6 +1166,15 @@ impl<T: Element> PyArray<T, Ix1> {
}

impl<T: Element> PyArray<T, Ix2> {
/// Deprecated form of [`PyArray<T, Ix2>::from_vec2_bound`]
#[deprecated(
since = "0.21.0",
note = "will be replaced by `PyArray::from_vec2_bound` in the future"
)]
pub fn from_vec2<'py>(py: Python<'py>, v: &[Vec<T>]) -> Result<&'py Self, FromVecError> {
Self::from_vec2_bound(py, v).map(Bound::into_gil_ref)
}

/// Construct a two-dimension array from a [`Vec<Vec<T>>`][Vec].
///
/// This function checks all dimensions of the inner vectors and returns
Expand All @@ -1164,20 +1183,23 @@ impl<T: Element> PyArray<T, Ix2> {
/// # Example
///
/// ```
/// use numpy::PyArray;
/// use numpy::{PyArray, PyArrayMethods};
/// use pyo3::Python;
/// use ndarray::array;
///
/// Python::with_gil(|py| {
/// let vec2 = vec![vec![11, 12], vec![21, 22]];
/// let pyarray = PyArray::from_vec2(py, &vec2).unwrap();
/// let pyarray = PyArray::from_vec2_bound(py, &vec2).unwrap();
/// assert_eq!(pyarray.readonly().as_array(), array![[11, 12], [21, 22]]);
///
/// let ragged_vec2 = vec![vec![11, 12], vec![21]];
/// assert!(PyArray::from_vec2(py, &ragged_vec2).is_err());
/// assert!(PyArray::from_vec2_bound(py, &ragged_vec2).is_err());
/// });
/// ```
pub fn from_vec2<'py>(py: Python<'py>, v: &[Vec<T>]) -> Result<&'py Self, FromVecError> {
pub fn from_vec2_bound<'py>(
py: Python<'py>,
v: &[Vec<T>],
) -> Result<Bound<'py, Self>, FromVecError> {
let len2 = v.first().map_or(0, |v| v.len());
let dims = [v.len(), len2];
// SAFETY: The result of `Self::new` is always safe to drop.
Expand All @@ -1191,12 +1213,21 @@ impl<T: Element> PyArray<T, Ix2> {
}
clone_elements(v, &mut data_ptr);
}
Ok(array.into_gil_ref())
Ok(array)
}
}
}

impl<T: Element> PyArray<T, Ix3> {
/// Deprecated form of [`PyArray<T, Ix3>::from_vec3_bound`]
#[deprecated(
since = "0.21.0",
note = "will be replaced by `PyArray::from_vec3_bound` in the future"
)]
pub fn from_vec3<'py>(py: Python<'py>, v: &[Vec<Vec<T>>]) -> Result<&'py Self, FromVecError> {
Self::from_vec3_bound(py, v).map(Bound::into_gil_ref)
}

/// Construct a three-dimensional array from a [`Vec<Vec<Vec<T>>>`][Vec].
///
/// This function checks all dimensions of the inner vectors and returns
Expand All @@ -1205,7 +1236,7 @@ impl<T: Element> PyArray<T, Ix3> {
/// # Example
///
/// ```
/// use numpy::PyArray;
/// use numpy::{PyArray, PyArrayMethods};
/// use pyo3::Python;
/// use ndarray::array;
///
Expand All @@ -1214,7 +1245,7 @@ impl<T: Element> PyArray<T, Ix3> {
/// vec![vec![111, 112], vec![121, 122]],
/// vec![vec![211, 212], vec![221, 222]],
/// ];
/// let pyarray = PyArray::from_vec3(py, &vec3).unwrap();
/// let pyarray = PyArray::from_vec3_bound(py, &vec3).unwrap();
/// assert_eq!(
/// pyarray.readonly().as_array(),
/// array![[[111, 112], [121, 122]], [[211, 212], [221, 222]]]
Expand All @@ -1224,10 +1255,13 @@ impl<T: Element> PyArray<T, Ix3> {
/// vec![vec![111, 112], vec![121, 122]],
/// vec![vec![211], vec![221, 222]],
/// ];
/// assert!(PyArray::from_vec3(py, &ragged_vec3).is_err());
/// assert!(PyArray::from_vec3_bound(py, &ragged_vec3).is_err());
/// });
/// ```
pub fn from_vec3<'py>(py: Python<'py>, v: &[Vec<Vec<T>>]) -> Result<&'py Self, FromVecError> {
pub fn from_vec3_bound<'py>(
py: Python<'py>,
v: &[Vec<Vec<T>>],
) -> Result<Bound<'py, Self>, FromVecError> {
let len2 = v.first().map_or(0, |v| v.len());
let len3 = v.first().map_or(0, |v| v.first().map_or(0, |v| v.len()));
let dims = [v.len(), len2, len3];
Expand All @@ -1248,7 +1282,7 @@ impl<T: Element> PyArray<T, Ix3> {
clone_elements(v, &mut data_ptr);
}
}
Ok(array.into_gil_ref())
Ok(array)
}
}
}
Expand Down Expand Up @@ -2319,7 +2353,7 @@ mod tests {
#[test]
fn test_dyn_to_owned_array() {
Python::with_gil(|py| {
let array = PyArray::from_vec2(py, &[vec![1, 2], vec![3, 4]])
let array = PyArray::from_vec2_bound(py, &[vec![1, 2], vec![3, 4]])
.unwrap()
.to_dyn()
.to_owned_array();
Expand Down
8 changes: 4 additions & 4 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ use crate::npyffi::NPY_TYPES;
///
/// ```rust
/// # use pyo3::Python;
/// use numpy::{PyArray1, PyFixedString};
/// use numpy::{PyArray1, PyUntypedArrayMethods, PyFixedString};
///
/// # Python::with_gil(|py| {
/// let array = PyArray1::<PyFixedString<3>>::from_vec(py, vec![[b'f', b'o', b'o'].into()]);
/// let array = PyArray1::<PyFixedString<3>>::from_vec_bound(py, vec![[b'f', b'o', b'o'].into()]);
///
/// assert!(array.dtype().to_string().contains("S3"));
/// # });
Expand Down Expand Up @@ -110,10 +110,10 @@ unsafe impl<const N: usize> Element for PyFixedString<N> {
///
/// ```rust
/// # use pyo3::Python;
/// use numpy::{PyArray1, PyFixedUnicode};
/// use numpy::{PyArray1, PyUntypedArrayMethods, PyFixedUnicode};
///
/// # Python::with_gil(|py| {
/// let array = PyArray1::<PyFixedUnicode<3>>::from_vec(py, vec![[b'b' as _, b'a' as _, b'r' as _].into()]);
/// let array = PyArray1::<PyFixedUnicode<3>>::from_vec_bound(py, vec![[b'b' as _, b'a' as _, b'r' as _].into()]);
///
/// assert!(array.dtype().to_string().contains("U3"));
/// # });
Expand Down
10 changes: 6 additions & 4 deletions src/untyped_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ impl PyUntypedArray {
/// # Example
///
/// ```
/// use numpy::prelude::*;
/// use numpy::{dtype_bound, PyArray};
/// use pyo3::Python;
///
/// Python::with_gil(|py| {
/// let array = PyArray::from_vec(py, vec![1_i32, 2, 3]);
/// let array = PyArray::from_vec_bound(py, vec![1_i32, 2, 3]);
///
/// assert!(array.dtype().is_equiv_to(dtype_bound::<i32>(py).as_gil_ref()));
/// assert!(array.dtype().is_equiv_to(&dtype_bound::<i32>(py)));
/// });
/// ```
///
Expand Down Expand Up @@ -269,13 +270,14 @@ pub trait PyUntypedArrayMethods<'py>: Sealed {
/// # Example
///
/// ```
/// use numpy::prelude::*;
/// use numpy::{dtype_bound, PyArray};
/// use pyo3::Python;
///
/// Python::with_gil(|py| {
/// let array = PyArray::from_vec(py, vec![1_i32, 2, 3]);
/// let array = PyArray::from_vec_bound(py, vec![1_i32, 2, 3]);
///
/// assert!(array.dtype().is_equiv_to(dtype_bound::<i32>(py).as_gil_ref()));
/// assert!(array.dtype().is_equiv_to(&dtype_bound::<i32>(py)));
/// });
/// ```
///
Expand Down
10 changes: 5 additions & 5 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn is_instance() {
#[test]
fn from_vec2() {
Python::with_gil(|py| {
let pyarray = PyArray::from_vec2(py, &[vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
let pyarray = PyArray::from_vec2_bound(py, &[vec![1, 2, 3], vec![4, 5, 6]]).unwrap();

assert_eq!(pyarray.readonly().as_array(), array![[1, 2, 3], [4, 5, 6]]);
});
Expand All @@ -204,7 +204,7 @@ fn from_vec2() {
#[test]
fn from_vec2_ragged() {
Python::with_gil(|py| {
let pyarray = PyArray::from_vec2(py, &[vec![1, 2, 3], vec![4, 5]]);
let pyarray = PyArray::from_vec2_bound(py, &[vec![1, 2, 3], vec![4, 5]]);

let err = pyarray.unwrap_err();
assert_eq!(err.to_string(), "invalid length: 2, but expected 3");
Expand All @@ -214,7 +214,7 @@ fn from_vec2_ragged() {
#[test]
fn from_vec3() {
Python::with_gil(|py| {
let pyarray = PyArray::from_vec3(
let pyarray = PyArray::from_vec3_bound(
py,
&[
vec![vec![1, 2], vec![3, 4]],
Expand All @@ -234,7 +234,7 @@ fn from_vec3() {
#[test]
fn from_vec3_ragged() {
Python::with_gil(|py| {
let pyarray = PyArray::from_vec3(
let pyarray = PyArray::from_vec3_bound(
py,
&[
vec![vec![1, 2], vec![3, 4]],
Expand All @@ -246,7 +246,7 @@ fn from_vec3_ragged() {
let err = pyarray.unwrap_err();
assert_eq!(err.to_string(), "invalid length: 1, but expected 2");

let pyarray = PyArray::from_vec3(
let pyarray = PyArray::from_vec3_bound(
py,
&[
vec![vec![1, 2], vec![3, 4]],
Expand Down

0 comments on commit 7dd1d4d

Please sign in to comment.