Skip to content

Commit 31f1eef

Browse files
Icxoluadamreichold
authored andcommitted
deprecate PyArray::from_slice
1 parent 345c613 commit 31f1eef

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

benches/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn from_slice(bencher: &mut Bencher, size: usize) {
9191
iter_with_gil(bencher, |py| {
9292
let slice = black_box(&vec);
9393

94-
PyArray1::from_slice(py, slice);
94+
PyArray1::from_slice_bound(py, slice);
9595
});
9696
}
9797

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

119-
PyArray1::from_slice(py, slice);
119+
PyArray1::from_slice_bound(py, slice);
120120
});
121121
}
122122

src/array.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,26 +1055,35 @@ impl<T: Copy + Element> PyArray<T, Ix0> {
10551055
}
10561056

10571057
impl<T: Element> PyArray<T, Ix1> {
1058+
/// Deprecated form of [`PyArray<T, Ix1>::from_slice_bound`]
1059+
#[deprecated(
1060+
since = "0.21.0",
1061+
note = "will be replaced by `PyArray::from_slice_bound` in the future"
1062+
)]
1063+
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
1064+
Self::from_slice_bound(py, slice).into_gil_ref()
1065+
}
1066+
10581067
/// Construct a one-dimensional array from a [mod@slice].
10591068
///
10601069
/// # Example
10611070
///
10621071
/// ```
1063-
/// use numpy::PyArray;
1072+
/// use numpy::{PyArray, PyArrayMethods};
10641073
/// use pyo3::Python;
10651074
///
10661075
/// Python::with_gil(|py| {
10671076
/// let slice = &[1, 2, 3, 4, 5];
1068-
/// let pyarray = PyArray::from_slice(py, slice);
1077+
/// let pyarray = PyArray::from_slice_bound(py, slice);
10691078
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
10701079
/// });
10711080
/// ```
1072-
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
1081+
pub fn from_slice_bound<'py>(py: Python<'py>, slice: &[T]) -> Bound<'py, Self> {
10731082
unsafe {
10741083
let array = PyArray::new_bound(py, [slice.len()], false);
10751084
let mut data_ptr = array.data();
10761085
clone_elements(slice, &mut data_ptr);
1077-
array.into_gil_ref()
1086+
array
10781087
}
10791088
}
10801089

@@ -2287,8 +2296,8 @@ mod tests {
22872296
#[test]
22882297
fn test_hasobject_flag() {
22892298
Python::with_gil(|py| {
2290-
let array: &PyArray<PyObject, _> =
2291-
PyArray1::from_slice(py, &[PyList::empty(py).into()]);
2299+
let array: Bound<'_, PyArray<PyObject, _>> =
2300+
PyArray1::from_slice_bound(py, &[PyList::empty_bound(py).into()]);
22922301

22932302
py_run!(py, array, "assert array.dtype.hasobject");
22942303
});

src/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<T: Element> ToPyArray for [T] {
143143
type Dim = Ix1;
144144

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

src/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ mod tests {
315315
fn unit_conversion() {
316316
#[track_caller]
317317
fn convert<'py, S: Unit, D: Unit>(py: Python<'py>, expected_value: i64) {
318-
let array = PyArray1::<Timedelta<S>>::from_slice(py, &[Timedelta::<S>::from(1)]);
318+
let array = PyArray1::<Timedelta<S>>::from_slice_bound(py, &[Timedelta::<S>::from(1)]);
319319
let array = array.cast::<Timedelta<D>>(false).unwrap();
320320

321321
let value: i64 = array.get_owned(0).unwrap().into();

src/npyffi/array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ const CAPSULE_NAME: &str = "_ARRAY_API";
2626
///
2727
/// # Example
2828
/// ```
29+
/// use numpy::prelude::*;
2930
/// use numpy::{PyArray, npyffi::types::NPY_SORTKIND, PY_ARRAY_API};
3031
/// pyo3::Python::with_gil(|py| {
31-
/// let array = PyArray::from_slice(py, &[3, 2, 4]);
32+
/// let array = PyArray::from_slice_bound(py, &[3, 2, 4]);
3233
/// unsafe {
3334
/// PY_ARRAY_API.PyArray_Sort(py, array.as_array_ptr(), 0, NPY_SORTKIND::NPY_QUICKSORT);
3435
/// }

tests/array.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use numpy::{
1313
use pyo3::{
1414
py_run, pyclass, pymethods,
1515
types::{IntoPyDict, PyAnyMethods, PyDict, PyList},
16-
Bound, IntoPy, Py, PyAny, PyResult, Python,
16+
Bound, Py, PyResult, Python,
1717
};
1818

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

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

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

453453
assert!(ob.downcast::<PyArray2<i32>>().is_err());
454454
});
455455
}
456456

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

462-
arr.into_py(py)
462+
arr.unbind()
463463
});
464464

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

477477
#[allow(deprecated)]
478-
arr.to_owned()
478+
arr.as_gil_ref().to_owned()
479479
});
480480

481481
Python::with_gil(|py| {

0 commit comments

Comments
 (0)