Skip to content

Commit baa3fe8

Browse files
authored
Merge pull request #81 from kngwyu/to-owned
Return Py<T> in to_owned
2 parents eee5e5a + bff4d7f commit baa3fe8

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extern crate pyo3;
115115

116116
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
117117
use numpy::{IntoPyArray, PyArrayDyn};
118-
use pyo3::prelude::{pymodinit, PyModule, PyResult, Python};
118+
use pyo3::prelude::{pymodinit, Py, PyModule, PyResult, Python};
119119

120120
#[pymodinit]
121121
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
@@ -136,12 +136,10 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
136136
a: f64,
137137
x: &PyArrayDyn<f64>,
138138
y: &PyArrayDyn<f64>,
139-
) -> PyResult<PyArrayDyn<f64>> {
140-
// you can convert numpy error into PyErr via ?
139+
) -> Py<PyArrayDyn<f64>> {
141140
let x = x.as_array();
142-
// you can also specify your error context, via closure
143141
let y = y.as_array();
144-
Ok(axpy(a, x, y).into_pyarray(py).to_owned(py))
142+
axpy(a, x, y).into_pyarray(py).to_owned()
145143
}
146144

147145
// wrapper of `mult`

examples/simple-extension/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate pyo3;
44

55
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
66
use numpy::{IntoPyArray, PyArrayDyn};
7-
use pyo3::prelude::{pymodinit, PyModule, PyResult, Python};
7+
use pyo3::prelude::{pymodinit, Py, PyModule, PyResult, Python};
88

99
#[pymodinit]
1010
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
@@ -25,12 +25,10 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
2525
a: f64,
2626
x: &PyArrayDyn<f64>,
2727
y: &PyArrayDyn<f64>,
28-
) -> PyResult<PyArrayDyn<f64>> {
29-
// you can convert numpy error into PyErr via ?
28+
) -> Py<PyArrayDyn<f64>> {
3029
let x = x.as_array();
31-
// you can also specify your error context, via closure
3230
let y = y.as_array();
33-
Ok(axpy(a, x, y).into_pyarray(py).to_owned(py))
31+
axpy(a, x, y).into_pyarray(py).to_owned()
3432
}
3533

3634
// wrapper of `mult`

src/array.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,28 @@ impl<T, D> PyArray<T, D> {
146146
self.as_ptr() as _
147147
}
148148

149-
// TODO: 'increasing ref counts' is really collect approach for extension?
150-
/// Get `PyArray` from `&PyArray`, by increasing ref counts.
149+
/// Get `Py<PyArray>` from `&PyArray`, which is the owned wrapper of PyObject.
151150
///
152151
/// You can use this method when you have to avoid lifetime annotation to your function args
153152
/// or return types, like used with pyo3's `pymethod`.
154153
///
155154
/// # Example
156155
/// ```
157156
/// # extern crate pyo3; extern crate numpy; fn main() {
158-
/// use pyo3::{GILGuard, Python};
157+
/// use pyo3::{GILGuard, Python, Py, AsPyRef};
159158
/// use numpy::PyArray1;
160-
/// fn return_py_array() -> PyArray1<i32> {
159+
/// fn return_py_array() -> Py<PyArray1<i32>> {
161160
/// let gil = Python::acquire_gil();
162161
/// let array = PyArray1::zeros(gil.python(), [5], false);
163-
/// array.to_owned(gil.python())
162+
/// array.to_owned()
164163
/// }
164+
/// let gil = Python::acquire_gil();
165165
/// let array = return_py_array();
166-
/// assert_eq!(array.as_slice(), &[0, 0, 0, 0, 0]);
166+
/// assert_eq!(array.as_ref(gil.python()).as_slice(), &[0, 0, 0, 0, 0]);
167167
/// # }
168168
/// ```
169-
pub fn to_owned(&self, py: Python) -> Self {
170-
let obj = unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) };
171-
PyArray(obj, PhantomData, PhantomData)
169+
pub fn to_owned(&self) -> Py<Self> {
170+
unsafe { Py::from_borrowed_ptr(self.as_ptr()) }
172171
}
173172

174173
/// Constructs `PyArray` from raw python object without incrementing reference counts.

0 commit comments

Comments
 (0)