Merge pull request #3385 from alex/remove-into-py-pointer

Remove IntoPyPointer
This commit is contained in:
David Hewitt 2023-08-14 21:22:39 +00:00 committed by GitHub
commit 1a33f8792f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 70 deletions

View File

@ -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

View File

@ -0,0 +1 @@
Removed `IntoPyPointer` trait, `into_ptr` is now an inherent method on most types representing Python objects

View File

@ -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<PyString> = "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<T> AsPyPointer for Option<T>
where
@ -105,26 +73,6 @@ where
}
}
/// Convert `None` into a null pointer.
impl<T> IntoPyPointer for Option<T>
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.

View File

@ -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<T> Py<T> {
kwargs: Option<&PyDict>,
) -> PyResult<PyObject> {
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<T> Py<T> {
{
let callee = self.getattr(py, name)?;
let args: Py<PyTuple> = 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<T> crate::AsPyPointer for Py<T> {
}
}
impl<T> IntoPyPointer for Py<T> {
/// 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<T> std::convert::From<&'_ T> for PyObject
where
T: AsPyPointer + PyNativeType,

View File

@ -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;

View File

@ -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;