Skip to content

Commit

Permalink
deprecate PyArray::from_owned_array
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 23, 2024
1 parent 31accc2 commit 072c2fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -571,24 +571,33 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
}
}

/// Deprecated form of [`PyArray<T, D>::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<T, D>) -> &'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.
///
/// # 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<T, D>) -> &'py Self {
pub fn from_owned_array_bound(py: Python<'_>, mut arr: Array<T, D>) -> Bound<'_, Self> {
let (strides, dims) = (arr.npy_strides(), arr.raw_dim());
let data_ptr = arr.as_mut_ptr();
unsafe {
Expand All @@ -599,7 +608,6 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
data_ptr,
PySliceContainer::from(arr),
)
.into_gil_ref()
}
}

Expand Down Expand Up @@ -955,6 +963,14 @@ where
}

impl<D: Dimension> PyArray<PyObject, D> {
/// Deprecated form of [`PyArray<T, D>::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<Py<T>, 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.
Expand All @@ -964,7 +980,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
/// ```
/// use ndarray::array;
/// use pyo3::{pyclass, Py, Python};
/// use numpy::PyArray;
/// use numpy::{PyArray, PyArrayMethods};
///
/// #[pyclass]
/// struct CustomElement {
Expand All @@ -984,12 +1000,15 @@ impl<D: Dimension> PyArray<PyObject, D> {
/// }).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::<CustomElement>());
/// });
/// ```
pub fn from_owned_object_array<'py, T>(py: Python<'py>, mut arr: Array<Py<T>, D>) -> &'py Self {
pub fn from_owned_object_array_bound<T>(
py: Python<'_>,
mut arr: Array<Py<T>, D>,
) -> Bound<'_, Self> {
let (strides, dims) = (arr.npy_strides(), arr.raw_dim());
let data_ptr = arr.as_mut_ptr() as *const PyObject;
unsafe {
Expand All @@ -1000,7 +1019,6 @@ impl<D: Dimension> PyArray<PyObject, D> {
data_ptr,
PySliceContainer::from(arr),
)
.into_gil_ref()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
type Dim = D;

fn into_pyarray<'py>(self, py: Python<'py>) -> &'py PyArray<Self::Item, Self::Dim> {
PyArray::from_owned_array(py, self)
PyArray::from_owned_array_bound(py, self).into_gil_ref()
}
}

Expand Down

0 comments on commit 072c2fc

Please sign in to comment.