Skip to content

Commit

Permalink
Remove IntoPyPointer
Browse files Browse the repository at this point in the history
it wasn't used in any public APIs following PyO3#3359
  • Loading branch information
alex committed Aug 14, 2023
1 parent a541925 commit 4088f7a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 70 deletions.
52 changes: 0 additions & 52 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
17 changes: 4 additions & 13 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4088f7a

Please sign in to comment.