Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove IntoPyPointer
Browse files Browse the repository at this point in the history
it wasn't used in any public APIs following #3359
alex committed Aug 14, 2023

Unverified

The signing certificate or its chain could not be verified.
1 parent a541925 commit 9cfcda5
Showing 5 changed files with 7 additions and 70 deletions.
1 change: 1 addition & 0 deletions newsfragments/3385.removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed `IntoPyPointer` trait, `into_ptr` is now an inherent method on most types representing Python objects
52 changes: 0 additions & 52 deletions src/conversion.rs
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 4 additions & 13 deletions src/instance.rs
Original file line number Diff line number Diff line change
@@ -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,
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 9cfcda5

Please sign in to comment.