diff --git a/guide/src/migration.md b/guide/src/migration.md index 79c5d2a2bd5..7e5a930a836 100644 --- a/guide/src/migration.md +++ b/guide/src/migration.md @@ -62,6 +62,10 @@ fn add(a: u64, b: u64) -> u64 { } ``` +### `IntoPyPointer` trait removed + +The trait `IntoPyPointer`, which provided the `into_ptr` method on many types, has been removed. `into_ptr` is now available as an inherent method on all types that previously implemented this trait. + ## from 0.18.* to 0.19 ### Access to `Python` inside `__traverse__` implementations are now forbidden diff --git a/newsfragments/3385.removed.md b/newsfragments/3385.removed.md new file mode 100644 index 00000000000..5691a4c2401 --- /dev/null +++ b/newsfragments/3385.removed.md @@ -0,0 +1 @@ +Removed `IntoPyPointer` trait, `into_ptr` is now an inherent method on most types representing Python objects diff --git a/src/conversion.rs b/src/conversion.rs index ba725ef42d5..95151242713 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -61,38 +61,6 @@ pub trait AsPyPointer { fn as_ptr(&self) -> *mut ffi::PyObject; } -/// Returns an owned pointer to a Python object. -/// -/// The returned pointer will be valid until you decrease its reference count. It may be null -/// depending on the implementation. -/// It is your responsibility to decrease the reference count of the pointer to avoid leaking memory. -/// -/// # Examples -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::types::PyString; -/// use pyo3::ffi; -/// -/// Python::with_gil(|py| { -/// let s: Py = "foo".into_py(py); -/// let ptr = s.into_ptr(); -/// -/// let is_really_a_pystring = unsafe { ffi::PyUnicode_CheckExact(ptr) }; -/// assert_eq!(is_really_a_pystring, 1); -/// -/// // Because we took ownership of the pointer, -/// // we must manually decrement it to avoid leaking memory -/// unsafe { ffi::Py_DECREF(ptr) }; -/// }); -/// ``` -pub trait IntoPyPointer { - /// Returns the underlying FFI pointer as an owned pointer. - /// - /// If `self` has ownership of the underlying pointer, it will "steal" ownership of it. - fn into_ptr(self) -> *mut ffi::PyObject; -} - /// Convert `None` into a null pointer. impl AsPyPointer for Option where @@ -105,26 +73,6 @@ where } } -/// Convert `None` into a null pointer. -impl IntoPyPointer for Option -where - T: IntoPyPointer, -{ - #[inline] - fn into_ptr(self) -> *mut ffi::PyObject { - self.map_or_else(std::ptr::null_mut, |t| t.into_ptr()) - } -} - -impl<'a, T> IntoPyPointer for &'a T -where - T: AsPyPointer, -{ - fn into_ptr(self) -> *mut ffi::PyObject { - unsafe { ffi::_Py_XNewRef(self.as_ptr()) } - } -} - /// Conversion trait that allows various objects to be converted into `PyObject`. pub trait ToPyObject { /// Converts self into a Python object. diff --git a/src/instance.rs b/src/instance.rs index 3a5e2b40a5c..b363c0a0342 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -5,8 +5,8 @@ use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell}; use crate::pyclass::boolean_struct::{False, True}; use crate::types::{PyDict, PyString, PyTuple}; use crate::{ - ffi, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyAny, PyClass, PyClassInitializer, - PyRef, PyRefMut, PyTypeInfo, Python, ToPyObject, + ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyClass, PyClassInitializer, PyRef, PyRefMut, + PyTypeInfo, Python, ToPyObject, }; use std::marker::PhantomData; use std::mem; @@ -693,7 +693,7 @@ impl Py { kwargs: Option<&PyDict>, ) -> PyResult { let args = args.into_py(py); - let kwargs = kwargs.into_ptr(); + let kwargs = kwargs.map_or(std::ptr::null_mut(), |p| p.into_ptr()); unsafe { let ret = PyObject::from_owned_ptr_or_err( @@ -750,7 +750,7 @@ impl Py { { let callee = self.getattr(py, name)?; let args: Py = args.into_py(py); - let kwargs = kwargs.into_ptr(); + let kwargs = kwargs.map_or(std::ptr::null_mut(), |p| p.into_ptr()); unsafe { let result = PyObject::from_owned_ptr_or_err( @@ -933,15 +933,6 @@ impl crate::AsPyPointer for Py { } } -impl IntoPyPointer for Py { - /// Gets the underlying FFI pointer, returns a owned pointer. - #[inline] - #[must_use] - fn into_ptr(self) -> *mut ffi::PyObject { - self.into_non_null().as_ptr() - } -} - impl std::convert::From<&'_ T> for PyObject where T: AsPyPointer + PyNativeType, diff --git a/src/lib.rs b/src/lib.rs index 5987c666d71..e8d813363b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -297,8 +297,7 @@ //! [`Ungil`]: crate::marker::Ungil pub use crate::class::*; pub use crate::conversion::{ - AsPyPointer, FromPyObject, FromPyPointer, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, - ToPyObject, + AsPyPointer, FromPyObject, FromPyPointer, IntoPy, PyTryFrom, PyTryInto, ToPyObject, }; pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyResult}; pub use crate::gil::GILPool; diff --git a/src/prelude.rs b/src/prelude.rs index ca0b0cf38db..73d7eda2b37 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -8,9 +8,7 @@ //! use pyo3::prelude::*; //! ``` -pub use crate::conversion::{ - FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, ToPyObject, -}; +pub use crate::conversion::{FromPyObject, IntoPy, PyTryFrom, PyTryInto, ToPyObject}; pub use crate::err::{PyErr, PyResult}; pub use crate::instance::{Py, PyObject}; pub use crate::marker::Python;