Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IntoPyPointer #3385

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
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