Skip to content

Commit

Permalink
deprecate PyArray::from_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 25, 2024
1 parent 345c613 commit 968ba66
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn from_slice(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let slice = black_box(&vec);

PyArray1::from_slice(py, slice);
PyArray1::from_slice_bound(py, slice);
});
}

Expand All @@ -116,7 +116,7 @@ fn from_object_slice(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let slice = black_box(&vec);

PyArray1::from_slice(py, slice);
PyArray1::from_slice_bound(py, slice);
});
}

Expand Down
21 changes: 15 additions & 6 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,26 +1055,35 @@ impl<T: Copy + Element> PyArray<T, Ix0> {
}

impl<T: Element> PyArray<T, Ix1> {
/// Deprecated form of [`PyArray<T, Ix1>::from_slice_bound`]
#[deprecated(
since = "0.21.0",
note = "will be replaced by `PyArray::from_slice_bound` in the future"
)]
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
Self::from_slice_bound(py, slice).into_gil_ref()
}

/// Construct a one-dimensional array from a [mod@slice].
///
/// # Example
///
/// ```
/// use numpy::PyArray;
/// use numpy::{PyArray, PyArrayMethods};
/// use pyo3::Python;
///
/// Python::with_gil(|py| {
/// let slice = &[1, 2, 3, 4, 5];
/// let pyarray = PyArray::from_slice(py, slice);
/// let pyarray = PyArray::from_slice_bound(py, slice);
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
/// });
/// ```
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
pub fn from_slice_bound<'py>(py: Python<'py>, slice: &[T]) -> Bound<'py, Self> {
unsafe {
let array = PyArray::new_bound(py, [slice.len()], false);
let mut data_ptr = array.data();
clone_elements(slice, &mut data_ptr);
array.into_gil_ref()
array
}
}

Expand Down Expand Up @@ -2287,8 +2296,8 @@ mod tests {
#[test]
fn test_hasobject_flag() {
Python::with_gil(|py| {
let array: &PyArray<PyObject, _> =
PyArray1::from_slice(py, &[PyList::empty(py).into()]);
let array: Bound<'_, PyArray<PyObject, _>> =
PyArray1::from_slice_bound(py, &[PyList::empty_bound(py).into()]);

py_run!(py, array, "assert array.dtype.hasobject");
});
Expand Down
2 changes: 1 addition & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<T: Element> ToPyArray for [T] {
type Dim = Ix1;

fn to_pyarray<'py>(&self, py: Python<'py>) -> &'py PyArray<Self::Item, Self::Dim> {
PyArray::from_slice(py, self)
PyArray::from_slice_bound(py, self).into_gil_ref()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ mod tests {
fn unit_conversion() {
#[track_caller]
fn convert<'py, S: Unit, D: Unit>(py: Python<'py>, expected_value: i64) {
let array = PyArray1::<Timedelta<S>>::from_slice(py, &[Timedelta::<S>::from(1)]);
let array = PyArray1::<Timedelta<S>>::from_slice_bound(py, &[Timedelta::<S>::from(1)]);
let array = array.cast::<Timedelta<D>>(false).unwrap();

let value: i64 = array.get_owned(0).unwrap().into();
Expand Down
3 changes: 2 additions & 1 deletion src/npyffi/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ const CAPSULE_NAME: &str = "_ARRAY_API";
///
/// # Example
/// ```
/// use numpy::prelude::*;
/// use numpy::{PyArray, npyffi::types::NPY_SORTKIND, PY_ARRAY_API};
/// pyo3::Python::with_gil(|py| {
/// let array = PyArray::from_slice(py, &[3, 2, 4]);
/// let array = PyArray::from_slice_bound(py, &[3, 2, 4]);
/// unsafe {
/// PY_ARRAY_API.PyArray_Sort(py, array.as_array_ptr(), 0, NPY_SORTKIND::NPY_QUICKSORT);
/// }
Expand Down
18 changes: 9 additions & 9 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use numpy::{
use pyo3::{
py_run, pyclass, pymethods,
types::{IntoPyDict, PyAnyMethods, PyDict, PyList},
Bound, IntoPy, Py, PyAny, PyResult, Python,
Bound, Py, PyResult, Python,
};

fn get_np_locals<'py>(py: Python<'py>) -> &'py PyDict {
Expand Down Expand Up @@ -430,7 +430,7 @@ fn borrow_from_array_works() {
#[test]
fn downcasting_works() {
Python::with_gil(|py| {
let ob: &PyAny = PyArray::from_slice(py, &[1_i32, 2, 3]);
let ob = PyArray::from_slice_bound(py, &[1_i32, 2, 3]).into_any();

assert!(ob.downcast::<PyArray1<i32>>().is_ok());
});
Expand All @@ -439,7 +439,7 @@ fn downcasting_works() {
#[test]
fn downcasting_respects_element_type() {
Python::with_gil(|py| {
let ob: &PyAny = PyArray::from_slice(py, &[1_i32, 2, 3]);
let ob = PyArray::from_slice_bound(py, &[1_i32, 2, 3]).into_any();

assert!(ob.downcast::<PyArray1<f64>>().is_err());
});
Expand All @@ -448,18 +448,18 @@ fn downcasting_respects_element_type() {
#[test]
fn downcasting_respects_dimensionality() {
Python::with_gil(|py| {
let ob: &PyAny = PyArray::from_slice(py, &[1_i32, 2, 3]);
let ob = PyArray::from_slice_bound(py, &[1_i32, 2, 3]).into_any();

assert!(ob.downcast::<PyArray2<i32>>().is_err());
});
}

#[test]
fn into_py_works() {
fn unbind_works() {
let arr: Py<PyArray1<_>> = Python::with_gil(|py| {
let arr = PyArray::from_slice(py, &[1_i32, 2, 3]);
let arr = PyArray::from_slice_bound(py, &[1_i32, 2, 3]);

arr.into_py(py)
arr.unbind()
});

Python::with_gil(|py| {
Expand All @@ -472,10 +472,10 @@ fn into_py_works() {
#[test]
fn to_owned_works() {
let arr: Py<PyArray1<_>> = Python::with_gil(|py| {
let arr = PyArray::from_slice(py, &[1_i32, 2, 3]);
let arr = PyArray::from_slice_bound(py, &[1_i32, 2, 3]);

#[allow(deprecated)]
arr.to_owned()
arr.as_gil_ref().to_owned()
});

Python::with_gil(|py| {
Expand Down

0 comments on commit 968ba66

Please sign in to comment.