Skip to content

Commit

Permalink
switch from ToPyObject to IntoPyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Oct 9, 2024
1 parent d99f6c2 commit eea9f73
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use test::{black_box, Bencher};
use std::ops::Range;

use numpy::{PyArray1, PyArray2, PyArray3};
use pyo3::{types::PyAnyMethods, Bound, Python, ToPyObject};
use pyo3::{types::PyAnyMethods, Bound, Python};

#[bench]
fn extract_success(bencher: &mut Bencher) {
Expand Down
29 changes: 22 additions & 7 deletions src/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use num_traits::{Bounded, Zero};
#[cfg(feature = "half")]
use pyo3::sync::GILOnceCell;
use pyo3::{
conversion::IntoPyObject,
exceptions::{PyIndexError, PyValueError},
ffi::{self, PyTuple_Size},
pyobject_native_type_named,
types::{PyAnyMethods, PyDict, PyDictMethods, PyTuple, PyType},
Borrowed, Bound, Py, PyAny, PyObject, PyResult, PyTypeInfo, Python, ToPyObject,
Borrowed, Bound, Py, PyAny, PyObject, PyResult, PyTypeInfo, Python,
};

use crate::npyffi::{
Expand Down Expand Up @@ -80,8 +81,14 @@ impl PyArrayDescr {
///
/// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html
#[inline]
pub fn new<'py, T: ToPyObject + ?Sized>(py: Python<'py>, ob: &T) -> PyResult<Bound<'py, Self>> {
fn inner(py: Python<'_>, obj: PyObject) -> PyResult<Bound<'_, PyArrayDescr>> {
pub fn new<'a, 'py, T>(py: Python<'py>, ob: T) -> PyResult<Bound<'py, Self>>
where
T: IntoPyObject<'py>,
{
fn inner<'py>(
py: Python<'py>,
obj: Borrowed<'_, 'py, PyAny>,
) -> PyResult<Bound<'py, PyArrayDescr>> {
let mut descr: *mut PyArray_Descr = ptr::null_mut();
unsafe {
// None is an invalid input here and is not converted to NPY_DEFAULT_TYPE
Expand All @@ -91,17 +98,24 @@ impl PyArrayDescr {
}
}

inner(py, ob.to_object(py))
inner(
py,
ob.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
)
}

/// Deprecated name for [`PyArrayDescr::new`].
#[deprecated(since = "0.23.0", note = "renamed to `PyArrayDescr::new`")]
#[allow(deprecated)]
#[inline]
pub fn new_bound<'py, T: ToPyObject + ?Sized>(
pub fn new_bound<'py, T: pyo3::ToPyObject + ?Sized>(
py: Python<'py>,
ob: &T,
) -> PyResult<Bound<'py, Self>> {
Self::new(py, ob)
Self::new(py, ob.to_object(py))
}

/// Shortcut for creating a type descriptor of `object` type.
Expand Down Expand Up @@ -598,6 +612,7 @@ macro_rules! clone_methods_impl {
};
}
pub(crate) use clone_methods_impl;
use pyo3::BoundObject;

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

assert!(PyArrayDescr::new(py, &123_usize).is_err());
assert!(PyArrayDescr::new(py, 123_usize).is_err());
});
}

Expand Down
15 changes: 12 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::error::Error;
use std::fmt;

use pyo3::{
exceptions::PyTypeError, Bound, Py, PyErr, PyErrArguments, PyObject, Python, ToPyObject,
conversion::IntoPyObject, exceptions::PyTypeError, Bound, Py, PyErr, PyErrArguments, PyObject,
Python,
};

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

impl PyErrArguments for $err_type {
fn arguments<'py>(self, py: Python<'py>) -> PyObject {
self.to_string().to_object(py)
self.to_string()
.into_pyobject(py)
.unwrap()
.into_any()
.unbind()
}
}

Expand Down Expand Up @@ -92,7 +97,11 @@ impl PyErrArguments for TypeErrorArguments {
to: self.to.into_bound(py),
};

err.to_string().to_object(py)
err.to_string()
.into_pyobject(py)
.unwrap()
.into_any()
.unbind()
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/to_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use numpy::{prelude::*, PyArray};
use pyo3::{
py_run,
types::{PyAnyMethods, PyDict, PyString},
Python, ToPyObject,
Python,
};

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

let arr = vec.to_pyarray(py);

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

let py_arr = nd_arr.to_pyarray(py);

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

let _py_arr = nd_arr.into_pyarray(py);

Expand Down

0 comments on commit eea9f73

Please sign in to comment.