From 072c2fc418b7520ec09bf380bb99368dffbbabd9 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:28:29 +0100 Subject: [PATCH] deprecate `PyArray::from_owned_array` --- src/array.rs | 36 +++++++++++++++++++++++++++--------- src/convert.rs | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/array.rs b/src/array.rs index 7a6c085e8..7c706b848 100644 --- a/src/array.rs +++ b/src/array.rs @@ -82,7 +82,7 @@ use crate::untyped_array::{PyUntypedArray, PyUntypedArrayMethods}; /// /// ``` /// use numpy::PyArray; -/// use ndarray::{array, Array}; +/// use ndarray::array; /// use pyo3::Python; /// /// Python::with_gil(|py| { @@ -571,6 +571,15 @@ impl PyArray { } } + /// Deprecated form of [`PyArray::from_owned_array_bound`] + #[deprecated( + since = "0.21.0", + note = "will be replaced by PyArray::from_owned_array_bound in the future" + )] + pub fn from_owned_array<'py>(py: Python<'py>, arr: Array) -> &'py Self { + Self::from_owned_array_bound(py, arr).into_gil_ref() + } + /// Constructs a NumPy from an [`ndarray::Array`] /// /// This method uses the internal [`Vec`] of the [`ndarray::Array`] as the base object of the NumPy array. @@ -578,17 +587,17 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods}; /// use ndarray::array; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let pyarray = PyArray::from_owned_array(py, array![[1, 2], [3, 4]]); + /// let pyarray = PyArray::from_owned_array_bound(py, array![[1, 2], [3, 4]]); /// /// assert_eq!(pyarray.readonly().as_array(), array![[1, 2], [3, 4]]); /// }); /// ``` - pub fn from_owned_array<'py>(py: Python<'py>, mut arr: Array) -> &'py Self { + pub fn from_owned_array_bound(py: Python<'_>, mut arr: Array) -> Bound<'_, Self> { let (strides, dims) = (arr.npy_strides(), arr.raw_dim()); let data_ptr = arr.as_mut_ptr(); unsafe { @@ -599,7 +608,6 @@ impl PyArray { data_ptr, PySliceContainer::from(arr), ) - .into_gil_ref() } } @@ -955,6 +963,14 @@ where } impl PyArray { + /// Deprecated form of [`PyArray::from_owned_object_array_bound`] + #[deprecated( + since = "0.21.0", + note = "will be replaced by PyArray::from_owned_object_array_bound in the future" + )] + pub fn from_owned_object_array<'py, T>(py: Python<'py>, arr: Array, D>) -> &'py Self { + Self::from_owned_object_array_bound(py, arr).into_gil_ref() + } /// Construct a NumPy array containing objects stored in a [`ndarray::Array`] /// /// This method uses the internal [`Vec`] of the [`ndarray::Array`] as the base object of the NumPy array. @@ -964,7 +980,7 @@ impl PyArray { /// ``` /// use ndarray::array; /// use pyo3::{pyclass, Py, Python}; - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods}; /// /// #[pyclass] /// struct CustomElement { @@ -984,12 +1000,15 @@ impl PyArray { /// }).unwrap(), /// ]; /// - /// let pyarray = PyArray::from_owned_object_array(py, array); + /// let pyarray = PyArray::from_owned_object_array_bound(py, array); /// /// assert!(pyarray.readonly().as_array().get(0).unwrap().as_ref(py).is_instance_of::()); /// }); /// ``` - pub fn from_owned_object_array<'py, T>(py: Python<'py>, mut arr: Array, D>) -> &'py Self { + pub fn from_owned_object_array_bound( + py: Python<'_>, + mut arr: Array, D>, + ) -> Bound<'_, Self> { let (strides, dims) = (arr.npy_strides(), arr.raw_dim()); let data_ptr = arr.as_mut_ptr() as *const PyObject; unsafe { @@ -1000,7 +1019,6 @@ impl PyArray { data_ptr, PySliceContainer::from(arr), ) - .into_gil_ref() } } } diff --git a/src/convert.rs b/src/convert.rs index f5904187a..a0d04018c 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -93,7 +93,7 @@ where type Dim = D; fn into_pyarray<'py>(self, py: Python<'py>) -> &'py PyArray { - PyArray::from_owned_array(py, self) + PyArray::from_owned_array_bound(py, self).into_gil_ref() } }