Skip to content

Commit 070127f

Browse files
committed
switch from ToPyObject to IntoPyObject
1 parent 1692669 commit 070127f

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

benches/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use test::{black_box, Bencher};
66
use std::ops::Range;
77

88
use numpy::{PyArray1, PyArray2, PyArray3};
9-
use pyo3::{types::PyAnyMethods, Bound, Python, ToPyObject};
9+
use pyo3::{types::PyAnyMethods, Bound, Python};
1010

1111
#[bench]
1212
fn extract_success(bencher: &mut Bencher) {

src/dtype.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use num_traits::{Bounded, Zero};
88
#[cfg(feature = "half")]
99
use pyo3::sync::GILOnceCell;
1010
use pyo3::{
11+
conversion::IntoPyObject,
1112
exceptions::{PyIndexError, PyValueError},
1213
ffi::{self, PyTuple_Size},
1314
pyobject_native_type_named,
1415
types::{PyAnyMethods, PyDict, PyDictMethods, PyTuple, PyType},
15-
Borrowed, Bound, Py, PyAny, PyObject, PyResult, PyTypeInfo, Python, ToPyObject,
16+
Borrowed, Bound, Py, PyAny, PyObject, PyResult, PyTypeInfo, Python,
1617
};
1718

1819
use crate::npyffi::{
@@ -80,8 +81,14 @@ impl PyArrayDescr {
8081
///
8182
/// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html
8283
#[inline]
83-
pub fn new<'py, T: ToPyObject + ?Sized>(py: Python<'py>, ob: &T) -> PyResult<Bound<'py, Self>> {
84-
fn inner(py: Python<'_>, obj: PyObject) -> PyResult<Bound<'_, PyArrayDescr>> {
84+
pub fn new<'a, 'py, T>(py: Python<'py>, ob: T) -> PyResult<Bound<'py, Self>>
85+
where
86+
T: IntoPyObject<'py>,
87+
{
88+
fn inner<'py>(
89+
py: Python<'py>,
90+
obj: Borrowed<'_, 'py, PyAny>,
91+
) -> PyResult<Bound<'py, PyArrayDescr>> {
8592
let mut descr: *mut PyArray_Descr = ptr::null_mut();
8693
unsafe {
8794
// None is an invalid input here and is not converted to NPY_DEFAULT_TYPE
@@ -91,17 +98,24 @@ impl PyArrayDescr {
9198
}
9299
}
93100

94-
inner(py, ob.to_object(py))
101+
inner(
102+
py,
103+
ob.into_pyobject(py)
104+
.map_err(Into::into)?
105+
.into_any()
106+
.as_borrowed(),
107+
)
95108
}
96109

97110
/// Deprecated name for [`PyArrayDescr::new`].
98111
#[deprecated(since = "0.23.0", note = "renamed to `PyArrayDescr::new`")]
112+
#[allow(deprecated)]
99113
#[inline]
100-
pub fn new_bound<'py, T: ToPyObject + ?Sized>(
114+
pub fn new_bound<'py, T: pyo3::ToPyObject + ?Sized>(
101115
py: Python<'py>,
102116
ob: &T,
103117
) -> PyResult<Bound<'py, Self>> {
104-
Self::new(py, ob)
118+
Self::new(py, ob.to_object(py))
105119
}
106120

107121
/// Shortcut for creating a type descriptor of `object` type.
@@ -598,6 +612,7 @@ macro_rules! clone_methods_impl {
598612
};
599613
}
600614
pub(crate) use clone_methods_impl;
615+
use pyo3::BoundObject;
601616

602617
macro_rules! impl_element_scalar {
603618
(@impl: $ty:ty, $npy_type:expr $(,#[$meta:meta])*) => {
@@ -692,7 +707,7 @@ mod tests {
692707
assert!(dt.get_field("a").unwrap().0.is(&dtype::<PyObject>(py)));
693708
assert!(dt.get_field("b").unwrap().0.is(&dtype::<bool>(py)));
694709

695-
assert!(PyArrayDescr::new(py, &123_usize).is_err());
710+
assert!(PyArrayDescr::new(py, 123_usize).is_err());
696711
});
697712
}
698713

src/error.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::error::Error;
44
use std::fmt;
55

66
use pyo3::{
7-
exceptions::PyTypeError, Bound, Py, PyErr, PyErrArguments, PyObject, Python, ToPyObject,
7+
conversion::IntoPyObject, exceptions::PyTypeError, Bound, Py, PyErr, PyErrArguments, PyObject,
8+
Python,
89
};
910

1011
use crate::dtype::PyArrayDescr;
@@ -22,7 +23,11 @@ macro_rules! impl_pyerr {
2223

2324
impl PyErrArguments for $err_type {
2425
fn arguments<'py>(self, py: Python<'py>) -> PyObject {
25-
self.to_string().to_object(py)
26+
self.to_string()
27+
.into_pyobject(py)
28+
.unwrap()
29+
.into_any()
30+
.unbind()
2631
}
2732
}
2833

@@ -92,7 +97,11 @@ impl PyErrArguments for TypeErrorArguments {
9297
to: self.to.into_bound(py),
9398
};
9499

95-
err.to_string().to_object(py)
100+
err.to_string()
101+
.into_pyobject(py)
102+
.unwrap()
103+
.into_any()
104+
.unbind()
96105
}
97106
}
98107

tests/to_py.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use numpy::{prelude::*, PyArray};
66
use pyo3::{
77
py_run,
88
types::{PyAnyMethods, PyDict, PyString},
9-
Python, ToPyObject,
9+
Python,
1010
};
1111

1212
#[test]
@@ -235,7 +235,7 @@ fn to_pyarray_object_vec() {
235235
let dict = PyDict::new(py);
236236
let string = PyString::new(py, "Hello:)");
237237
#[allow(clippy::useless_vec)] // otherwise we do not test the right trait impl
238-
let vec = vec![dict.to_object(py), string.to_object(py)];
238+
let vec = vec![dict.into_any().unbind(), string.into_any().unbind()];
239239

240240
let arr = vec.to_pyarray(py);
241241

@@ -252,8 +252,8 @@ fn to_pyarray_object_vec() {
252252
fn to_pyarray_object_array() {
253253
Python::with_gil(|py| {
254254
let mut nd_arr = Array2::from_shape_fn((2, 3), |(_, _)| py.None());
255-
nd_arr[(0, 2)] = PyDict::new(py).to_object(py);
256-
nd_arr[(1, 0)] = PyString::new(py, "Hello:)").to_object(py);
255+
nd_arr[(0, 2)] = PyDict::new(py).into_any().unbind();
256+
nd_arr[(1, 0)] = PyString::new(py, "Hello:)").into_any().unbind();
257257

258258
let py_arr = nd_arr.to_pyarray(py);
259259

@@ -275,8 +275,8 @@ fn to_pyarray_object_array() {
275275
fn slice_container_type_confusion() {
276276
Python::with_gil(|py| {
277277
let mut nd_arr = Array2::from_shape_fn((2, 3), |(_, _)| py.None());
278-
nd_arr[(0, 2)] = PyDict::new(py).to_object(py);
279-
nd_arr[(1, 0)] = PyString::new(py, "Hello:)").to_object(py);
278+
nd_arr[(0, 2)] = PyDict::new(py).into_any().unbind();
279+
nd_arr[(1, 0)] = PyString::new(py, "Hello:)").into_any().unbind();
280280

281281
let _py_arr = nd_arr.into_pyarray(py);
282282

0 commit comments

Comments
 (0)