From 1dfebf33ad232580cc650671bb589c589e412216 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:24:17 +0100 Subject: [PATCH] deprecate `PyArray::from_array` --- src/array.rs | 20 ++++++++++++++++---- tests/to_py.rs | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/array.rs b/src/array.rs index 7a837c973..34f8aaaee 100644 --- a/src/array.rs +++ b/src/array.rs @@ -803,6 +803,18 @@ impl PyArray { self.as_borrowed().to_vec() } + /// Deprecated form of [`PyArray::from_array_bound`] + #[deprecated( + since = "0.21.0", + note = "will be replaced by PyArray::from_array_bound in the future" + )] + pub fn from_array<'py, S>(py: Python<'py>, arr: &ArrayBase) -> &'py Self + where + S: Data, + { + Self::from_array_bound(py, arr).into_gil_ref() + } + /// Construct a NumPy array from a [`ndarray::ArrayBase`]. /// /// This method allocates memory in Python's heap via the NumPy API, @@ -811,21 +823,21 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods}; /// use ndarray::array; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let pyarray = PyArray::from_array(py, &array![[1, 2], [3, 4]]); + /// let pyarray = PyArray::from_array_bound(py, &array![[1, 2], [3, 4]]); /// /// assert_eq!(pyarray.readonly().as_array(), array![[1, 2], [3, 4]]); /// }); /// ``` - pub fn from_array<'py, S>(py: Python<'py>, arr: &ArrayBase) -> &'py Self + pub fn from_array_bound<'py, S>(py: Python<'py>, arr: &ArrayBase) -> Bound<'py, Self> where S: Data, { - ToPyArray::to_pyarray_bound(arr, py).into_gil_ref() + ToPyArray::to_pyarray_bound(arr, py) } /// Get an immutable borrow of the NumPy array diff --git a/tests/to_py.rs b/tests/to_py.rs index 5181aa63a..9be556bde 100644 --- a/tests/to_py.rs +++ b/tests/to_py.rs @@ -42,7 +42,7 @@ fn to_pyarray_array() { .map(|dim| dim * size_of::() as isize) .collect::>(); - let py_arr = PyArray::from_array(py, &arr); + let py_arr = PyArray::from_array_bound(py, &arr); assert_eq!(py_arr.shape(), shape.as_slice()); assert_eq!(py_arr.strides(), strides.as_slice());