Skip to content

Commit e4222ba

Browse files
Icxoluadamreichold
authored andcommitted
migrate IntoPyArray and ToPyArray
1 parent c2a3c39 commit e4222ba

File tree

11 files changed

+165
-122
lines changed

11 files changed

+165
-122
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ numpy = "0.20"
4444

4545
```rust
4646
use numpy::ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
47-
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
48-
use pyo3::{pymodule, types::PyModule, PyResult, Python};
47+
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn, PyArrayMethods};
48+
use pyo3::{pymodule, types::PyModule, PyResult, Python, Bound};
4949

5050
#[pymodule]
51-
fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
51+
fn rust_ext<'py>(_py: Python<'py>, m: &Bound<'py, PyModule>) -> PyResult<()> {
5252
// example using immutable borrows producing a new array
5353
fn axpy(a: f64, x: ArrayViewD<'_, f64>, y: ArrayViewD<'_, f64>) -> ArrayD<f64> {
5454
a * &x + &y
@@ -67,17 +67,17 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
6767
a: f64,
6868
x: PyReadonlyArrayDyn<'py, f64>,
6969
y: PyReadonlyArrayDyn<'py, f64>,
70-
) -> &'py PyArrayDyn<f64> {
70+
) -> Bound<'py, PyArrayDyn<f64>> {
7171
let x = x.as_array();
7272
let y = y.as_array();
7373
let z = axpy(a, x, y);
74-
z.into_pyarray(py)
74+
z.into_pyarray_bound(py)
7575
}
7676

7777
// wrapper of `mult`
7878
#[pyfn(m)]
7979
#[pyo3(name = "mult")]
80-
fn mult_py<'py>(a: f64, x: &'py PyArrayDyn<f64>) {
80+
fn mult_py<'py>(a: f64, x: &Bound<'py, PyArrayDyn<f64>>) {
8181
let x = unsafe { x.as_array_mut() };
8282
mult(a, x);
8383
}

examples/linalg/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
use ndarray_linalg::solve::Inverse;
22
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
3-
use pyo3::{exceptions::PyRuntimeError, pymodule, types::PyModule, PyResult, Python};
3+
use pyo3::{exceptions::PyRuntimeError, pymodule, types::PyModule, Bound, PyResult, Python};
44

55
#[pymodule]
6-
fn rust_linalg<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
6+
fn rust_linalg<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
77
#[pyfn(m)]
8-
fn inv<'py>(py: Python<'py>, x: PyReadonlyArray2<'py, f64>) -> PyResult<&'py PyArray2<f64>> {
8+
fn inv<'py>(
9+
py: Python<'py>,
10+
x: PyReadonlyArray2<'py, f64>,
11+
) -> PyResult<Bound<'py, PyArray2<f64>>> {
912
let x = x.as_array();
1013
let y = x
1114
.inv()
1215
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
13-
Ok(y.into_pyarray(py))
16+
Ok(y.into_pyarray_bound(py))
1417
}
1518
Ok(())
1619
}

examples/parallel/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ extern crate blas_src;
33

44
use numpy::ndarray::Zip;
55
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1, PyReadonlyArray2};
6-
use pyo3::{pymodule, types::PyModule, PyResult, Python};
6+
use pyo3::{pymodule, types::PyModule, Bound, PyResult, Python};
77

88
#[pymodule]
9-
fn rust_parallel<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
9+
fn rust_parallel<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
1010
#[pyfn(m)]
1111
fn rows_dot<'py>(
1212
py: Python<'py>,
1313
x: PyReadonlyArray2<'py, f64>,
1414
y: PyReadonlyArray1<'py, f64>,
15-
) -> &'py PyArray1<f64> {
15+
) -> Bound<'py, PyArray1<f64>> {
1616
let x = x.as_array();
1717
let y = y.as_array();
1818
let z = Zip::from(x.rows()).par_map_collect(|row| row.dot(&y));
19-
z.into_pyarray(py)
19+
z.into_pyarray_bound(py)
2020
}
2121
Ok(())
2222
}

examples/simple/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use pyo3::{
1010
exceptions::PyIndexError,
1111
pymodule,
1212
types::{PyDict, PyModule},
13-
FromPyObject, PyAny, PyObject, PyResult, Python,
13+
Bound, FromPyObject, PyAny, PyObject, PyResult, Python,
1414
};
1515

1616
#[pymodule]
17-
fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
17+
fn rust_ext<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
1818
// example using generic PyObject
1919
fn head(x: ArrayViewD<'_, PyObject>) -> PyResult<PyObject> {
2020
x.get(0)
@@ -60,11 +60,11 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
6060
a: f64,
6161
x: PyReadonlyArrayDyn<'py, f64>,
6262
y: PyReadonlyArrayDyn<'py, f64>,
63-
) -> &'py PyArrayDyn<f64> {
63+
) -> Bound<'py, PyArrayDyn<f64>> {
6464
let x = x.as_array();
6565
let y = y.as_array();
6666
let z = axpy(a, x, y);
67-
z.into_pyarray(py)
67+
z.into_pyarray_bound(py)
6868
}
6969

7070
// wrapper of `mult`
@@ -81,8 +81,8 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
8181
fn conj_py<'py>(
8282
py: Python<'py>,
8383
x: PyReadonlyArrayDyn<'py, Complex64>,
84-
) -> &'py PyArrayDyn<Complex64> {
85-
conj(x.as_array()).into_pyarray(py)
84+
) -> Bound<'py, PyArrayDyn<Complex64>> {
85+
conj(x.as_array()).into_pyarray_bound(py)
8686
}
8787

8888
// example of how to extract an array from a dictionary
@@ -125,28 +125,28 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
125125
fn polymorphic_add<'py>(
126126
x: SupportedArray<'py>,
127127
y: SupportedArray<'py>,
128-
) -> PyResult<&'py PyAny> {
128+
) -> PyResult<Bound<'py, PyAny>> {
129129
match (x, y) {
130130
(SupportedArray::F64(x), SupportedArray::F64(y)) => Ok(generic_add(
131131
x.readonly().as_array(),
132132
y.readonly().as_array(),
133133
)
134-
.into_pyarray(x.py())
135-
.into()),
134+
.into_pyarray_bound(x.py())
135+
.into_any()),
136136
(SupportedArray::I64(x), SupportedArray::I64(y)) => Ok(generic_add(
137137
x.readonly().as_array(),
138138
y.readonly().as_array(),
139139
)
140-
.into_pyarray(x.py())
141-
.into()),
140+
.into_pyarray_bound(x.py())
141+
.into_any()),
142142
(SupportedArray::F64(x), SupportedArray::I64(y))
143143
| (SupportedArray::I64(y), SupportedArray::F64(x)) => {
144144
let y = y.cast::<f64>(false)?;
145145

146146
Ok(
147147
generic_add(x.readonly().as_array(), y.readonly().as_array())
148-
.into_pyarray(x.py())
149-
.into(),
148+
.into_pyarray_bound(x.py())
149+
.into_any(),
150150
)
151151
}
152152
}

src/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
825825
where
826826
S: Data<Elem = T>,
827827
{
828-
ToPyArray::to_pyarray(arr, py)
828+
ToPyArray::to_pyarray_bound(arr, py).into_gil_ref()
829829
}
830830

831831
/// Get an immutable borrow of the NumPy array
@@ -1103,7 +1103,7 @@ impl<T: Element> PyArray<T, Ix1> {
11031103
/// ```
11041104
#[inline(always)]
11051105
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
1106-
vec.into_pyarray(py)
1106+
vec.into_pyarray_bound(py).into_gil_ref()
11071107
}
11081108

11091109
/// Construct a one-dimensional array from an [`Iterator`].
@@ -1127,7 +1127,7 @@ impl<T: Element> PyArray<T, Ix1> {
11271127
I: IntoIterator<Item = T>,
11281128
{
11291129
let data = iter.into_iter().collect::<Vec<_>>();
1130-
data.into_pyarray(py)
1130+
data.into_pyarray_bound(py).into_gil_ref()
11311131
}
11321132
}
11331133

src/array_like.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ where
154154
let array = Array1::from(vec)
155155
.into_dimensionality()
156156
.expect("D being compatible to Ix1")
157-
.into_pyarray(py)
157+
.into_pyarray_bound(py)
158158
.readonly();
159159
return Ok(Self(array, PhantomData));
160160
}

src/borrow/shared.rs

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -477,18 +477,20 @@ mod tests {
477477
#[test]
478478
fn with_base_object() {
479479
Python::with_gil(|py| {
480-
let array = Array::<f64, _>::zeros((1, 2, 3)).into_pyarray(py);
480+
let array = Array::<f64, _>::zeros((1, 2, 3)).into_pyarray_bound(py);
481481

482482
let base = unsafe { (*array.as_array_ptr()).base };
483483
assert!(!base.is_null());
484484

485485
let base_address = base_address(py, array.as_array_ptr());
486-
assert_ne!(base_address, array as *const _ as *mut c_void);
487-
assert_eq!(base_address, base as *mut c_void);
486+
assert_ne!(base_address, array.as_ptr().cast());
487+
assert_eq!(base_address, base.cast::<c_void>());
488488

489489
let data_range = data_range(array.as_array_ptr());
490-
assert_eq!(data_range.0, array.data() as *mut c_char);
491-
assert_eq!(data_range.1, unsafe { array.data().add(6) } as *mut c_char);
490+
assert_eq!(data_range.0, array.data().cast::<c_char>());
491+
assert_eq!(data_range.1, unsafe {
492+
array.data().add(6).cast::<c_char>()
493+
});
492494
});
493495
}
494496

@@ -524,33 +526,35 @@ mod tests {
524526
#[test]
525527
fn view_with_base_object() {
526528
Python::with_gil(|py| {
527-
let array = Array::<f64, _>::zeros((1, 2, 3)).into_pyarray(py);
529+
let array = Array::<f64, _>::zeros((1, 2, 3)).into_pyarray_bound(py);
528530

529-
let locals = [("array", array)].into_py_dict(py);
531+
let locals = [("array", &array)].into_py_dict_bound(py);
530532
let view = py
531-
.eval("array[:,:,0]", None, Some(locals))
533+
.eval_bound("array[:,:,0]", None, Some(&locals))
532534
.unwrap()
533-
.downcast::<PyArray2<f64>>()
535+
.downcast_into::<PyArray2<f64>>()
534536
.unwrap();
535537
assert_ne!(
536-
view as *const _ as *mut c_void,
537-
array as *const _ as *mut c_void
538+
view.as_ptr().cast::<c_void>(),
539+
array.as_ptr().cast::<c_void>(),
538540
);
539541

540542
let base = unsafe { (*view.as_array_ptr()).base };
541-
assert_eq!(base as *mut c_void, array as *const _ as *mut c_void);
543+
assert_eq!(base.cast::<c_void>(), array.as_ptr().cast::<c_void>());
542544

543545
let base = unsafe { (*array.as_array_ptr()).base };
544546
assert!(!base.is_null());
545547

546548
let base_address = base_address(py, view.as_array_ptr());
547-
assert_ne!(base_address, view as *const _ as *mut c_void);
548-
assert_ne!(base_address, array as *const _ as *mut c_void);
549-
assert_eq!(base_address, base as *mut c_void);
549+
assert_ne!(base_address, view.as_ptr().cast::<c_void>());
550+
assert_ne!(base_address, array.as_ptr().cast::<c_void>());
551+
assert_eq!(base_address, base.cast::<c_void>());
550552

551553
let data_range = data_range(view.as_array_ptr());
552-
assert_eq!(data_range.0, array.data() as *mut c_char);
553-
assert_eq!(data_range.1, unsafe { array.data().add(4) } as *mut c_char);
554+
assert_eq!(data_range.0, array.data().cast::<c_char>());
555+
assert_eq!(data_range.1, unsafe {
556+
array.data().add(4).cast::<c_char>()
557+
});
554558
});
555559
}
556560

@@ -605,52 +609,54 @@ mod tests {
605609
#[test]
606610
fn view_of_view_with_base_object() {
607611
Python::with_gil(|py| {
608-
let array = Array::<f64, _>::zeros((1, 2, 3)).into_pyarray(py);
612+
let array = Array::<f64, _>::zeros((1, 2, 3)).into_pyarray_bound(py);
609613

610-
let locals = [("array", array)].into_py_dict(py);
614+
let locals = [("array", &array)].into_py_dict_bound(py);
611615
let view1 = py
612-
.eval("array[:,:,0]", None, Some(locals))
616+
.eval_bound("array[:,:,0]", None, Some(&locals))
613617
.unwrap()
614-
.downcast::<PyArray2<f64>>()
618+
.downcast_into::<PyArray2<f64>>()
615619
.unwrap();
616620
assert_ne!(
617-
view1 as *const _ as *mut c_void,
618-
array as *const _ as *mut c_void
621+
view1.as_ptr().cast::<c_void>(),
622+
array.as_ptr().cast::<c_void>(),
619623
);
620624

621-
let locals = [("view1", view1)].into_py_dict(py);
625+
let locals = [("view1", &view1)].into_py_dict_bound(py);
622626
let view2 = py
623-
.eval("view1[:,0]", None, Some(locals))
627+
.eval_bound("view1[:,0]", None, Some(&locals))
624628
.unwrap()
625-
.downcast::<PyArray1<f64>>()
629+
.downcast_into::<PyArray1<f64>>()
626630
.unwrap();
627631
assert_ne!(
628-
view2 as *const _ as *mut c_void,
629-
array as *const _ as *mut c_void
632+
view2.as_ptr().cast::<c_void>(),
633+
array.as_ptr().cast::<c_void>(),
630634
);
631635
assert_ne!(
632-
view2 as *const _ as *mut c_void,
633-
view1 as *const _ as *mut c_void
636+
view2.as_ptr().cast::<c_void>(),
637+
view1.as_ptr().cast::<c_void>(),
634638
);
635639

636640
let base = unsafe { (*view2.as_array_ptr()).base };
637-
assert_eq!(base as *mut c_void, array as *const _ as *mut c_void);
641+
assert_eq!(base.cast::<c_void>(), array.as_ptr().cast::<c_void>());
638642

639643
let base = unsafe { (*view1.as_array_ptr()).base };
640-
assert_eq!(base as *mut c_void, array as *const _ as *mut c_void);
644+
assert_eq!(base.cast::<c_void>(), array.as_ptr().cast::<c_void>());
641645

642646
let base = unsafe { (*array.as_array_ptr()).base };
643647
assert!(!base.is_null());
644648

645649
let base_address = base_address(py, view2.as_array_ptr());
646-
assert_ne!(base_address, view2 as *const _ as *mut c_void);
647-
assert_ne!(base_address, view1 as *const _ as *mut c_void);
648-
assert_ne!(base_address, array as *const _ as *mut c_void);
649-
assert_eq!(base_address, base as *mut c_void);
650+
assert_ne!(base_address, view2.as_ptr().cast::<c_void>());
651+
assert_ne!(base_address, view1.as_ptr().cast::<c_void>());
652+
assert_ne!(base_address, array.as_ptr().cast::<c_void>());
653+
assert_eq!(base_address, base.cast::<c_void>());
650654

651655
let data_range = data_range(view2.as_array_ptr());
652-
assert_eq!(data_range.0, array.data() as *mut c_char);
653-
assert_eq!(data_range.1, unsafe { array.data().add(1) } as *mut c_char);
656+
assert_eq!(data_range.0, array.data().cast::<c_char>());
657+
assert_eq!(data_range.1, unsafe {
658+
array.data().add(1).cast::<c_char>()
659+
});
654660
});
655661
}
656662

0 commit comments

Comments
 (0)