Skip to content

Commit

Permalink
deprecate PyArray::from_array
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 25, 2024
1 parent d85359d commit 1dfebf3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,18 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
self.as_borrowed().to_vec()
}

/// Deprecated form of [`PyArray<T, D>::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<S, D>) -> &'py Self
where
S: Data<Elem = T>,
{
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,
Expand All @@ -811,21 +823,21 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
/// # 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<S, D>) -> &'py Self
pub fn from_array_bound<'py, S>(py: Python<'py>, arr: &ArrayBase<S, D>) -> Bound<'py, Self>
where
S: Data<Elem = T>,
{
ToPyArray::to_pyarray_bound(arr, py).into_gil_ref()
ToPyArray::to_pyarray_bound(arr, py)
}

/// Get an immutable borrow of the NumPy array
Expand Down
2 changes: 1 addition & 1 deletion tests/to_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn to_pyarray_array() {
.map(|dim| dim * size_of::<f64>() as isize)
.collect::<Vec<_>>();

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());
Expand Down

0 comments on commit 1dfebf3

Please sign in to comment.