From b648b9b2edbbf6a29837142cf8c12635d31117e7 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:32:12 +0100 Subject: [PATCH] deprecate `PyArray::from_vec` --- benches/array.rs | 4 +-- src/array.rs | 64 +++++++++++++++++++++++++++++++++----------- src/strings.rs | 8 +++--- src/untyped_array.rs | 10 ++++--- tests/array.rs | 10 +++---- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/benches/array.rs b/benches/array.rs index 6619c4e27..7b7ed21d1 100644 --- a/benches/array.rs +++ b/benches/array.rs @@ -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(); }); } @@ -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(); }); } diff --git a/src/array.rs b/src/array.rs index 34f8aaaee..e1c60f021 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1099,23 +1099,33 @@ impl PyArray { } } + /// Deprecated form of [`PyArray::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) -> &'py Self { + Self::from_vec_bound(py, vec).into_gil_ref() + } + /// Construct a one-dimensional array from a [`Vec`][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) -> &'py Self { - vec.into_pyarray_bound(py).into_gil_ref() + pub fn from_vec_bound(py: Python<'_>, vec: Vec) -> Bound<'_, Self> { + vec.into_pyarray_bound(py) } /// Deprecated form of [`PyArray::from_iter_bound`] @@ -1156,6 +1166,15 @@ impl PyArray { } impl PyArray { + /// Deprecated form of [`PyArray::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]) -> Result<&'py Self, FromVecError> { + Self::from_vec2_bound(py, v).map(Bound::into_gil_ref) + } + /// Construct a two-dimension array from a [`Vec>`][Vec]. /// /// This function checks all dimensions of the inner vectors and returns @@ -1164,20 +1183,23 @@ impl PyArray { /// # 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]) -> Result<&'py Self, FromVecError> { + pub fn from_vec2_bound<'py>( + py: Python<'py>, + v: &[Vec], + ) -> Result, 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. @@ -1191,12 +1213,21 @@ impl PyArray { } clone_elements(v, &mut data_ptr); } - Ok(array.into_gil_ref()) + Ok(array) } } } impl PyArray { + /// Deprecated form of [`PyArray::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>]) -> Result<&'py Self, FromVecError> { + Self::from_vec3_bound(py, v).map(Bound::into_gil_ref) + } + /// Construct a three-dimensional array from a [`Vec>>`][Vec]. /// /// This function checks all dimensions of the inner vectors and returns @@ -1205,7 +1236,7 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods}; /// use pyo3::Python; /// use ndarray::array; /// @@ -1214,7 +1245,7 @@ impl PyArray { /// 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]]] @@ -1224,10 +1255,13 @@ impl PyArray { /// 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>]) -> Result<&'py Self, FromVecError> { + pub fn from_vec3_bound<'py>( + py: Python<'py>, + v: &[Vec>], + ) -> Result, 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]; @@ -1248,7 +1282,7 @@ impl PyArray { clone_elements(v, &mut data_ptr); } } - Ok(array.into_gil_ref()) + Ok(array) } } } @@ -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(); diff --git a/src/strings.rs b/src/strings.rs index ba9f14236..74606289e 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -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::>::from_vec(py, vec![[b'f', b'o', b'o'].into()]); +/// let array = PyArray1::>::from_vec_bound(py, vec![[b'f', b'o', b'o'].into()]); /// /// assert!(array.dtype().to_string().contains("S3")); /// # }); @@ -110,10 +110,10 @@ unsafe impl Element for PyFixedString { /// /// ```rust /// # use pyo3::Python; -/// use numpy::{PyArray1, PyFixedUnicode}; +/// use numpy::{PyArray1, PyUntypedArrayMethods, PyFixedUnicode}; /// /// # Python::with_gil(|py| { -/// let array = PyArray1::>::from_vec(py, vec![[b'b' as _, b'a' as _, b'r' as _].into()]); +/// let array = PyArray1::>::from_vec_bound(py, vec![[b'b' as _, b'a' as _, b'r' as _].into()]); /// /// assert!(array.dtype().to_string().contains("U3")); /// # }); diff --git a/src/untyped_array.rs b/src/untyped_array.rs index 46f631542..c51a89458 100644 --- a/src/untyped_array.rs +++ b/src/untyped_array.rs @@ -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::(py).as_gil_ref())); + /// assert!(array.dtype().is_equiv_to(&dtype_bound::(py))); /// }); /// ``` /// @@ -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::(py).as_gil_ref())); + /// assert!(array.dtype().is_equiv_to(&dtype_bound::(py))); /// }); /// ``` /// diff --git a/tests/array.rs b/tests/array.rs index 79b5a3314..610821b1c 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -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]]); }); @@ -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"); @@ -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]], @@ -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]], @@ -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]],