diff --git a/guide/src/class.md b/guide/src/class.md index c20cacd3cc7..6a80fd7ad9c 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -1258,8 +1258,8 @@ enum Shape { # #[cfg(Py_3_10)] Python::with_gil(|py| { - let circle = Shape::Circle { radius: 10.0 }.into_py(py); - let square = Shape::RegularPolygon(4, 10.0).into_py(py); + let circle = Shape::Circle { radius: 10.0 }.into_pyobject(py)?; + let square = Shape::RegularPolygon(4, 10.0).into_pyobject(py)?; let cls = py.get_type::(); pyo3::py_run!(py, circle square cls, r#" assert isinstance(circle, cls) @@ -1284,11 +1284,13 @@ Python::with_gil(|py| { assert count_vertices(cls, circle) == 0 assert count_vertices(cls, square) == 4 - "#) + "#); +# Ok::<_, PyErr>(()) }) +# .unwrap(); ``` -WARNING: `Py::new` and `.into_py` are currently inconsistent. Note how the constructed value is _not_ an instance of the specific variant. For this reason, constructing values is only recommended using `.into_py`. +WARNING: `Py::new` and `.into_pyobject` are currently inconsistent. Note how the constructed value is _not_ an instance of the specific variant. For this reason, constructing values is only recommended using `.into_pyobject`. ```rust # use pyo3::prelude::*; @@ -1404,6 +1406,7 @@ impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a } } +#[allow(deprecated)] impl pyo3::IntoPy for MyClass { fn into_py(self, py: pyo3::Python<'_>) -> pyo3::PyObject { pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py) diff --git a/guide/src/class/protocols.md b/guide/src/class/protocols.md index c5ad847834f..4d553c276eb 100644 --- a/guide/src/class/protocols.md +++ b/guide/src/class/protocols.md @@ -97,19 +97,21 @@ given signatures should be interpreted as follows: ```rust use pyo3::class::basic::CompareOp; + use pyo3::types::PyNotImplemented; # use pyo3::prelude::*; + # use pyo3::BoundObject; # # #[pyclass] # struct Number(i32); # #[pymethods] impl Number { - fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyObject { + fn __richcmp__<'py>(&self, other: &Self, op: CompareOp, py: Python<'py>) -> PyResult> { match op { - CompareOp::Eq => (self.0 == other.0).into_py(py), - CompareOp::Ne => (self.0 != other.0).into_py(py), - _ => py.NotImplemented(), + CompareOp::Eq => Ok((self.0 == other.0).into_pyobject(py)?.into_any()), + CompareOp::Ne => Ok((self.0 != other.0).into_pyobject(py)?.into_any()), + _ => Ok(PyNotImplemented::get(py).into_any()), } } } diff --git a/guide/src/conversions/traits.md b/guide/src/conversions/traits.md index def32ecc614..6cc809e0d03 100644 --- a/guide/src/conversions/traits.md +++ b/guide/src/conversions/traits.md @@ -615,6 +615,7 @@ use pyo3::prelude::*; # #[allow(dead_code)] struct MyPyObjectWrapper(PyObject); +#[allow(deprecated)] impl IntoPy for MyPyObjectWrapper { fn into_py(self, py: Python<'_>) -> PyObject { self.0 diff --git a/guide/src/migration.md b/guide/src/migration.md index 6a5b44a6c00..0d76d220dc9 100644 --- a/guide/src/migration.md +++ b/guide/src/migration.md @@ -1217,6 +1217,7 @@ Python::with_gil(|py| { After, some type annotations may be necessary: ```rust +# #![allow(deprecated)] # use pyo3::prelude::*; # # fn main() { @@ -1695,6 +1696,7 @@ After # #[allow(dead_code)] struct MyPyObjectWrapper(PyObject); +# #[allow(deprecated)] impl IntoPy for MyPyObjectWrapper { fn into_py(self, _py: Python<'_>) -> PyObject { self.0 @@ -1714,6 +1716,7 @@ let obj = PyObject::from_py(1.234, py); After: ```rust +# #![allow(deprecated)] # use pyo3::prelude::*; # Python::with_gil(|py| { let obj: PyObject = 1.234.into_py(py); diff --git a/newsfragments/4618.changed.md b/newsfragments/4618.changed.md new file mode 100644 index 00000000000..f3e9dee773d --- /dev/null +++ b/newsfragments/4618.changed.md @@ -0,0 +1 @@ +deprecate `IntoPy` in favor of `IntoPyObject` \ No newline at end of file diff --git a/pyo3-macros-backend/src/pyclass.rs b/pyo3-macros-backend/src/pyclass.rs index d7edeb0bf24..e44ac890c9c 100644 --- a/pyo3-macros-backend/src/pyclass.rs +++ b/pyo3-macros-backend/src/pyclass.rs @@ -1044,6 +1044,7 @@ fn impl_complex_enum( .collect(); quote! { + #[allow(deprecated)] impl #pyo3_path::IntoPy<#pyo3_path::PyObject> for #cls { fn into_py(self, py: #pyo3_path::Python) -> #pyo3_path::PyObject { match self { @@ -1349,13 +1350,11 @@ fn impl_complex_enum_tuple_variant_getitem( let match_arms: Vec<_> = (0..num_fields) .map(|i| { let field_access = format_ident!("_{}", i); - quote! { - #i => ::std::result::Result::Ok( - #pyo3_path::IntoPy::into_py( - #variant_cls::#field_access(slf)? - , py) - ) - + quote! { #i => + #pyo3_path::IntoPyObject::into_pyobject(#variant_cls::#field_access(slf)?, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) } }) .collect(); @@ -1837,10 +1836,16 @@ fn pyclass_richcmp_arms( .map(|span| { quote_spanned! { span => #pyo3_path::pyclass::CompareOp::Eq => { - ::std::result::Result::Ok(#pyo3_path::conversion::IntoPy::into_py(self_val == other, py)) + #pyo3_path::IntoPyObject::into_pyobject(self_val == other, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) }, #pyo3_path::pyclass::CompareOp::Ne => { - ::std::result::Result::Ok(#pyo3_path::conversion::IntoPy::into_py(self_val != other, py)) + #pyo3_path::IntoPyObject::into_pyobject(self_val != other, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) }, } }) @@ -1855,16 +1860,28 @@ fn pyclass_richcmp_arms( .map(|ord| { quote_spanned! { ord.span() => #pyo3_path::pyclass::CompareOp::Gt => { - ::std::result::Result::Ok(#pyo3_path::conversion::IntoPy::into_py(self_val > other, py)) + #pyo3_path::IntoPyObject::into_pyobject(self_val > other, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) }, #pyo3_path::pyclass::CompareOp::Lt => { - ::std::result::Result::Ok(#pyo3_path::conversion::IntoPy::into_py(self_val < other, py)) + #pyo3_path::IntoPyObject::into_pyobject(self_val < other, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) }, #pyo3_path::pyclass::CompareOp::Le => { - ::std::result::Result::Ok(#pyo3_path::conversion::IntoPy::into_py(self_val <= other, py)) + #pyo3_path::IntoPyObject::into_pyobject(self_val <= other, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) }, #pyo3_path::pyclass::CompareOp::Ge => { - ::std::result::Result::Ok(#pyo3_path::conversion::IntoPy::into_py(self_val >= other, py)) + #pyo3_path::IntoPyObject::into_pyobject(self_val >= other, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) }, } }) @@ -2145,6 +2162,7 @@ impl<'a> PyClassImplsBuilder<'a> { // If #cls is not extended type, we allow Self->PyObject conversion if attr.options.extends.is_none() { quote! { + #[allow(deprecated)] impl #pyo3_path::IntoPy<#pyo3_path::PyObject> for #cls { fn into_py(self, py: #pyo3_path::Python<'_>) -> #pyo3_path::PyObject { #pyo3_path::IntoPy::into_py(#pyo3_path::Py::new(py, self).unwrap(), py) diff --git a/pyo3-macros-backend/src/pyimpl.rs b/pyo3-macros-backend/src/pyimpl.rs index 1c3d7f766c2..614db3c5459 100644 --- a/pyo3-macros-backend/src/pyimpl.rs +++ b/pyo3-macros-backend/src/pyimpl.rs @@ -213,7 +213,10 @@ pub fn gen_py_const(cls: &syn::Type, spec: &ConstSpec, ctx: &Ctx) -> MethodAndMe let associated_method = quote! { fn #wrapper_ident(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - ::std::result::Result::Ok(#pyo3_path::IntoPy::into_py(#cls::#member, py)) + #pyo3_path::IntoPyObject::into_pyobject(#cls::#member, py) + .map(#pyo3_path::BoundObject::into_any) + .map(#pyo3_path::BoundObject::unbind) + .map_err(::std::convert::Into::into) } }; diff --git a/src/conversion.rs b/src/conversion.rs index e551e7c1133..a280055cc7c 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -19,16 +19,17 @@ use std::convert::Infallible; /// /// ```rust /// use pyo3::prelude::*; -/// use pyo3::types::PyString; /// use pyo3::ffi; /// /// Python::with_gil(|py| { -/// let s: Py = "foo".into_py(py); +/// let s = "foo".into_pyobject(py)?; /// let ptr = s.as_ptr(); /// /// let is_really_a_pystring = unsafe { ffi::PyUnicode_CheckExact(ptr) }; /// assert_eq!(is_really_a_pystring, 1); -/// }); +/// # Ok::<_, PyErr>(()) +/// }) +/// # .unwrap(); /// ``` /// /// # Safety @@ -41,18 +42,21 @@ use std::convert::Infallible; /// # use pyo3::ffi; /// # /// Python::with_gil(|py| { -/// let ptr: *mut ffi::PyObject = 0xabad1dea_u32.into_py(py).as_ptr(); +/// // ERROR: calling `.as_ptr()` will throw away the temporary object and leave `ptr` dangling. +/// let ptr: *mut ffi::PyObject = 0xabad1dea_u32.into_pyobject(py)?.as_ptr(); /// /// let isnt_a_pystring = unsafe { /// // `ptr` is dangling, this is UB /// ffi::PyUnicode_CheckExact(ptr) /// }; -/// # assert_eq!(isnt_a_pystring, 0); -/// }); +/// # assert_eq!(isnt_a_pystring, 0); +/// # Ok::<_, PyErr>(()) +/// }) +/// # .unwrap(); /// ``` /// /// This happens because the pointer returned by `as_ptr` does not carry any lifetime information -/// and the Python object is dropped immediately after the `0xabad1dea_u32.into_py(py).as_ptr()` +/// and the Python object is dropped immediately after the `0xabad1dea_u32.into_pyobject(py).as_ptr()` /// expression is evaluated. To fix the problem, bind Python object to a local variable like earlier /// to keep the Python object alive until the end of its scope. /// @@ -100,6 +104,7 @@ pub trait ToPyObject { /// However, it may not be desirable to expose the existence of `Number` to Python code. /// `IntoPy` allows us to define a conversion to an appropriate Python object. /// ```rust +/// #![allow(deprecated)] /// use pyo3::prelude::*; /// /// # #[allow(dead_code)] @@ -121,6 +126,7 @@ pub trait ToPyObject { /// This is useful for types like enums that can carry different types. /// /// ```rust +/// #![allow(deprecated)] /// use pyo3::prelude::*; /// /// enum Value { @@ -161,6 +167,10 @@ pub trait ToPyObject { note = "if you do not own `{Self}` you can perform a manual conversion to one of the types in `pyo3::types::*`" ) )] +#[deprecated( + since = "0.23.0", + note = "`IntoPy` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23/migration) for more information." +)] pub trait IntoPy: Sized { /// Performs the conversion. fn into_py(self, py: Python<'_>) -> T; @@ -503,6 +513,7 @@ where } /// Converts `()` to an empty Python tuple. +#[allow(deprecated)] impl IntoPy> for () { fn into_py(self, py: Python<'_>) -> Py { PyTuple::empty(py).unbind() diff --git a/src/conversions/chrono.rs b/src/conversions/chrono.rs index ce94b98c1a3..90f9c69761d 100644 --- a/src/conversions/chrono.rs +++ b/src/conversions/chrono.rs @@ -54,11 +54,11 @@ use crate::types::{ timezone_utc, PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess, PyTzInfo, PyTzInfoAccess, }; -#[allow(deprecated)] -use crate::ToPyObject; -use crate::{ffi, Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python}; +use crate::{ffi, Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python}; #[cfg(Py_LIMITED_API)] use crate::{intern, DowncastError}; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use chrono::offset::{FixedOffset, Utc}; use chrono::{ DateTime, Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, @@ -72,6 +72,7 @@ impl ToPyObject for Duration { } } +#[allow(deprecated)] impl IntoPy for Duration { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -178,6 +179,7 @@ impl ToPyObject for NaiveDate { } } +#[allow(deprecated)] impl IntoPy for NaiveDate { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -244,6 +246,7 @@ impl ToPyObject for NaiveTime { } } +#[allow(deprecated)] impl IntoPy for NaiveTime { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -320,6 +323,7 @@ impl ToPyObject for NaiveDateTime { } } +#[allow(deprecated)] impl IntoPy for NaiveDateTime { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -411,6 +415,7 @@ impl ToPyObject for DateTime { } } +#[allow(deprecated)] impl IntoPy for DateTime { fn into_py(self, py: Python<'_>) -> PyObject { self.into_pyobject(py).unwrap().into_any().unbind() @@ -505,6 +510,7 @@ impl ToPyObject for FixedOffset { } } +#[allow(deprecated)] impl IntoPy for FixedOffset { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -589,6 +595,7 @@ impl ToPyObject for Utc { } } +#[allow(deprecated)] impl IntoPy for Utc { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -1408,8 +1415,8 @@ mod tests { // python values of durations (from -999999999 to 999999999 days), Python::with_gil(|py| { let dur = Duration::days(days); - let py_delta = dur.into_py(py); - let roundtripped: Duration = py_delta.extract(py).expect("Round trip"); + let py_delta = dur.into_pyobject(py).unwrap(); + let roundtripped: Duration = py_delta.extract().expect("Round trip"); assert_eq!(dur, roundtripped); }) } @@ -1418,8 +1425,8 @@ mod tests { fn test_fixed_offset_roundtrip(secs in -86399i32..=86399i32) { Python::with_gil(|py| { let offset = FixedOffset::east_opt(secs).unwrap(); - let py_offset = offset.into_py(py); - let roundtripped: FixedOffset = py_offset.extract(py).expect("Round trip"); + let py_offset = offset.into_pyobject(py).unwrap(); + let roundtripped: FixedOffset = py_offset.extract().expect("Round trip"); assert_eq!(offset, roundtripped); }) } @@ -1504,8 +1511,8 @@ mod tests { if let (Some(date), Some(time)) = (date_opt, time_opt) { let dt: DateTime = NaiveDateTime::new(date, time).and_utc(); // Wrap in CatchWarnings to avoid into_py firing warning for truncated leap second - let py_dt = CatchWarnings::enter(py, |_| Ok(dt.into_py(py))).unwrap(); - let roundtripped: DateTime = py_dt.extract(py).expect("Round trip"); + let py_dt = CatchWarnings::enter(py, |_| dt.into_pyobject(py)).unwrap(); + let roundtripped: DateTime = py_dt.extract().expect("Round trip"); // Leap seconds are not roundtripped let expected_roundtrip_time = micro.checked_sub(1_000_000).map(|micro| NaiveTime::from_hms_micro_opt(hour, min, sec, micro).unwrap()).unwrap_or(time); let expected_roundtrip_dt: DateTime = NaiveDateTime::new(date, expected_roundtrip_time).and_utc(); @@ -1532,8 +1539,8 @@ mod tests { if let (Some(date), Some(time)) = (date_opt, time_opt) { let dt: DateTime = NaiveDateTime::new(date, time).and_local_timezone(offset).unwrap(); // Wrap in CatchWarnings to avoid into_py firing warning for truncated leap second - let py_dt = CatchWarnings::enter(py, |_| Ok(dt.into_py(py))).unwrap(); - let roundtripped: DateTime = py_dt.extract(py).expect("Round trip"); + let py_dt = CatchWarnings::enter(py, |_| dt.into_pyobject(py)).unwrap(); + let roundtripped: DateTime = py_dt.extract().expect("Round trip"); // Leap seconds are not roundtripped let expected_roundtrip_time = micro.checked_sub(1_000_000).map(|micro| NaiveTime::from_hms_micro_opt(hour, min, sec, micro).unwrap()).unwrap_or(time); let expected_roundtrip_dt: DateTime = NaiveDateTime::new(date, expected_roundtrip_time).and_local_timezone(offset).unwrap(); diff --git a/src/conversions/chrono_tz.rs b/src/conversions/chrono_tz.rs index 8d324bfcacb..bb1a74c1519 100644 --- a/src/conversions/chrono_tz.rs +++ b/src/conversions/chrono_tz.rs @@ -39,9 +39,9 @@ use crate::exceptions::PyValueError; use crate::pybacked::PyBackedStr; use crate::sync::GILOnceCell; use crate::types::{any::PyAnyMethods, PyType}; +use crate::{intern, Bound, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{intern, Bound, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; use chrono_tz::Tz; use std::str::FromStr; @@ -53,6 +53,7 @@ impl ToPyObject for Tz { } } +#[allow(deprecated)] impl IntoPy for Tz { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { diff --git a/src/conversions/either.rs b/src/conversions/either.rs index 2cf1029ffb6..1fdcd22d7e2 100644 --- a/src/conversions/either.rs +++ b/src/conversions/either.rs @@ -46,15 +46,16 @@ #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, exceptions::PyTypeError, types::any::PyAnyMethods, Bound, - BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, + BoundObject, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use either::Either; #[cfg_attr(docsrs, doc(cfg(feature = "either")))] +#[allow(deprecated)] impl IntoPy for Either where L: IntoPy, diff --git a/src/conversions/hashbrown.rs b/src/conversions/hashbrown.rs index 25ec014341e..0efe7f5161f 100644 --- a/src/conversions/hashbrown.rs +++ b/src/conversions/hashbrown.rs @@ -16,8 +16,6 @@ //! //! Note that you must use compatible versions of hashbrown and PyO3. //! The required hashbrown version may vary based on the version of PyO3. -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, types::{ @@ -27,8 +25,10 @@ use crate::{ set::{new_from_iter, try_new_from_iter, PySetMethods}, PyDict, PyFrozenSet, PySet, }, - Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, + Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use std::{cmp, hash}; #[allow(deprecated)] @@ -47,6 +47,7 @@ where } } +#[allow(deprecated)] impl IntoPy for hashbrown::HashMap where K: hash::Hash + cmp::Eq + IntoPy, @@ -128,6 +129,7 @@ where } } +#[allow(deprecated)] impl IntoPy for hashbrown::HashSet where K: IntoPy + Eq + hash::Hash, @@ -193,7 +195,7 @@ mod tests { use crate::types::IntoPyDict; #[test] - fn test_hashbrown_hashmap_to_python() { + fn test_hashbrown_hashmap_into_pyobject() { Python::with_gil(|py| { let mut map = hashbrown::HashMap::::new(); map.insert(1, 1); @@ -213,27 +215,6 @@ mod tests { assert_eq!(map, py_map.extract().unwrap()); }); } - #[test] - fn test_hashbrown_hashmap_into_python() { - Python::with_gil(|py| { - let mut map = hashbrown::HashMap::::new(); - map.insert(1, 1); - - let m: PyObject = map.into_py(py); - let py_map = m.downcast_bound::(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - }); - } #[test] fn test_hashbrown_hashmap_into_dict() { @@ -270,13 +251,13 @@ mod tests { } #[test] - fn test_hashbrown_hashset_into_py() { + fn test_hashbrown_hashset_into_pyobject() { Python::with_gil(|py| { let hs: hashbrown::HashSet = [1, 2, 3, 4, 5].iter().cloned().collect(); - let hso: PyObject = hs.clone().into_py(py); + let hso = hs.clone().into_pyobject(py).unwrap(); - assert_eq!(hs, hso.extract(py).unwrap()); + assert_eq!(hs, hso.extract().unwrap()); }); } } diff --git a/src/conversions/indexmap.rs b/src/conversions/indexmap.rs index 8d74b126f29..e3787e68091 100644 --- a/src/conversions/indexmap.rs +++ b/src/conversions/indexmap.rs @@ -89,9 +89,9 @@ use crate::conversion::IntoPyObject; use crate::types::*; +use crate::{Bound, FromPyObject, PyErr, PyObject, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{Bound, FromPyObject, IntoPy, PyErr, PyObject, Python}; +use crate::{IntoPy, ToPyObject}; use std::{cmp, hash}; #[allow(deprecated)] @@ -110,6 +110,7 @@ where } } +#[allow(deprecated)] impl IntoPy for indexmap::IndexMap where K: hash::Hash + cmp::Eq + IntoPy, @@ -183,10 +184,10 @@ where mod test_indexmap { use crate::types::*; - use crate::{IntoPy, IntoPyObject, PyObject, Python}; + use crate::{IntoPyObject, Python}; #[test] - fn test_indexmap_indexmap_to_python() { + fn test_indexmap_indexmap_into_pyobject() { Python::with_gil(|py| { let mut map = indexmap::IndexMap::::new(); map.insert(1, 1); @@ -210,28 +211,6 @@ mod test_indexmap { }); } - #[test] - fn test_indexmap_indexmap_into_python() { - Python::with_gil(|py| { - let mut map = indexmap::IndexMap::::new(); - map.insert(1, 1); - - let m: PyObject = map.into_py(py); - let py_map = m.downcast_bound::(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - }); - } - #[test] fn test_indexmap_indexmap_into_dict() { Python::with_gil(|py| { diff --git a/src/conversions/num_bigint.rs b/src/conversions/num_bigint.rs index df8e108776f..d91973b5572 100644 --- a/src/conversions/num_bigint.rs +++ b/src/conversions/num_bigint.rs @@ -49,15 +49,15 @@ #[cfg(Py_LIMITED_API)] use crate::types::{bytes::PyBytesMethods, PyBytes}; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, ffi, instance::Bound, types::{any::PyAnyMethods, PyInt}, - FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python, + FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use num_bigint::{BigInt, BigUint}; @@ -77,6 +77,7 @@ macro_rules! bigint_conversion { } #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] + #[allow(deprecated)] impl IntoPy for $rust_ty { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -451,8 +452,8 @@ mod tests { ($T:ty, $value:expr, $py:expr) => { let value = $value; println!("{}: {}", stringify!($T), value); - let python_value = value.clone().into_py(py); - let roundtrip_value = python_value.extract::<$T>(py).unwrap(); + let python_value = value.clone().into_pyobject(py).unwrap(); + let roundtrip_value = python_value.extract::<$T>().unwrap(); assert_eq!(value, roundtrip_value); }; } diff --git a/src/conversions/num_complex.rs b/src/conversions/num_complex.rs index 4b3be8e15b0..db981a4179b 100644 --- a/src/conversions/num_complex.rs +++ b/src/conversions/num_complex.rs @@ -130,6 +130,7 @@ macro_rules! complex_conversion { } #[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))] + #[allow(deprecated)] impl crate::IntoPy for Complex<$float> { fn into_py(self, py: Python<'_>) -> PyObject { unsafe { diff --git a/src/conversions/num_rational.rs b/src/conversions/num_rational.rs index cda877502b3..6c48f67fcf2 100644 --- a/src/conversions/num_rational.rs +++ b/src/conversions/num_rational.rs @@ -48,9 +48,9 @@ use crate::ffi; use crate::sync::GILOnceCell; use crate::types::any::PyAnyMethods; use crate::types::PyType; +use crate::{Bound, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{Bound, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; #[cfg(feature = "num-bigint")] use num_bigint::BigInt; @@ -91,6 +91,7 @@ macro_rules! rational_conversion { self.into_pyobject(py).unwrap().into_any().unbind() } } + #[allow(deprecated)] impl IntoPy for Ratio<$int> { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -223,9 +224,9 @@ mod tests { #[test] fn test_int_roundtrip() { Python::with_gil(|py| { - let rs_frac = Ratio::new(1, 2); - let py_frac: PyObject = rs_frac.into_py(py); - let roundtripped: Ratio = py_frac.extract(py).unwrap(); + let rs_frac = Ratio::new(1i32, 2); + let py_frac = rs_frac.into_pyobject(py).unwrap(); + let roundtripped: Ratio = py_frac.extract().unwrap(); assert_eq!(rs_frac, roundtripped); // float conversion }) @@ -236,8 +237,8 @@ mod tests { fn test_big_int_roundtrip() { Python::with_gil(|py| { let rs_frac = Ratio::from_float(5.5).unwrap(); - let py_frac: PyObject = rs_frac.clone().into_py(py); - let roundtripped: Ratio = py_frac.extract(py).unwrap(); + let py_frac = rs_frac.clone().into_pyobject(py).unwrap(); + let roundtripped: Ratio = py_frac.extract().unwrap(); assert_eq!(rs_frac, roundtripped); }) } @@ -248,8 +249,8 @@ mod tests { fn test_int_roundtrip(num in any::(), den in any::()) { Python::with_gil(|py| { let rs_frac = Ratio::new(num, den); - let py_frac = rs_frac.into_py(py); - let roundtripped: Ratio = py_frac.extract(py).unwrap(); + let py_frac = rs_frac.into_pyobject(py).unwrap(); + let roundtripped: Ratio = py_frac.extract().unwrap(); assert_eq!(rs_frac, roundtripped); }) } @@ -259,8 +260,8 @@ mod tests { fn test_big_int_roundtrip(num in any::()) { Python::with_gil(|py| { let rs_frac = Ratio::from_float(num).unwrap(); - let py_frac = rs_frac.clone().into_py(py); - let roundtripped: Ratio = py_frac.extract(py).unwrap(); + let py_frac = rs_frac.clone().into_pyobject(py).unwrap(); + let roundtripped: Ratio = py_frac.extract().unwrap(); assert_eq!(roundtripped, rs_frac); }) } diff --git a/src/conversions/rust_decimal.rs b/src/conversions/rust_decimal.rs index 7926592119a..7c6eda21a66 100644 --- a/src/conversions/rust_decimal.rs +++ b/src/conversions/rust_decimal.rs @@ -55,9 +55,9 @@ use crate::sync::GILOnceCell; use crate::types::any::PyAnyMethods; use crate::types::string::PyStringMethods; use crate::types::PyType; +use crate::{Bound, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{Bound, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; use rust_decimal::Decimal; use std::str::FromStr; @@ -90,6 +90,7 @@ impl ToPyObject for Decimal { } } +#[allow(deprecated)] impl IntoPy for Decimal { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -138,7 +139,7 @@ mod test_rust_decimal { fn $name() { Python::with_gil(|py| { let rs_orig = $rs; - let rs_dec = rs_orig.into_py(py); + let rs_dec = rs_orig.into_pyobject(py).unwrap(); let locals = PyDict::new(py); locals.set_item("rs_dec", &rs_dec).unwrap(); // Checks if Rust Decimal -> Python Decimal conversion is correct @@ -181,7 +182,7 @@ mod test_rust_decimal { ) { let num = Decimal::from_parts(lo, mid, high, negative, scale); Python::with_gil(|py| { - let rs_dec = num.into_py(py); + let rs_dec = num.into_pyobject(py).unwrap(); let locals = PyDict::new(py); locals.set_item("rs_dec", &rs_dec).unwrap(); py.run( @@ -189,7 +190,7 @@ mod test_rust_decimal { "import decimal\npy_dec = decimal.Decimal(\"{}\")\nassert py_dec == rs_dec", num)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: Decimal = rs_dec.extract(py).unwrap(); + let roundtripped: Decimal = rs_dec.extract().unwrap(); assert_eq!(num, roundtripped); }) } @@ -197,8 +198,8 @@ mod test_rust_decimal { #[test] fn test_integers(num in any::()) { Python::with_gil(|py| { - let py_num = num.into_py(py); - let roundtripped: Decimal = py_num.extract(py).unwrap(); + let py_num = num.into_pyobject(py).unwrap(); + let roundtripped: Decimal = py_num.extract().unwrap(); let rs_dec = Decimal::new(num, 0); assert_eq!(rs_dec, roundtripped); }) diff --git a/src/conversions/smallvec.rs b/src/conversions/smallvec.rs index a01f204b73d..99244d81417 100644 --- a/src/conversions/smallvec.rs +++ b/src/conversions/smallvec.rs @@ -23,11 +23,9 @@ use crate::types::any::PyAnyMethods; use crate::types::list::new_from_iter; use crate::types::{PySequence, PyString}; use crate::PyErr; +use crate::{err::DowncastError, ffi, Bound, FromPyObject, PyAny, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{ - err::DowncastError, ffi, Bound, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, -}; +use crate::{IntoPy, ToPyObject}; use smallvec::{Array, SmallVec}; #[allow(deprecated)] @@ -41,6 +39,7 @@ where } } +#[allow(deprecated)] impl IntoPy for SmallVec where A: Array, @@ -144,6 +143,7 @@ mod tests { use crate::types::{PyBytes, PyBytesMethods, PyDict, PyList}; #[test] + #[allow(deprecated)] fn test_smallvec_into_py() { Python::with_gil(|py| { let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); diff --git a/src/conversions/std/array.rs b/src/conversions/std/array.rs index c184aa7f732..1c33a610273 100644 --- a/src/conversions/std/array.rs +++ b/src/conversions/std/array.rs @@ -2,11 +2,12 @@ use crate::conversion::IntoPyObject; use crate::instance::Bound; use crate::types::any::PyAnyMethods; use crate::types::PySequence; -#[allow(deprecated)] -use crate::ToPyObject; -use crate::{err::DowncastError, ffi, FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python}; +use crate::{err::DowncastError, ffi, FromPyObject, Py, PyAny, PyObject, PyResult, Python}; use crate::{exceptions, PyErr}; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; +#[allow(deprecated)] impl IntoPy for [T; N] where T: IntoPy, @@ -168,7 +169,7 @@ mod tests { ffi, types::{any::PyAnyMethods, PyBytes, PyBytesMethods}, }; - use crate::{types::PyList, IntoPy, PyResult, Python}; + use crate::{types::PyList, PyResult, Python}; #[test] fn array_try_from_fn() { @@ -246,11 +247,15 @@ mod tests { } #[test] - fn test_intopy_array_conversion() { + fn test_intopyobject_array_conversion() { Python::with_gil(|py| { let array: [f32; 4] = [0.0, -16.0, 16.0, 42.0]; - let pyobject = array.into_py(py); - let pylist = pyobject.downcast_bound::(py).unwrap(); + let pylist = array + .into_pyobject(py) + .unwrap() + .downcast_into::() + .unwrap(); + assert_eq!(pylist.get_item(0).unwrap().extract::().unwrap(), 0.0); assert_eq!(pylist.get_item(1).unwrap().extract::().unwrap(), -16.0); assert_eq!(pylist.get_item(2).unwrap().extract::().unwrap(), 16.0); @@ -290,8 +295,11 @@ mod tests { Python::with_gil(|py| { let array: [Foo; 8] = [Foo, Foo, Foo, Foo, Foo, Foo, Foo, Foo]; - let pyobject = array.into_py(py); - let list = pyobject.downcast_bound::(py).unwrap(); + let list = array + .into_pyobject(py) + .unwrap() + .downcast_into::() + .unwrap(); let _bound = list.get_item(4).unwrap().downcast::().unwrap(); }); } diff --git a/src/conversions/std/cell.rs b/src/conversions/std/cell.rs index 369d91bf69e..70da688b70c 100644 --- a/src/conversions/std/cell.rs +++ b/src/conversions/std/cell.rs @@ -1,8 +1,8 @@ use std::cell::Cell; use crate::{ - conversion::IntoPyObject, types::any::PyAnyMethods, Bound, FromPyObject, IntoPy, PyAny, - PyObject, PyResult, Python, + conversion::IntoPyObject, types::any::PyAnyMethods, Bound, FromPyObject, PyAny, PyObject, + PyResult, Python, }; #[allow(deprecated)] @@ -12,7 +12,8 @@ impl crate::ToPyObject for Cell { } } -impl> IntoPy for Cell { +#[allow(deprecated)] +impl> crate::IntoPy for Cell { fn into_py(self, py: Python<'_>) -> PyObject { self.get().into_py(py) } diff --git a/src/conversions/std/ipaddr.rs b/src/conversions/std/ipaddr.rs index 6c5e74d4881..7ba8da87749 100755 --- a/src/conversions/std/ipaddr.rs +++ b/src/conversions/std/ipaddr.rs @@ -7,9 +7,9 @@ use crate::sync::GILOnceCell; use crate::types::any::PyAnyMethods; use crate::types::string::PyStringMethods; use crate::types::PyType; +use crate::{intern, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{intern, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; impl FromPyObject<'_> for IpAddr { fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { @@ -103,6 +103,7 @@ impl ToPyObject for IpAddr { } } +#[allow(deprecated)] impl IntoPy for IpAddr { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -153,12 +154,12 @@ mod test_ipaddr { "IPv6Address" }; - let pyobj = ip.into_py(py); - let repr = pyobj.bind(py).repr().unwrap(); + let pyobj = ip.into_pyobject(py).unwrap(); + let repr = pyobj.repr().unwrap(); let repr = repr.to_string_lossy(); assert_eq!(repr, format!("{}('{}')", py_cls, ip)); - let ip2: IpAddr = pyobj.extract(py).unwrap(); + let ip2: IpAddr = pyobj.extract().unwrap(); assert_eq!(ip, ip2); } roundtrip(py, "127.0.0.1"); diff --git a/src/conversions/std/map.rs b/src/conversions/std/map.rs index 388c50b2f3e..8a6ba57012e 100644 --- a/src/conversions/std/map.rs +++ b/src/conversions/std/map.rs @@ -2,14 +2,14 @@ use std::{cmp, collections, hash}; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, instance::Bound, types::{any::PyAnyMethods, dict::PyDictMethods, PyDict}, - FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, + FromPyObject, PyAny, PyErr, PyObject, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; #[allow(deprecated)] impl ToPyObject for collections::HashMap @@ -42,6 +42,7 @@ where } } +#[allow(deprecated)] impl IntoPy for collections::HashMap where K: hash::Hash + cmp::Eq + IntoPy, @@ -107,6 +108,7 @@ where } } +#[allow(deprecated)] impl IntoPy for collections::BTreeMap where K: cmp::Eq + IntoPy, @@ -265,8 +267,7 @@ mod tests { let mut map = HashMap::::new(); map.insert(1, 1); - let m: PyObject = map.into_py(py); - let py_map = m.downcast_bound::(py).unwrap(); + let py_map = map.into_pyobject(py).unwrap(); assert!(py_map.len() == 1); assert!( @@ -287,8 +288,7 @@ mod tests { let mut map = BTreeMap::::new(); map.insert(1, 1); - let m: PyObject = map.into_py(py); - let py_map = m.downcast_bound::(py).unwrap(); + let py_map = map.into_pyobject(py).unwrap(); assert!(py_map.len() == 1); assert!( diff --git a/src/conversions/std/num.rs b/src/conversions/std/num.rs index ded970ef9a1..0450c591ae8 100644 --- a/src/conversions/std/num.rs +++ b/src/conversions/std/num.rs @@ -5,11 +5,9 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::inspect::types::TypeInfo; use crate::types::any::PyAnyMethods; use crate::types::{PyBytes, PyInt}; +use crate::{exceptions, ffi, Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{ - exceptions, ffi, Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, -}; +use crate::{IntoPy, ToPyObject}; use std::convert::Infallible; use std::num::{ NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, @@ -26,6 +24,7 @@ macro_rules! int_fits_larger_int { self.into_pyobject(py).unwrap().into_any().unbind() } } + #[allow(deprecated)] impl IntoPy for $rust_type { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -112,6 +111,7 @@ macro_rules! int_convert_u64_or_i64 { self.into_pyobject(py).unwrap().into_any().unbind() } } + #[allow(deprecated)] impl IntoPy for $rust_type { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -173,6 +173,7 @@ macro_rules! int_fits_c_long { self.into_pyobject(py).unwrap().into_any().unbind() } } + #[allow(deprecated)] impl IntoPy for $rust_type { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -237,6 +238,7 @@ impl ToPyObject for u8 { self.into_pyobject(py).unwrap().into_any().unbind() } } +#[allow(deprecated)] impl IntoPy for u8 { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -361,6 +363,8 @@ mod fast_128bit_int_conversion { self.into_pyobject(py).unwrap().into_any().unbind() } } + + #[allow(deprecated)] impl IntoPy for $rust_type { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -514,6 +518,7 @@ mod slow_128bit_int_conversion { } } + #[allow(deprecated)] impl IntoPy for $rust_type { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -571,7 +576,7 @@ mod slow_128bit_int_conversion { -1 as _, ffi::PyLong_AsUnsignedLongLongMask(ob.as_ptr()), )? as $rust_type; - let shift = SHIFT.into_py(py); + let shift = SHIFT.into_pyobject(py)?; let shifted = PyObject::from_owned_ptr_or_err( py, ffi::PyNumber_Rshift(ob.as_ptr(), shift.as_ptr()), @@ -617,6 +622,7 @@ macro_rules! nonzero_int_impl { } } + #[allow(deprecated)] impl IntoPy for $nonzero_type { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -705,11 +711,11 @@ mod test_128bit_integers { #[test] fn test_i128_roundtrip(x: i128) { Python::with_gil(|py| { - let x_py = x.into_py(py); + let x_py = x.into_pyobject(py).unwrap(); let locals = PyDict::new(py); - locals.set_item("x_py", x_py.clone_ref(py)).unwrap(); + locals.set_item("x_py", &x_py).unwrap(); py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: i128 = x_py.extract(py).unwrap(); + let roundtripped: i128 = x_py.extract().unwrap(); assert_eq!(x, roundtripped); }) } @@ -721,11 +727,11 @@ mod test_128bit_integers { .prop_map(|x| NonZeroI128::new(x).unwrap()) ) { Python::with_gil(|py| { - let x_py = x.into_py(py); + let x_py = x.into_pyobject(py).unwrap(); let locals = PyDict::new(py); - locals.set_item("x_py", x_py.clone_ref(py)).unwrap(); + locals.set_item("x_py", &x_py).unwrap(); py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: NonZeroI128 = x_py.extract(py).unwrap(); + let roundtripped: NonZeroI128 = x_py.extract().unwrap(); assert_eq!(x, roundtripped); }) } @@ -736,11 +742,11 @@ mod test_128bit_integers { #[test] fn test_u128_roundtrip(x: u128) { Python::with_gil(|py| { - let x_py = x.into_py(py); + let x_py = x.into_pyobject(py).unwrap(); let locals = PyDict::new(py); - locals.set_item("x_py", x_py.clone_ref(py)).unwrap(); + locals.set_item("x_py", &x_py).unwrap(); py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: u128 = x_py.extract(py).unwrap(); + let roundtripped: u128 = x_py.extract().unwrap(); assert_eq!(x, roundtripped); }) } @@ -752,11 +758,11 @@ mod test_128bit_integers { .prop_map(|x| NonZeroU128::new(x).unwrap()) ) { Python::with_gil(|py| { - let x_py = x.into_py(py); + let x_py = x.into_pyobject(py).unwrap(); let locals = PyDict::new(py); - locals.set_item("x_py", x_py.clone_ref(py)).unwrap(); + locals.set_item("x_py", &x_py).unwrap(); py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: NonZeroU128 = x_py.extract(py).unwrap(); + let roundtripped: NonZeroU128 = x_py.extract().unwrap(); assert_eq!(x, roundtripped); }) } diff --git a/src/conversions/std/option.rs b/src/conversions/std/option.rs index a9c8908faa7..aac8c7ab210 100644 --- a/src/conversions/std/option.rs +++ b/src/conversions/std/option.rs @@ -1,6 +1,6 @@ use crate::{ conversion::IntoPyObject, ffi, types::any::PyAnyMethods, AsPyPointer, Bound, BoundObject, - FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, + FromPyObject, PyAny, PyObject, PyResult, Python, }; /// `Option::Some` is converted like `T`. @@ -16,9 +16,10 @@ where } } -impl IntoPy for Option +#[allow(deprecated)] +impl crate::IntoPy for Option where - T: IntoPy, + T: crate::IntoPy, { fn into_py(self, py: Python<'_>) -> PyObject { self.map_or_else(|| py.None(), |val| val.into_py(py)) diff --git a/src/conversions/std/osstr.rs b/src/conversions/std/osstr.rs index 14c5902d8d9..70b5bd152ac 100644 --- a/src/conversions/std/osstr.rs +++ b/src/conversions/std/osstr.rs @@ -3,9 +3,9 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::instance::Bound; use crate::types::any::PyAnyMethods; use crate::types::PyString; +use crate::{ffi, FromPyObject, PyAny, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{ffi, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; use std::borrow::Cow; use std::convert::Infallible; use std::ffi::{OsStr, OsString}; @@ -134,6 +134,7 @@ impl FromPyObject<'_> for OsString { } } +#[allow(deprecated)] impl IntoPy for &'_ OsStr { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -149,6 +150,7 @@ impl ToPyObject for Cow<'_, OsStr> { } } +#[allow(deprecated)] impl IntoPy for Cow<'_, OsStr> { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -186,6 +188,7 @@ impl ToPyObject for OsString { } } +#[allow(deprecated)] impl IntoPy for OsString { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -204,6 +207,7 @@ impl<'py> IntoPyObject<'py> for OsString { } } +#[allow(deprecated)] impl IntoPy for &OsString { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -225,7 +229,7 @@ impl<'py> IntoPyObject<'py> for &OsString { #[cfg(test)] mod tests { use crate::types::{PyAnyMethods, PyString, PyStringMethods}; - use crate::{BoundObject, IntoPy, IntoPyObject, PyObject, Python}; + use crate::{BoundObject, IntoPyObject, Python}; use std::fmt::Debug; use std::{ borrow::Cow, @@ -246,14 +250,14 @@ mod tests { let os_str = OsStr::from_bytes(payload); // do a roundtrip into Pythonland and back and compare - let py_str: PyObject = os_str.into_py(py); - let os_str_2: OsString = py_str.extract(py).unwrap(); + let py_str = os_str.into_pyobject(py).unwrap(); + let os_str_2: OsString = py_str.extract().unwrap(); assert_eq!(os_str, os_str_2); }); } #[test] - fn test_topyobject_roundtrip() { + fn test_intopyobject_roundtrip() { Python::with_gil(|py| { fn test_roundtrip<'py, T>(py: Python<'py>, obj: T) where @@ -273,24 +277,4 @@ mod tests { test_roundtrip::(py, os_str.to_os_string()); }); } - - #[test] - fn test_intopy_roundtrip() { - Python::with_gil(|py| { - fn test_roundtrip + AsRef + Debug + Clone>( - py: Python<'_>, - obj: T, - ) { - let pyobject = obj.clone().into_py(py); - let pystring = pyobject.downcast_bound::(py).unwrap(); - assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy()); - let roundtripped_obj: OsString = pystring.extract().unwrap(); - assert!(obj.as_ref() == roundtripped_obj.as_os_str()); - } - let os_str = OsStr::new("Hello\0\nšŸ"); - test_roundtrip::<&OsStr>(py, os_str); - test_roundtrip::(py, os_str.to_os_string()); - test_roundtrip::<&OsString>(py, &os_str.to_os_string()); - }) - } } diff --git a/src/conversions/std/path.rs b/src/conversions/std/path.rs index d1b628b065e..dc528ee3595 100644 --- a/src/conversions/std/path.rs +++ b/src/conversions/std/path.rs @@ -3,9 +3,9 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::instance::Bound; use crate::types::any::PyAnyMethods; use crate::types::PyString; +use crate::{ffi, FromPyObject, PyAny, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{ffi, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; use std::borrow::Cow; use std::convert::Infallible; use std::ffi::OsString; @@ -29,6 +29,7 @@ impl FromPyObject<'_> for PathBuf { } } +#[allow(deprecated)] impl IntoPy for &Path { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -66,6 +67,7 @@ impl ToPyObject for Cow<'_, Path> { } } +#[allow(deprecated)] impl IntoPy for Cow<'_, Path> { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -103,6 +105,7 @@ impl ToPyObject for PathBuf { } } +#[allow(deprecated)] impl IntoPy for PathBuf { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -121,6 +124,7 @@ impl<'py> IntoPyObject<'py> for PathBuf { } } +#[allow(deprecated)] impl IntoPy for &PathBuf { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -142,7 +146,7 @@ impl<'py> IntoPyObject<'py> for &PathBuf { #[cfg(test)] mod tests { use crate::types::{PyAnyMethods, PyString, PyStringMethods}; - use crate::{BoundObject, IntoPy, IntoPyObject, PyObject, Python}; + use crate::{BoundObject, IntoPyObject, Python}; use std::borrow::Cow; use std::fmt::Debug; use std::path::{Path, PathBuf}; @@ -162,14 +166,14 @@ mod tests { let path = Path::new(OsStr::from_bytes(payload)); // do a roundtrip into Pythonland and back and compare - let py_str: PyObject = path.into_py(py); - let path_2: PathBuf = py_str.extract(py).unwrap(); + let py_str = path.into_pyobject(py).unwrap(); + let path_2: PathBuf = py_str.extract().unwrap(); assert_eq!(path, path_2); }); } #[test] - fn test_topyobject_roundtrip() { + fn test_intopyobject_roundtrip() { Python::with_gil(|py| { fn test_roundtrip<'py, T>(py: Python<'py>, obj: T) where @@ -189,24 +193,4 @@ mod tests { test_roundtrip::(py, path.to_path_buf()); }); } - - #[test] - fn test_intopy_roundtrip() { - Python::with_gil(|py| { - fn test_roundtrip + AsRef + Debug + Clone>( - py: Python<'_>, - obj: T, - ) { - let pyobject = obj.clone().into_py(py); - let pystring = pyobject.downcast_bound::(py).unwrap(); - assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy()); - let roundtripped_obj: PathBuf = pystring.extract().unwrap(); - assert_eq!(obj.as_ref(), roundtripped_obj.as_path()); - } - let path = Path::new("Hello\0\nšŸ"); - test_roundtrip::<&Path>(py, path); - test_roundtrip::(py, path.to_path_buf()); - test_roundtrip::<&PathBuf>(py, &path.to_path_buf()); - }) - } } diff --git a/src/conversions/std/set.rs b/src/conversions/std/set.rs index 0bd1b9d7ed6..3f1e6733f1a 100644 --- a/src/conversions/std/set.rs +++ b/src/conversions/std/set.rs @@ -2,8 +2,6 @@ use std::{cmp, collections, hash}; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, instance::Bound, @@ -13,8 +11,10 @@ use crate::{ set::{new_from_iter, try_new_from_iter, PySetMethods}, PyFrozenSet, PySet, }, - FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, + FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; #[allow(deprecated)] impl ToPyObject for collections::HashSet @@ -41,6 +41,7 @@ where } } +#[allow(deprecated)] impl IntoPy for collections::HashSet where K: IntoPy + Eq + hash::Hash, @@ -116,6 +117,7 @@ where } } +#[allow(deprecated)] impl IntoPy for collections::BTreeSet where K: IntoPy + cmp::Ord, @@ -190,7 +192,7 @@ where #[cfg(test)] mod tests { use crate::types::{any::PyAnyMethods, PyFrozenSet, PySet}; - use crate::{IntoPy, IntoPyObject, PyObject, Python}; + use crate::{IntoPyObject, PyObject, Python}; use std::collections::{BTreeSet, HashSet}; #[test] @@ -220,7 +222,9 @@ mod tests { } #[test] + #[allow(deprecated)] fn test_set_into_py() { + use crate::IntoPy; Python::with_gil(|py| { let bt: BTreeSet = [1, 2, 3, 4, 5].iter().cloned().collect(); let hs: HashSet = [1, 2, 3, 4, 5].iter().cloned().collect(); diff --git a/src/conversions/std/slice.rs b/src/conversions/std/slice.rs index ea33cc8461b..798e5b54c45 100644 --- a/src/conversions/std/slice.rs +++ b/src/conversions/std/slice.rs @@ -2,14 +2,15 @@ use std::borrow::Cow; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, types::{PyByteArray, PyByteArrayMethods, PyBytes}, - Bound, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python, + Bound, Py, PyAny, PyErr, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; +#[allow(deprecated)] impl IntoPy for &[u8] { fn into_py(self, py: Python<'_>) -> PyObject { PyBytes::new(py, self).unbind().into() @@ -82,6 +83,7 @@ impl ToPyObject for Cow<'_, [u8]> { } } +#[allow(deprecated)] impl IntoPy> for Cow<'_, [u8]> { fn into_py(self, py: Python<'_>) -> Py { self.into_pyobject(py).unwrap().into_any().unbind() diff --git a/src/conversions/std/string.rs b/src/conversions/std/string.rs index b1eff507b7a..074b5c2ba73 100644 --- a/src/conversions/std/string.rs +++ b/src/conversions/std/string.rs @@ -2,14 +2,14 @@ use std::{borrow::Cow, convert::Infallible}; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, instance::Bound, types::{any::PyAnyMethods, string::PyStringMethods, PyString}, - FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python, + FromPyObject, Py, PyAny, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; /// Converts a Rust `str` to a Python object. /// See `PyString::new` for details on the conversion. @@ -21,6 +21,7 @@ impl ToPyObject for str { } } +#[allow(deprecated)] impl IntoPy for &str { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -28,6 +29,7 @@ impl IntoPy for &str { } } +#[allow(deprecated)] impl IntoPy> for &str { #[inline] fn into_py(self, py: Python<'_>) -> Py { @@ -77,6 +79,7 @@ impl ToPyObject for Cow<'_, str> { } } +#[allow(deprecated)] impl IntoPy for Cow<'_, str> { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -134,6 +137,7 @@ impl ToPyObject for char { } } +#[allow(deprecated)] impl IntoPy for char { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -173,6 +177,7 @@ impl<'py> IntoPyObject<'py> for &char { } } +#[allow(deprecated)] impl IntoPy for String { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -195,6 +200,7 @@ impl<'py> IntoPyObject<'py> for String { } } +#[allow(deprecated)] impl IntoPy for &String { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -276,11 +282,13 @@ impl FromPyObject<'_> for char { #[cfg(test)] mod tests { use crate::types::any::PyAnyMethods; - use crate::{IntoPy, IntoPyObject, PyObject, Python}; + use crate::{IntoPyObject, PyObject, Python}; use std::borrow::Cow; #[test] + #[allow(deprecated)] fn test_cow_into_py() { + use crate::IntoPy; Python::with_gil(|py| { let s = "Hello Python"; let py_string: PyObject = Cow::Borrowed(s).into_py(py); @@ -345,27 +353,30 @@ mod tests { } #[test] - fn test_string_into_py() { + fn test_string_into_pyobject() { Python::with_gil(|py| { let s = "Hello Python"; let s2 = s.to_owned(); let s3 = &s2; assert_eq!( s, - IntoPy::::into_py(s3, py) - .extract::>(py) + s3.into_pyobject(py) + .unwrap() + .extract::>() .unwrap() ); assert_eq!( s, - IntoPy::::into_py(s2, py) - .extract::>(py) + s2.into_pyobject(py) + .unwrap() + .extract::>() .unwrap() ); assert_eq!( s, - IntoPy::::into_py(s, py) - .extract::>(py) + s.into_pyobject(py) + .unwrap() + .extract::>() .unwrap() ); }) diff --git a/src/conversions/std/time.rs b/src/conversions/std/time.rs index c55a22666de..741c28ee905 100755 --- a/src/conversions/std/time.rs +++ b/src/conversions/std/time.rs @@ -8,9 +8,9 @@ use crate::types::PyType; use crate::types::{timezone_utc, PyDateTime, PyDelta, PyDeltaAccess}; #[cfg(Py_LIMITED_API)] use crate::Py; +use crate::{intern, Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{intern, Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python}; +use crate::{IntoPy, ToPyObject}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; const SECONDS_PER_DAY: u64 = 24 * 60 * 60; @@ -60,6 +60,7 @@ impl ToPyObject for Duration { } } +#[allow(deprecated)] impl IntoPy for Duration { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -141,6 +142,7 @@ impl ToPyObject for SystemTime { } } +#[allow(deprecated)] impl IntoPy for SystemTime { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -201,7 +203,6 @@ fn unix_epoch_py(py: Python<'_>) -> PyResult<&PyObject> { mod tests { use super::*; use crate::types::PyDict; - use std::panic; #[test] fn test_duration_frompyobject() { @@ -347,24 +348,26 @@ mod tests { } #[test] - fn test_time_topyobject() { + fn test_time_intopyobject() { Python::with_gil(|py| { - let assert_eq = |l: PyObject, r: Bound<'_, PyAny>| { - assert!(l.bind(py).eq(r).unwrap()); + let assert_eq = |l: Bound<'_, PyAny>, r: Bound<'_, PyAny>| { + assert!(l.eq(r).unwrap()); }; assert_eq( UNIX_EPOCH .checked_add(Duration::new(1580702706, 7123)) .unwrap() - .into_py(py), + .into_pyobject(py) + .unwrap(), new_datetime(py, 2020, 2, 3, 4, 5, 6, 7), ); assert_eq( UNIX_EPOCH .checked_add(Duration::new(253402300799, 999999000)) .unwrap() - .into_py(py), + .into_pyobject(py) + .unwrap(), max_datetime(py), ); }); @@ -403,12 +406,12 @@ mod tests { } #[test] - fn test_time_topyobject_overflow() { + fn test_time_intopyobject_overflow() { let big_system_time = UNIX_EPOCH .checked_add(Duration::new(300000000000, 0)) .unwrap(); Python::with_gil(|py| { - assert!(panic::catch_unwind(|| big_system_time.into_py(py)).is_err()); + assert!(big_system_time.into_pyobject(py).is_err()); }) } diff --git a/src/conversions/std/vec.rs b/src/conversions/std/vec.rs index a667460fd82..b630ca4019a 100644 --- a/src/conversions/std/vec.rs +++ b/src/conversions/std/vec.rs @@ -2,9 +2,9 @@ use crate::conversion::IntoPyObject; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; use crate::types::list::new_from_iter; +use crate::{Bound, PyAny, PyErr, PyObject, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{Bound, IntoPy, PyAny, PyErr, PyObject, Python}; +use crate::{IntoPy, ToPyObject}; #[allow(deprecated)] impl ToPyObject for [T] @@ -28,6 +28,7 @@ where } } +#[allow(deprecated)] impl IntoPy for Vec where T: IntoPy, diff --git a/src/coroutine.rs b/src/coroutine.rs index 82f5460f03e..56ed58f7460 100644 --- a/src/coroutine.rs +++ b/src/coroutine.rs @@ -15,7 +15,7 @@ use crate::{ exceptions::{PyAttributeError, PyRuntimeError, PyStopIteration}, panic::PanicException, types::{string::PyStringMethods, PyIterator, PyString}, - Bound, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, Python, + Bound, BoundObject, IntoPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python, }; pub(crate) mod cancel; @@ -42,24 +42,27 @@ impl Coroutine { /// (should always be `None` anyway). /// /// `Coroutine `throw` drop the wrapped future and reraise the exception passed - pub(crate) fn new( - name: Option>, + pub(crate) fn new<'py, F, T, E>( + name: Option>, qualname_prefix: Option<&'static str>, throw_callback: Option, future: F, ) -> Self where F: Future> + Send + 'static, - T: IntoPy, + T: IntoPyObject<'py>, E: Into, { let wrap = async move { let obj = future.await.map_err(Into::into)?; // SAFETY: GIL is acquired when future is polled (see `Coroutine::poll`) - Ok(obj.into_py(unsafe { Python::assume_gil_acquired() })) + obj.into_pyobject(unsafe { Python::assume_gil_acquired() }) + .map(BoundObject::into_any) + .map(BoundObject::unbind) + .map_err(Into::into) }; Self { - name, + name: name.map(Bound::unbind), qualname_prefix, throw_callback, future: Some(Box::pin(wrap)), @@ -115,7 +118,7 @@ impl Coroutine { } // if waker has been waken during future polling, this is roughly equivalent to // `await asyncio.sleep(0)`, so just yield `None`. - Ok(py.None().into_py(py)) + Ok(py.None()) } } @@ -132,9 +135,11 @@ impl Coroutine { #[getter] fn __qualname__(&self, py: Python<'_>) -> PyResult> { match (&self.name, &self.qualname_prefix) { - (Some(name), Some(prefix)) => Ok(format!("{}.{}", prefix, name.bind(py).to_cow()?) + (Some(name), Some(prefix)) => format!("{}.{}", prefix, name.bind(py).to_cow()?) .as_str() - .into_py(py)), + .into_pyobject(py) + .map(BoundObject::unbind) + .map_err(Into::into), (Some(name), None) => Ok(name.clone_ref(py)), (None, _) => Err(PyAttributeError::new_err("__qualname__")), } diff --git a/src/err/impls.rs b/src/err/impls.rs index 81d0b2ae81b..b84f46d4306 100644 --- a/src/err/impls.rs +++ b/src/err/impls.rs @@ -1,4 +1,5 @@ -use crate::{err::PyErrArguments, exceptions, IntoPy, PyErr, PyObject, Python}; +use crate::IntoPyObject; +use crate::{err::PyErrArguments, exceptions, PyErr, PyObject, Python}; use std::io; /// Convert `PyErr` to `io::Error` @@ -60,7 +61,12 @@ impl From for PyErr { impl PyErrArguments for io::Error { fn arguments(self, py: Python<'_>) -> PyObject { - self.to_string().into_py(py) + //FIXME(icxolu) remove unwrap + self.to_string() + .into_pyobject(py) + .unwrap() + .into_any() + .unbind() } } @@ -86,7 +92,12 @@ macro_rules! impl_to_pyerr { ($err: ty, $pyexc: ty) => { impl PyErrArguments for $err { fn arguments(self, py: Python<'_>) -> PyObject { - self.to_string().into_py(py) + // FIXME(icxolu) remove unwrap + self.to_string() + .into_pyobject(py) + .unwrap() + .into_any() + .unbind() } } diff --git a/src/err/mod.rs b/src/err/mod.rs index d65eac171f6..3d8cedae1d5 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -3,13 +3,13 @@ use crate::panic::PanicException; use crate::type_object::PyTypeInfo; use crate::types::any::PyAnyMethods; use crate::types::{string::PyStringMethods, typeobject::PyTypeMethods, PyTraceback, PyType}; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ exceptions::{self, PyBaseException}, ffi, }; -use crate::{Borrowed, BoundObject, IntoPy, Py, PyAny, PyObject, Python}; +use crate::{Borrowed, BoundObject, Py, PyAny, PyObject, Python}; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use std::borrow::Cow; use std::ffi::{CStr, CString}; @@ -246,7 +246,7 @@ impl PyErr { // is not the case let obj = err.into_inner(); let py = obj.py(); - PyErrState::lazy_arguments(obj.into_py(py), py.None()) + PyErrState::lazy_arguments(obj.unbind(), py.None()) } }; @@ -847,6 +847,7 @@ impl std::fmt::Display for PyErr { impl std::error::Error for PyErr {} +#[allow(deprecated)] impl IntoPy for PyErr { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -862,6 +863,7 @@ impl ToPyObject for PyErr { } } +#[allow(deprecated)] impl IntoPy for &PyErr { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { diff --git a/src/ffi/tests.rs b/src/ffi/tests.rs index cd01eed4b1e..3396e409368 100644 --- a/src/ffi/tests.rs +++ b/src/ffi/tests.rs @@ -6,7 +6,7 @@ use crate::Python; use crate::types::PyString; #[cfg(not(Py_LIMITED_API))] -use crate::{types::PyDict, Bound, IntoPy, Py, PyAny}; +use crate::{types::PyDict, Bound, PyAny}; #[cfg(not(any(Py_3_12, Py_LIMITED_API)))] use libc::wchar_t; @@ -14,8 +14,9 @@ use libc::wchar_t; #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons #[test] fn test_datetime_fromtimestamp() { + use crate::IntoPyObject; Python::with_gil(|py| { - let args: Py = (100,).into_py(py); + let args = (100,).into_pyobject(py).unwrap(); let dt = unsafe { PyDateTime_IMPORT(); Bound::from_owned_ptr(py, PyDateTime_FromTimestamp(args.as_ptr())) @@ -35,8 +36,9 @@ fn test_datetime_fromtimestamp() { #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons #[test] fn test_date_fromtimestamp() { + use crate::IntoPyObject; Python::with_gil(|py| { - let args: Py = (100,).into_py(py); + let args = (100,).into_pyobject(py).unwrap(); let dt = unsafe { PyDateTime_IMPORT(); Bound::from_owned_ptr(py, PyDate_FromTimestamp(args.as_ptr())) diff --git a/src/impl_/coroutine.rs b/src/impl_/coroutine.rs index 1d3119400a0..f893a2c2fe9 100644 --- a/src/impl_/coroutine.rs +++ b/src/impl_/coroutine.rs @@ -9,26 +9,21 @@ use crate::{ pycell::impl_::PyClassBorrowChecker, pyclass::boolean_struct::False, types::{PyAnyMethods, PyString}, - IntoPy, Py, PyAny, PyClass, PyErr, PyObject, PyResult, Python, + IntoPyObject, Py, PyAny, PyClass, PyErr, PyResult, Python, }; -pub fn new_coroutine( - name: &Bound<'_, PyString>, +pub fn new_coroutine<'py, F, T, E>( + name: &Bound<'py, PyString>, qualname_prefix: Option<&'static str>, throw_callback: Option, future: F, ) -> Coroutine where F: Future> + Send + 'static, - T: IntoPy, + T: IntoPyObject<'py>, E: Into, { - Coroutine::new( - Some(name.clone().into()), - qualname_prefix, - throw_callback, - future, - ) + Coroutine::new(Some(name.clone()), qualname_prefix, throw_callback, future) } fn get_ptr(obj: &Py) -> *mut T { diff --git a/src/impl_/pyclass.rs b/src/impl_/pyclass.rs index 5a1fb963271..85cf95a9e11 100644 --- a/src/impl_/pyclass.rs +++ b/src/impl_/pyclass.rs @@ -1,5 +1,3 @@ -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ conversion::IntoPyObject, exceptions::{PyAttributeError, PyNotImplementedError, PyRuntimeError, PyValueError}, @@ -12,8 +10,10 @@ use crate::{ }, pycell::PyBorrowError, types::{any::PyAnyMethods, PyBool}, - Borrowed, BoundObject, IntoPy, Py, PyAny, PyClass, PyErr, PyRef, PyResult, PyTypeInfo, Python, + Borrowed, BoundObject, Py, PyAny, PyClass, PyErr, PyRef, PyResult, PyTypeInfo, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use std::{ borrow::Cow, ffi::{CStr, CString}, @@ -1372,6 +1372,7 @@ where } /// IntoPy + Clone fallback case, which was the only behaviour before PyO3 0.22. +#[allow(deprecated)] impl PyClassGetterGenerator where @@ -1452,6 +1453,7 @@ impl IsToPyObject { probe!(IsIntoPy); +#[allow(deprecated)] impl> IsIntoPy { pub const VALUE: bool = true; } @@ -1556,6 +1558,7 @@ where .into_ptr()) } +#[allow(deprecated)] fn pyo3_get_value< ClassT: PyClass, FieldT: IntoPy> + Clone, diff --git a/src/impl_/wrap.rs b/src/impl_/wrap.rs index c999cf40249..9381828245a 100644 --- a/src/impl_/wrap.rs +++ b/src/impl_/wrap.rs @@ -1,8 +1,9 @@ use std::{convert::Infallible, marker::PhantomData, ops::Deref}; +#[allow(deprecated)] +use crate::IntoPy; use crate::{ - conversion::IntoPyObject, ffi, types::PyNone, Bound, BoundObject, IntoPy, PyObject, PyResult, - Python, + conversion::IntoPyObject, ffi, types::PyNone, Bound, BoundObject, PyObject, PyResult, Python, }; /// Used to wrap values in `Option` for default arguments. @@ -112,6 +113,7 @@ impl<'py, T: IntoPyObject<'py>, E> IntoPyObjectConverter> { } } +#[allow(deprecated)] impl> IntoPyConverter { #[inline] pub fn wrap(&self, obj: T) -> Result { @@ -119,6 +121,7 @@ impl> IntoPyConverter { } } +#[allow(deprecated)] impl, E> IntoPyConverter> { #[inline] pub fn wrap(&self, obj: Result) -> Result { diff --git a/src/instance.rs b/src/instance.rs index 6b191abd5a2..99643e12eb1 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -6,13 +6,13 @@ use crate::pycell::{PyBorrowError, PyBorrowMutError}; use crate::pyclass::boolean_struct::{False, True}; use crate::types::{any::PyAnyMethods, string::PyStringMethods, typeobject::PyTypeMethods}; use crate::types::{DerefToPyAny, PyDict, PyString, PyTuple}; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ - ffi, AsPyPointer, DowncastError, FromPyObject, IntoPy, PyAny, PyClass, PyClassInitializer, - PyRef, PyRefMut, PyTypeInfo, Python, + ffi, AsPyPointer, DowncastError, FromPyObject, PyAny, PyClass, PyClassInitializer, PyRef, + PyRefMut, PyTypeInfo, Python, }; use crate::{gil, PyTypeCheck}; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use std::marker::PhantomData; use std::mem::ManuallyDrop; use std::ops::Deref; @@ -830,6 +830,7 @@ impl ToPyObject for Borrowed<'_, '_, T> { } } +#[allow(deprecated)] impl IntoPy for Borrowed<'_, '_, T> { /// Converts `Py` instance -> PyObject. #[inline] @@ -1465,7 +1466,7 @@ impl Py { /// # Example: `intern!`ing the attribute name /// /// ``` - /// # use pyo3::{intern, pyfunction, types::PyModule, IntoPy, PyObject, Python, PyResult}; + /// # use pyo3::{intern, pyfunction, types::PyModule, IntoPyObject, PyObject, Python, PyResult}; /// # /// #[pyfunction] /// fn set_answer(ob: PyObject, py: Python<'_>) -> PyResult<()> { @@ -1473,7 +1474,7 @@ impl Py { /// } /// # /// # Python::with_gil(|py| { - /// # let ob = PyModule::new(py, "empty").unwrap().into_py(py); + /// # let ob = PyModule::new(py, "empty").unwrap().into_pyobject(py).unwrap().into_any().unbind(); /// # set_answer(ob, py).unwrap(); /// # }); /// ``` @@ -1497,11 +1498,19 @@ impl Py { where A: IntoPyObject<'py, Target = PyTuple>, { - self.bind(py).as_any().call(args, kwargs).map(Bound::unbind) + self.bind(py) + .as_any() + .call( + // FIXME(icxolu): remove explicit args conversion + args.into_pyobject(py).map_err(Into::into)?.into_bound(), + kwargs, + ) + .map(Bound::unbind) } /// Deprecated name for [`Py::call`]. #[deprecated(since = "0.23.0", note = "renamed to `Py::call`")] + #[allow(deprecated)] #[inline] pub fn call_bound( &self, @@ -1519,7 +1528,11 @@ impl Py { where N: IntoPyObject<'py, Target = PyTuple>, { - self.bind(py).as_any().call1(args).map(Bound::unbind) + self.bind(py) + .as_any() + // FIXME(icxolu): remove explicit args conversion + .call1(args.into_pyobject(py).map_err(Into::into)?.into_bound()) + .map(Bound::unbind) } /// Calls the object without arguments. @@ -1548,12 +1561,18 @@ impl Py { { self.bind(py) .as_any() - .call_method(name, args, kwargs) + .call_method( + name, + // FIXME(icxolu): remove explicit args conversion + args.into_pyobject(py).map_err(Into::into)?.into_bound(), + kwargs, + ) .map(Bound::unbind) } /// Deprecated name for [`Py::call_method`]. #[deprecated(since = "0.23.0", note = "renamed to `Py::call_method`")] + #[allow(deprecated)] #[inline] pub fn call_method_bound( &self, @@ -1582,7 +1601,11 @@ impl Py { { self.bind(py) .as_any() - .call_method1(name, args) + .call_method1( + name, + // FIXME(icxolu): remove explicit args conversion + args.into_pyobject(py).map_err(Into::into)?.into_bound(), + ) .map(Bound::unbind) } @@ -1720,6 +1743,7 @@ impl ToPyObject for Py { } } +#[allow(deprecated)] impl IntoPy for Py { /// Converts a `Py` instance to `PyObject`. /// Consumes `self` without calling `Py_DECREF()`. @@ -1729,6 +1753,7 @@ impl IntoPy for Py { } } +#[allow(deprecated)] impl IntoPy for &'_ Py { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -1745,6 +1770,7 @@ impl ToPyObject for Bound<'_, T> { } } +#[allow(deprecated)] impl IntoPy for Bound<'_, T> { /// Converts a `Bound` instance to `PyObject`. #[inline] @@ -1753,6 +1779,7 @@ impl IntoPy for Bound<'_, T> { } } +#[allow(deprecated)] impl IntoPy for &Bound<'_, T> { /// Converts `&Bound` instance -> PyObject, increasing the reference count. #[inline] @@ -1785,8 +1812,7 @@ where { #[inline] fn from(other: Bound<'_, T>) -> Self { - let py = other.py(); - other.into_py(py) + other.into_any().unbind() } } @@ -1928,7 +1954,7 @@ impl PyObject { /// } /// /// Python::with_gil(|py| { - /// let class: PyObject = Py::new(py, Class { i: 0 }).unwrap().into_py(py); + /// let class: PyObject = Py::new(py, Class { i: 0 })?.into_any(); /// /// let class_bound = class.downcast_bound::(py)?; /// diff --git a/src/lib.rs b/src/lib.rs index d6bf1b374c3..b4fcf918fae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -323,9 +323,9 @@ #![doc = concat!("[Features chapter of the guide]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/features.html#features-reference \"Features Reference - PyO3 user guide\"")] //! [`Ungil`]: crate::marker::Ungil pub use crate::class::*; +pub use crate::conversion::{AsPyPointer, FromPyObject, IntoPyObject}; #[allow(deprecated)] -pub use crate::conversion::ToPyObject; -pub use crate::conversion::{AsPyPointer, FromPyObject, IntoPy, IntoPyObject}; +pub use crate::conversion::{IntoPy, ToPyObject}; pub use crate::err::{DowncastError, DowncastIntoError, PyErr, PyErrArguments, PyResult, ToPyErr}; #[cfg(not(any(PyPy, GraalPy)))] pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter}; diff --git a/src/marker.rs b/src/marker.rs index 0fedf3f8c81..1a4c0482569 100644 --- a/src/marker.rs +++ b/src/marker.rs @@ -129,7 +129,9 @@ use crate::types::{ PyAny, PyDict, PyEllipsis, PyModule, PyNone, PyNotImplemented, PyString, PyType, }; use crate::version::PythonVersionInfo; -use crate::{ffi, Bound, IntoPy, Py, PyObject, PyTypeInfo}; +#[allow(deprecated)] +use crate::IntoPy; +use crate::{ffi, Bound, Py, PyObject, PyTypeInfo}; use std::ffi::{CStr, CString}; use std::marker::PhantomData; use std::os::raw::c_int; @@ -715,6 +717,7 @@ impl<'py> Python<'py> { /// Deprecated name for [`Python::import`]. #[deprecated(since = "0.23.0", note = "renamed to `Python::import`")] + #[allow(deprecated)] #[track_caller] #[inline] pub fn import_bound(self, name: N) -> PyResult> @@ -728,21 +731,21 @@ impl<'py> Python<'py> { #[allow(non_snake_case)] // the Python keyword starts with uppercase #[inline] pub fn None(self) -> PyObject { - PyNone::get(self).into_py(self) + PyNone::get(self).to_owned().into_any().unbind() } /// Gets the Python builtin value `Ellipsis`, or `...`. #[allow(non_snake_case)] // the Python keyword starts with uppercase #[inline] pub fn Ellipsis(self) -> PyObject { - PyEllipsis::get(self).into_py(self) + PyEllipsis::get(self).to_owned().into_any().unbind() } /// Gets the Python builtin value `NotImplemented`. #[allow(non_snake_case)] // the Python keyword starts with uppercase #[inline] pub fn NotImplemented(self) -> PyObject { - PyNotImplemented::get(self).into_py(self) + PyNotImplemented::get(self).to_owned().into_any().unbind() } /// Gets the running Python interpreter version as a string. diff --git a/src/prelude.rs b/src/prelude.rs index cc44a199611..b465167108a 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -8,9 +8,9 @@ //! use pyo3::prelude::*; //! ``` +pub use crate::conversion::{FromPyObject, IntoPyObject}; #[allow(deprecated)] -pub use crate::conversion::ToPyObject; -pub use crate::conversion::{FromPyObject, IntoPy, IntoPyObject}; +pub use crate::conversion::{IntoPy, ToPyObject}; pub use crate::err::{PyErr, PyResult}; pub use crate::instance::{Borrowed, Bound, Py, PyObject}; pub use crate::marker::Python; diff --git a/src/pybacked.rs b/src/pybacked.rs index 5a45c8f1036..173d8e0e9e4 100644 --- a/src/pybacked.rs +++ b/src/pybacked.rs @@ -2,15 +2,15 @@ use std::{convert::Infallible, ops::Deref, ptr::NonNull, sync::Arc}; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ types::{ any::PyAnyMethods, bytearray::PyByteArrayMethods, bytes::PyBytesMethods, string::PyStringMethods, PyByteArray, PyBytes, PyString, }, - Bound, DowncastError, FromPyObject, IntoPy, IntoPyObject, Py, PyAny, PyErr, PyResult, Python, + Bound, DowncastError, FromPyObject, IntoPyObject, Py, PyAny, PyErr, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; /// A wrapper around `str` where the storage is owned by a Python `bytes` or `str` object. /// @@ -99,6 +99,7 @@ impl ToPyObject for PyBackedStr { } } +#[allow(deprecated)] impl IntoPy> for PyBackedStr { #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] fn into_py(self, _py: Python<'_>) -> Py { @@ -248,6 +249,7 @@ impl ToPyObject for PyBackedBytes { } } +#[allow(deprecated)] impl IntoPy> for PyBackedBytes { fn into_py(self, py: Python<'_>) -> Py { match self.storage { @@ -403,6 +405,7 @@ mod test { } #[test] + #[allow(deprecated)] fn py_backed_str_into_py() { Python::with_gil(|py| { let orig_str = PyString::new(py, "hello"); @@ -451,6 +454,7 @@ mod test { } #[test] + #[allow(deprecated)] fn py_backed_bytes_into_pyobject() { Python::with_gil(|py| { let orig_bytes = PyBytes::new(py, b"abcde"); @@ -464,6 +468,7 @@ mod test { } #[test] + #[allow(deprecated)] fn rust_backed_bytes_into_pyobject() { Python::with_gil(|py| { let orig_bytes = PyByteArray::new(py, b"abcde"); diff --git a/src/pycell.rs b/src/pycell.rs index 1451e5499ce..51c9f201068 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -199,7 +199,9 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::internal_tricks::{ptr_from_mut, ptr_from_ref}; use crate::pyclass::{boolean_struct::False, PyClass}; use crate::types::any::PyAnyMethods; -use crate::{ffi, Borrowed, Bound, IntoPy, PyErr, PyObject, Python}; +#[allow(deprecated)] +use crate::IntoPy; +use crate::{ffi, Borrowed, Bound, PyErr, PyObject, Python}; use std::convert::Infallible; use std::fmt; use std::mem::ManuallyDrop; @@ -447,12 +449,14 @@ impl Drop for PyRef<'_, T> { } } +#[allow(deprecated)] impl IntoPy for PyRef<'_, T> { fn into_py(self, py: Python<'_>) -> PyObject { unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } } } +#[allow(deprecated)] impl IntoPy for &'_ PyRef<'_, T> { fn into_py(self, py: Python<'_>) -> PyObject { unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } @@ -636,12 +640,14 @@ impl> Drop for PyRefMut<'_, T> { } } +#[allow(deprecated)] impl> IntoPy for PyRefMut<'_, T> { fn into_py(self, py: Python<'_>) -> PyObject { unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } } } +#[allow(deprecated)] impl> IntoPy for &'_ PyRefMut<'_, T> { fn into_py(self, py: Python<'_>) -> PyObject { self.inner.clone().into_py(py) diff --git a/src/types/any.rs b/src/types/any.rs index 62f7cdfc27e..e620cf6d137 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -227,12 +227,11 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed { /// ```rust /// use pyo3::class::basic::CompareOp; /// use pyo3::prelude::*; - /// use pyo3::types::PyInt; /// /// # fn main() -> PyResult<()> { /// Python::with_gil(|py| -> PyResult<()> { - /// let a: Bound<'_, PyInt> = 0_u8.into_py(py).into_bound(py).downcast_into()?; - /// let b: Bound<'_, PyInt> = 42_u8.into_py(py).into_bound(py).downcast_into()?; + /// let a = 0_u8.into_pyobject(py)?; + /// let b = 42_u8.into_pyobject(py)?; /// assert!(a.rich_compare(b, CompareOp::Le)?.is_truthy()?); /// Ok(()) /// })?; diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index d54bddc8848..53043fa798c 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -1,12 +1,11 @@ #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, - types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyObject, PyResult, - Python, + types::typeobject::PyTypeMethods, Borrowed, FromPyObject, PyAny, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use super::any::PyAnyMethods; use crate::conversion::IntoPyObject; @@ -155,6 +154,7 @@ impl ToPyObject for bool { } } +#[allow(deprecated)] impl IntoPy for bool { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { diff --git a/src/types/datetime.rs b/src/types/datetime.rs index ac956c250d3..8ab512ac466 100644 --- a/src/types/datetime.rs +++ b/src/types/datetime.rs @@ -25,7 +25,7 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::py_result_ext::PyResultExt; use crate::types::any::PyAnyMethods; use crate::types::PyTuple; -use crate::{Bound, IntoPy, Py, PyAny, PyErr, Python}; +use crate::{Bound, IntoPyObject, PyAny, PyErr, Python}; use std::os::raw::c_int; #[cfg(feature = "chrono")] use std::ptr; @@ -409,7 +409,7 @@ impl PyDateTime { timestamp: f64, tzinfo: Option<&Bound<'py, PyTzInfo>>, ) -> PyResult> { - let args = IntoPy::>::into_py((timestamp, tzinfo), py).into_bound(py); + let args = (timestamp, tzinfo).into_pyobject(py)?; // safety ensure API is loaded let _api = ensure_datetime_api(py)?; diff --git a/src/types/float.rs b/src/types/float.rs index 8438629e577..3c2d6643d18 100644 --- a/src/types/float.rs +++ b/src/types/float.rs @@ -2,12 +2,12 @@ use super::any::PyAnyMethods; use crate::conversion::IntoPyObject; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ - ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, Borrowed, FromPyObject, IntoPy, PyAny, PyErr, - PyObject, PyResult, Python, + ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, Borrowed, FromPyObject, PyAny, PyErr, PyObject, + PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; use std::convert::Infallible; use std::os::raw::c_double; @@ -86,6 +86,7 @@ impl ToPyObject for f64 { } } +#[allow(deprecated)] impl IntoPy for f64 { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -163,6 +164,7 @@ impl ToPyObject for f32 { } } +#[allow(deprecated)] impl IntoPy for f32 { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { diff --git a/src/types/function.rs b/src/types/function.rs index f443403aa67..039e2774546 100644 --- a/src/types/function.rs +++ b/src/types/function.rs @@ -7,7 +7,7 @@ use crate::{ impl_::pymethods::{self, PyMethodDef}, types::{PyCapsule, PyDict, PyModule, PyString, PyTuple}, }; -use crate::{Bound, IntoPy, Py, PyAny, PyResult, Python}; +use crate::{Bound, Py, PyAny, PyResult, Python}; use std::cell::UnsafeCell; use std::ffi::CStr; @@ -155,7 +155,7 @@ impl PyCFunction { ) -> PyResult> { let (mod_ptr, module_name): (_, Option>) = if let Some(m) = module { let mod_ptr = m.as_ptr(); - (mod_ptr, Some(m.name()?.into_py(py))) + (mod_ptr, Some(m.name()?.unbind())) } else { (std::ptr::null_mut(), None) }; diff --git a/src/types/module.rs b/src/types/module.rs index 3822ed86714..f3490385721 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -97,6 +97,7 @@ impl PyModule { /// Deprecated name for [`PyModule::import`]. #[deprecated(since = "0.23.0", note = "renamed to `PyModule::import`")] + #[allow(deprecated)] #[inline] pub fn import_bound(py: Python<'_>, name: N) -> PyResult> where diff --git a/src/types/none.rs b/src/types/none.rs index 9a0c9b11f45..1ec12d3f5b0 100644 --- a/src/types/none.rs +++ b/src/types/none.rs @@ -1,9 +1,7 @@ use crate::ffi_ptr_ext::FfiPtrExt; +use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyObject, PyTypeInfo, Python}; #[allow(deprecated)] -use crate::ToPyObject; -use crate::{ - ffi, types::any::PyAnyMethods, Borrowed, Bound, IntoPy, PyAny, PyObject, PyTypeInfo, Python, -}; +use crate::{IntoPy, ToPyObject}; /// Represents the Python `None` object. /// @@ -58,6 +56,7 @@ impl ToPyObject for () { } } +#[allow(deprecated)] impl IntoPy for () { #[inline] fn into_py(self, py: Python<'_>) -> PyObject { @@ -69,7 +68,8 @@ impl IntoPy for () { mod tests { use crate::types::any::PyAnyMethods; use crate::types::{PyDict, PyNone}; - use crate::{IntoPy, PyObject, PyTypeInfo, Python}; + use crate::{PyObject, PyTypeInfo, Python}; + #[test] fn test_none_is_itself() { Python::with_gil(|py| { @@ -102,7 +102,9 @@ mod tests { } #[test] + #[allow(deprecated)] fn test_unit_into_py_is_none() { + use crate::IntoPy; Python::with_gil(|py| { let obj: PyObject = ().into_py(py); assert!(obj.downcast_bound::(py).is_ok()); diff --git a/src/types/num.rs b/src/types/num.rs index f33d85f4a1c..0e377f66d48 100644 --- a/src/types/num.rs +++ b/src/types/num.rs @@ -60,8 +60,7 @@ int_compare!(usize); #[cfg(test)] mod tests { - use super::PyInt; - use crate::{types::PyAnyMethods, IntoPy, Python}; + use crate::{IntoPyObject, Python}; #[test] fn test_partial_eq() { @@ -78,7 +77,7 @@ mod tests { let v_u128 = 123u128; let v_isize = 123isize; let v_usize = 123usize; - let obj = 123_i64.into_py(py).downcast_bound(py).unwrap().clone(); + let obj = 123_i64.into_pyobject(py).unwrap(); assert_eq!(v_i8, obj); assert_eq!(obj, v_i8); @@ -116,11 +115,7 @@ mod tests { assert_eq!(obj, v_usize); let big_num = (u8::MAX as u16) + 1; - let big_obj = big_num - .into_py(py) - .into_bound(py) - .downcast_into::() - .unwrap(); + let big_obj = big_num.into_pyobject(py).unwrap(); for x in 0u8..=u8::MAX { assert_ne!(x, big_obj); diff --git a/src/types/string.rs b/src/types/string.rs index 1bcd025d1ce..65a9e85fa3e 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -6,7 +6,9 @@ use crate::py_result_ext::PyResultExt; use crate::types::any::PyAnyMethods; use crate::types::bytes::PyBytesMethods; use crate::types::PyBytes; -use crate::{ffi, Bound, IntoPy, Py, PyAny, PyResult, Python}; +#[allow(deprecated)] +use crate::IntoPy; +use crate::{ffi, Bound, Py, PyAny, PyResult, Python}; use std::borrow::Cow; use std::str; @@ -444,18 +446,21 @@ impl Py { } } +#[allow(deprecated)] impl IntoPy> for Bound<'_, PyString> { fn into_py(self, _py: Python<'_>) -> Py { self.unbind() } } +#[allow(deprecated)] impl IntoPy> for &Bound<'_, PyString> { fn into_py(self, _py: Python<'_>) -> Py { self.clone().unbind() } } +#[allow(deprecated)] impl IntoPy> for &'_ Py { fn into_py(self, py: Python<'_>) -> Py { self.clone_ref(py) @@ -805,7 +810,7 @@ mod tests { fn test_py_to_str_utf8() { Python::with_gil(|py| { let s = "ascii šŸˆ"; - let py_string: Py = PyString::new(py, s).into_py(py); + let py_string = PyString::new(py, s).unbind(); #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] assert_eq!(s, py_string.to_str(py).unwrap()); diff --git a/src/types/traceback.rs b/src/types/traceback.rs index 6c9909e7113..885c0f67031 100644 --- a/src/types/traceback.rs +++ b/src/types/traceback.rs @@ -80,10 +80,11 @@ impl<'py> PyTracebackMethods<'py> for Bound<'py, PyTraceback> { #[cfg(test)] mod tests { + use crate::IntoPyObject; use crate::{ ffi, types::{any::PyAnyMethods, dict::PyDictMethods, traceback::PyTracebackMethods, PyDict}, - IntoPy, PyErr, Python, + PyErr, Python, }; #[test] @@ -143,7 +144,7 @@ def f(): let f = locals.get_item("f").unwrap().unwrap(); let err = f.call0().unwrap_err(); let traceback = err.traceback(py).unwrap(); - let err_object = err.clone_ref(py).into_py(py).into_bound(py); + let err_object = err.clone_ref(py).into_pyobject(py).unwrap(); assert!(err_object.getattr("__traceback__").unwrap().is(&traceback)); }) diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 1b6a95abb14..3a1f92815c2 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -8,12 +8,11 @@ use crate::inspect::types::TypeInfo; use crate::instance::Borrowed; use crate::internal_tricks::get_ssize_index; use crate::types::{any::PyAnyMethods, sequence::PySequenceMethods, PyList, PySequence}; -#[allow(deprecated)] -use crate::ToPyObject; use crate::{ - exceptions, Bound, BoundObject, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, - Python, + exceptions, Bound, BoundObject, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python, }; +#[allow(deprecated)] +use crate::{IntoPy, ToPyObject}; #[inline] #[track_caller] @@ -496,12 +495,14 @@ impl ExactSizeIterator for BorrowedTupleIterator<'_, '_> { impl FusedIterator for BorrowedTupleIterator<'_, '_> {} +#[allow(deprecated)] impl IntoPy> for Bound<'_, PyTuple> { fn into_py(self, _: Python<'_>) -> Py { self.unbind() } } +#[allow(deprecated)] impl IntoPy> for &'_ Bound<'_, PyTuple> { fn into_py(self, _: Python<'_>) -> Py { self.clone().unbind() @@ -525,6 +526,8 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+ array_into_tuple(py, [$(self.$n.to_object(py)),+]).into() } } + + #[allow(deprecated)] impl <$($T: IntoPy),+> IntoPy for ($($T,)+) { fn into_py(self, py: Python<'_>) -> PyObject { array_into_tuple(py, [$(self.$n.into_py(py)),+]).into() @@ -568,6 +571,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+ } } + #[allow(deprecated)] impl <$($T: IntoPy),+> IntoPy> for ($($T,)+) { fn into_py(self, py: Python<'_>) -> Py { array_into_tuple(py, [$(self.$n.into_py(py)),+]) diff --git a/tests/test_arithmetics.rs b/tests/test_arithmetics.rs index e7914a0bb95..21e32d4c13c 100644 --- a/tests/test_arithmetics.rs +++ b/tests/test_arithmetics.rs @@ -1,8 +1,8 @@ #![cfg(feature = "macros")] use pyo3::class::basic::CompareOp; -use pyo3::prelude::*; use pyo3::py_run; +use pyo3::{prelude::*, BoundObject}; #[path = "../src/tests/common.rs"] mod common; @@ -527,11 +527,19 @@ impl RichComparisons2 { "RC2" } - fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyObject { + fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyResult { match op { - CompareOp::Eq => true.into_py(other.py()), - CompareOp::Ne => false.into_py(other.py()), - _ => other.py().NotImplemented(), + CompareOp::Eq => true + .into_pyobject(other.py()) + .map_err(Into::into) + .map(BoundObject::into_any) + .map(BoundObject::unbind), + CompareOp::Ne => false + .into_pyobject(other.py()) + .map_err(Into::into) + .map(BoundObject::into_any) + .map(BoundObject::unbind), + _ => Ok(other.py().NotImplemented()), } } } diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index 4d396f9be68..1f15e34b384 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -71,13 +71,14 @@ fn test_buffer_referenced() { let buf = { let input = vec![b' ', b'2', b'3']; Python::with_gil(|py| { - let instance: PyObject = TestBufferClass { + let instance = TestBufferClass { vec: input.clone(), drop_called: drop_called.clone(), } - .into_py(py); + .into_pyobject(py) + .unwrap(); - let buf = PyBuffer::::get(instance.bind(py)).unwrap(); + let buf = PyBuffer::::get(&instance).unwrap(); assert_eq!(buf.to_vec(py).unwrap(), input); drop(instance); buf diff --git a/tests/test_class_basics.rs b/tests/test_class_basics.rs index a48354c47c8..4a687a89eea 100644 --- a/tests/test_class_basics.rs +++ b/tests/test_class_basics.rs @@ -247,7 +247,7 @@ fn class_with_hash() { let env = [ ("obj", Py::new(py, class).unwrap().into_any()), - ("hsh", hash.into_py(py)), + ("hsh", hash.into_pyobject(py).unwrap().into_any().unbind()), ] .into_py_dict(py) .unwrap(); diff --git a/tests/test_class_conversion.rs b/tests/test_class_conversion.rs index 671dcd126b2..a1e2188e83a 100644 --- a/tests/test_class_conversion.rs +++ b/tests/test_class_conversion.rs @@ -112,11 +112,11 @@ fn test_polymorphic_container_does_not_accept_other_types() { ) .unwrap(); - let setattr = |value: PyObject| p.bind(py).setattr("inner", value); + let setattr = |value: Bound<'_, PyAny>| p.bind(py).setattr("inner", value); - assert!(setattr(1i32.into_py(py)).is_err()); - assert!(setattr(py.None()).is_err()); - assert!(setattr((1i32, 2i32).into_py(py)).is_err()); + assert!(setattr(1i32.into_pyobject(py).unwrap().into_any()).is_err()); + assert!(setattr(py.None().into_bound(py)).is_err()); + assert!(setattr((1i32, 2i32).into_pyobject(py).unwrap().into_any()).is_err()); }); } diff --git a/tests/test_enum.rs b/tests/test_enum.rs index 5e994548edd..9cb9d5fae65 100644 --- a/tests/test_enum.rs +++ b/tests/test_enum.rs @@ -257,7 +257,7 @@ fn test_simple_enum_with_hash() { let env = [ ("obj", Py::new(py, class).unwrap().into_any()), - ("hsh", hash.into_py(py)), + ("hsh", hash.into_pyobject(py).unwrap().into_any().unbind()), ] .into_py_dict(py) .unwrap(); @@ -289,7 +289,7 @@ fn test_complex_enum_with_hash() { let env = [ ("obj", Py::new(py, class).unwrap().into_any()), - ("hsh", hash.into_py(py)), + ("hsh", hash.into_pyobject(py).unwrap().into_any().unbind()), ] .into_py_dict(py) .unwrap(); diff --git a/tests/test_frompyobject.rs b/tests/test_frompyobject.rs index 6093b774733..344a47acf72 100644 --- a/tests/test_frompyobject.rs +++ b/tests/test_frompyobject.rs @@ -76,13 +76,13 @@ pub struct B { #[test] fn test_transparent_named_field_struct() { Python::with_gil(|py| { - let test: PyObject = "test".into_py(py); + let test = "test".into_pyobject(py).unwrap(); let b = test - .extract::(py) + .extract::() .expect("Failed to extract B from String"); assert_eq!(b.test, "test"); - let test: PyObject = 1.into_py(py); - let b = test.extract::(py); + let test = 1i32.into_pyobject(py).unwrap(); + let b = test.extract::(); assert!(b.is_err()); }); } @@ -96,14 +96,14 @@ pub struct D { #[test] fn test_generic_transparent_named_field_struct() { Python::with_gil(|py| { - let test: PyObject = "test".into_py(py); + let test = "test".into_pyobject(py).unwrap(); let d = test - .extract::>(py) + .extract::>() .expect("Failed to extract D from String"); assert_eq!(d.test, "test"); - let test = 1usize.into_py(py); + let test = 1usize.into_pyobject(py).unwrap(); let d = test - .extract::>(py) + .extract::>() .expect("Failed to extract D from String"); assert_eq!(d.test, 1); }); @@ -146,14 +146,15 @@ fn test_generic_named_fields_struct() { test: "test".into(), test2: 2, } - .into_py(py); + .into_pyobject(py) + .unwrap(); let e = pye - .extract::>(py) + .extract::>() .expect("Failed to extract E from PyE"); assert_eq!(e.test, "test"); assert_eq!(e.test2, 2); - let e = pye.extract::>(py); + let e = pye.extract::>(); assert!(e.is_err()); }); } @@ -171,8 +172,9 @@ fn test_named_field_with_ext_fn() { test: "foo".into(), test2: 0, } - .into_py(py); - let c = pyc.extract::(py).expect("Failed to extract C from PyE"); + .into_pyobject(py) + .unwrap(); + let c = pyc.extract::().expect("Failed to extract C from PyE"); assert_eq!(c.test, "foo"); }); } @@ -215,12 +217,12 @@ pub struct TransparentTuple(String); #[test] fn test_transparent_tuple_struct() { Python::with_gil(|py| { - let tup: PyObject = 1.into_py(py); - let tup = tup.extract::(py); + let tup = 1i32.into_pyobject(py).unwrap(); + let tup = tup.extract::(); assert!(tup.is_err()); - let test: PyObject = "test".into_py(py); + let test = "test".into_pyobject(py).unwrap(); let tup = test - .extract::(py) + .extract::() .expect("Failed to extract TransparentTuple from PyTuple"); assert_eq!(tup.0, "test"); }); @@ -251,9 +253,10 @@ fn test_struct_nested_type_errors() { test2: 0, }, } - .into_py(py); + .into_pyobject(py) + .unwrap(); - let test = pybaz.extract::>(py); + let test = pybaz.extract::>(); assert!(test.is_err()); assert_eq!( extract_traceback(py,test.unwrap_err()), @@ -273,9 +276,10 @@ fn test_struct_nested_type_errors_with_generics() { test2: 0, }, } - .into_py(py); + .into_pyobject(py) + .unwrap(); - let test = pybaz.extract::>(py); + let test = pybaz.extract::>(); assert!(test.is_err()); assert_eq!( extract_traceback(py, test.unwrap_err()), @@ -288,8 +292,8 @@ fn test_struct_nested_type_errors_with_generics() { #[test] fn test_transparent_struct_error_message() { Python::with_gil(|py| { - let tup: PyObject = 1.into_py(py); - let tup = tup.extract::(py); + let tup = 1i32.into_pyobject(py).unwrap(); + let tup = tup.extract::(); assert!(tup.is_err()); assert_eq!( extract_traceback(py,tup.unwrap_err()), @@ -302,8 +306,8 @@ fn test_transparent_struct_error_message() { #[test] fn test_tuple_struct_error_message() { Python::with_gil(|py| { - let tup: PyObject = (1, "test").into_py(py); - let tup = tup.extract::(py); + let tup = (1, "test").into_pyobject(py).unwrap(); + let tup = tup.extract::(); assert!(tup.is_err()); assert_eq!( extract_traceback(py, tup.unwrap_err()), @@ -316,8 +320,8 @@ fn test_tuple_struct_error_message() { #[test] fn test_transparent_tuple_error_message() { Python::with_gil(|py| { - let tup: PyObject = 1.into_py(py); - let tup = tup.extract::(py); + let tup = 1i32.into_pyobject(py).unwrap(); + let tup = tup.extract::(); assert!(tup.is_err()); assert_eq!( extract_traceback(py, tup.unwrap_err()), @@ -362,7 +366,14 @@ pub struct PyBool { #[test] fn test_enum() { Python::with_gil(|py| { - let tup = PyTuple::new(py, &[1i32.into_py(py), "test".into_py(py)]).unwrap(); + let tup = PyTuple::new( + py, + &[ + 1i32.into_pyobject(py).unwrap().into_any(), + "test".into_pyobject(py).unwrap().into_any(), + ], + ) + .unwrap(); let f = tup .extract::>() .expect("Failed to extract Foo from tuple"); @@ -378,18 +389,19 @@ fn test_enum() { test: "foo".into(), test2: 0, } - .into_py(py); + .into_pyobject(py) + .unwrap(); let f = pye - .extract::>(py) + .extract::>() .expect("Failed to extract Foo from PyE"); match f { Foo::StructVar { test } => assert_eq!(test.to_string_lossy(), "foo"), _ => panic!("Expected extracting Foo::StructVar, got {:?}", f), } - let int: PyObject = 1.into_py(py); + let int = 1i32.into_pyobject(py).unwrap(); let f = int - .extract::>(py) + .extract::>() .expect("Failed to extract Foo from int"); match f { Foo::TransparentTuple(test) => assert_eq!(test, 1), @@ -404,9 +416,9 @@ fn test_enum() { _ => panic!("Expected extracting Foo::TransparentStructVar, got {:?}", f), } - let pybool = PyBool { bla: true }.into_py(py); + let pybool = PyBool { bla: true }.into_pyobject(py).unwrap(); let f = pybool - .extract::>(py) + .extract::>() .expect("Failed to extract Foo from PyBool"); match f { Foo::StructVarGetAttrArg { a } => assert!(a), diff --git a/tests/test_getter_setter.rs b/tests/test_getter_setter.rs index 144e3f2b7eb..064511772da 100644 --- a/tests/test_getter_setter.rs +++ b/tests/test_getter_setter.rs @@ -4,6 +4,7 @@ use std::cell::Cell; use pyo3::prelude::*; use pyo3::py_run; +use pyo3::types::PyString; use pyo3::types::{IntoPyDict, PyList}; #[path = "../src/tests/common.rs"] @@ -266,14 +267,14 @@ fn frozen_py_field_get() { #[pyclass(frozen)] struct FrozenPyField { #[pyo3(get)] - value: Py, + value: Py, } Python::with_gil(|py| { let inst = Py::new( py, FrozenPyField { - value: "value".into_py(py), + value: "value".into_pyobject(py).unwrap().unbind(), }, ) .unwrap(); diff --git a/tests/test_mapping.rs b/tests/test_mapping.rs index ecb944e983e..ba65c647222 100644 --- a/tests/test_mapping.rs +++ b/tests/test_mapping.rs @@ -61,11 +61,16 @@ impl Mapping { } #[pyo3(signature=(key, default=None))] - fn get(&self, py: Python<'_>, key: &str, default: Option) -> Option { - self.index - .get(key) - .map(|value| value.into_py(py)) - .or(default) + fn get( + &self, + py: Python<'_>, + key: &str, + default: Option, + ) -> PyResult> { + match self.index.get(key) { + Some(value) => Ok(Some(value.into_pyobject(py)?.into_any().unbind())), + None => Ok(default), + } } } diff --git a/tests/test_proto_methods.rs b/tests/test_proto_methods.rs index 11214ec0dc6..bd4847c46bf 100644 --- a/tests/test_proto_methods.rs +++ b/tests/test_proto_methods.rs @@ -22,7 +22,7 @@ struct ExampleClass { impl ExampleClass { fn __getattr__(&self, py: Python<'_>, attr: &str) -> PyResult { if attr == "special_custom_attr" { - Ok(self.custom_attr.into_py(py)) + Ok(self.custom_attr.into_pyobject(py)?.into_any().unbind()) } else { Err(PyAttributeError::new_err(attr.to_string())) } diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index 4896207b0ab..13ba5405ed3 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -435,22 +435,22 @@ fn test_closure() { _kwargs: Option<&Bound<'_, types::PyDict>>| -> PyResult<_> { Python::with_gil(|py| { - let res: Vec<_> = args + let res: PyResult> = args .iter() .map(|elem| { if let Ok(i) = elem.extract::() { - (i + 1).into_py(py) + Ok((i + 1).into_pyobject(py)?.into_any().unbind()) } else if let Ok(f) = elem.extract::() { - (2. * f).into_py(py) + Ok((2. * f).into_pyobject(py)?.into_any().unbind()) } else if let Ok(mut s) = elem.extract::() { s.push_str("-py"); - s.into_py(py) + Ok(s.into_pyobject(py)?.into_any().unbind()) } else { panic!("unexpected argument type for {:?}", elem) } }) .collect(); - Ok(res) + res }) }; let closure_py = diff --git a/tests/test_pyself.rs b/tests/test_pyself.rs index a4899131c41..c4f3e6d6fa6 100644 --- a/tests/test_pyself.rs +++ b/tests/test_pyself.rs @@ -93,7 +93,7 @@ fn reader() -> Reader { #[test] fn test_nested_iter() { Python::with_gil(|py| { - let reader: PyObject = reader().into_py(py); + let reader = reader().into_pyobject(py).unwrap(); py_assert!( py, reader, @@ -105,7 +105,7 @@ fn test_nested_iter() { #[test] fn test_clone_ref() { Python::with_gil(|py| { - let reader: PyObject = reader().into_py(py); + let reader = reader().into_pyobject(py).unwrap(); py_assert!(py, reader, "reader == reader.clone_ref()"); py_assert!(py, reader, "reader == reader.clone_ref_with_py()"); }); diff --git a/tests/test_sequence.rs b/tests/test_sequence.rs index 484e519fd21..e8f61e80e42 100644 --- a/tests/test_sequence.rs +++ b/tests/test_sequence.rs @@ -263,13 +263,14 @@ struct GenericList { #[test] fn test_generic_list_get() { Python::with_gil(|py| { - let list: PyObject = GenericList { + let list = GenericList { items: [1i32, 2, 3] .iter() .map(|i| i.into_pyobject(py).unwrap().into_any().unbind()) .collect(), } - .into_py(py); + .into_pyobject(py) + .unwrap(); py_assert!(py, list, "list.items == [1, 2, 3]"); }); @@ -286,7 +287,7 @@ fn test_generic_list_set() { .items .iter() .zip(&[1u32, 2, 3]) - .all(|(a, b)| a.bind(py).eq(b.into_py(py)).unwrap())); + .all(|(a, b)| a.bind(py).eq(b.into_pyobject(py).unwrap()).unwrap())); }); }