Skip to content

Commit f979966

Browse files
Icxoluadamreichold
authored andcommitted
deprecate PyArray::from_owned_array
1 parent 6244177 commit f979966

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/array.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use crate::untyped_array::{PyUntypedArray, PyUntypedArrayMethods};
8282
///
8383
/// ```
8484
/// use numpy::PyArray;
85-
/// use ndarray::{array, Array};
85+
/// use ndarray::array;
8686
/// use pyo3::Python;
8787
///
8888
/// Python::with_gil(|py| {
@@ -575,24 +575,33 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
575575
}
576576
}
577577

578+
/// Deprecated form of [`PyArray<T, D>::from_owned_array_bound`]
579+
#[deprecated(
580+
since = "0.21.0",
581+
note = "will be replaced by PyArray::from_owned_array_bound in the future"
582+
)]
583+
pub fn from_owned_array<'py>(py: Python<'py>, arr: Array<T, D>) -> &'py Self {
584+
Self::from_owned_array_bound(py, arr).into_gil_ref()
585+
}
586+
578587
/// Constructs a NumPy from an [`ndarray::Array`]
579588
///
580589
/// This method uses the internal [`Vec`] of the [`ndarray::Array`] as the base object of the NumPy array.
581590
///
582591
/// # Example
583592
///
584593
/// ```
585-
/// use numpy::PyArray;
594+
/// use numpy::{PyArray, PyArrayMethods};
586595
/// use ndarray::array;
587596
/// use pyo3::Python;
588597
///
589598
/// Python::with_gil(|py| {
590-
/// let pyarray = PyArray::from_owned_array(py, array![[1, 2], [3, 4]]);
599+
/// let pyarray = PyArray::from_owned_array_bound(py, array![[1, 2], [3, 4]]);
591600
///
592601
/// assert_eq!(pyarray.readonly().as_array(), array![[1, 2], [3, 4]]);
593602
/// });
594603
/// ```
595-
pub fn from_owned_array<'py>(py: Python<'py>, mut arr: Array<T, D>) -> &'py Self {
604+
pub fn from_owned_array_bound(py: Python<'_>, mut arr: Array<T, D>) -> Bound<'_, Self> {
596605
let (strides, dims) = (arr.npy_strides(), arr.raw_dim());
597606
let data_ptr = arr.as_mut_ptr();
598607
unsafe {
@@ -603,7 +612,6 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
603612
data_ptr,
604613
PySliceContainer::from(arr),
605614
)
606-
.into_gil_ref()
607615
}
608616
}
609617

@@ -963,6 +971,15 @@ where
963971
}
964972

965973
impl<D: Dimension> PyArray<PyObject, D> {
974+
/// Deprecated form of [`PyArray<T, D>::from_owned_object_array_bound`]
975+
#[deprecated(
976+
since = "0.21.0",
977+
note = "will be replaced by PyArray::from_owned_object_array_bound in the future"
978+
)]
979+
pub fn from_owned_object_array<'py, T>(py: Python<'py>, arr: Array<Py<T>, D>) -> &'py Self {
980+
Self::from_owned_object_array_bound(py, arr).into_gil_ref()
981+
}
982+
966983
/// Construct a NumPy array containing objects stored in a [`ndarray::Array`]
967984
///
968985
/// This method uses the internal [`Vec`] of the [`ndarray::Array`] as the base object of the NumPy array.
@@ -972,7 +989,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
972989
/// ```
973990
/// use ndarray::array;
974991
/// use pyo3::{pyclass, Py, Python};
975-
/// use numpy::PyArray;
992+
/// use numpy::{PyArray, PyArrayMethods};
976993
///
977994
/// #[pyclass]
978995
/// struct CustomElement {
@@ -992,12 +1009,15 @@ impl<D: Dimension> PyArray<PyObject, D> {
9921009
/// }).unwrap(),
9931010
/// ];
9941011
///
995-
/// let pyarray = PyArray::from_owned_object_array(py, array);
1012+
/// let pyarray = PyArray::from_owned_object_array_bound(py, array);
9961013
///
9971014
/// assert!(pyarray.readonly().as_array().get(0).unwrap().as_ref(py).is_instance_of::<CustomElement>());
9981015
/// });
9991016
/// ```
1000-
pub fn from_owned_object_array<'py, T>(py: Python<'py>, mut arr: Array<Py<T>, D>) -> &'py Self {
1017+
pub fn from_owned_object_array_bound<T>(
1018+
py: Python<'_>,
1019+
mut arr: Array<Py<T>, D>,
1020+
) -> Bound<'_, Self> {
10011021
let (strides, dims) = (arr.npy_strides(), arr.raw_dim());
10021022
let data_ptr = arr.as_mut_ptr() as *const PyObject;
10031023
unsafe {
@@ -1008,7 +1028,6 @@ impl<D: Dimension> PyArray<PyObject, D> {
10081028
data_ptr,
10091029
PySliceContainer::from(arr),
10101030
)
1011-
.into_gil_ref()
10121031
}
10131032
}
10141033
}

src/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
type Dim = D;
9494

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

0 commit comments

Comments
 (0)