Skip to content

Commit

Permalink
Deprecate py.from_owned_ptr methods
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Feb 20, 2024
1 parent 61bc02d commit 3911f70
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 21 deletions.
31 changes: 31 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,29 +434,60 @@ pub unsafe trait FromPyPointer<'p>: Sized {
/// Implementations must ensure the object does not get freed during `'p`
/// and ensure that `ptr` is of the correct type.
/// Note that it must be safe to decrement the reference count of `ptr`.
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr_or_opt(py, ptr)` or `Bound::from_owned_ptr_or_opt(py, ptr)` instead"
)
)]
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self>;
/// Convert from an arbitrary `PyObject` or panic.
///
/// # Safety
///
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
)
)]
unsafe fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
#[allow(deprecated)]
Self::from_owned_ptr_or_opt(py, ptr).unwrap_or_else(|| err::panic_after_error(py))
}
/// Convert from an arbitrary `PyObject` or panic.
///
/// # Safety
///
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
)
)]
unsafe fn from_owned_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
#[allow(deprecated)]
Self::from_owned_ptr_or_panic(py, ptr)
}
/// Convert from an arbitrary `PyObject`.
///
/// # Safety
///
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr_or_err(py, ptr)` or `Bound::from_owned_ptr_or_err(py, ptr)` instead"
)
)]
unsafe fn from_owned_ptr_or_err(py: Python<'p>, ptr: *mut ffi::PyObject) -> PyResult<&'p Self> {
#[allow(deprecated)]
Self::from_owned_ptr_or_opt(py, ptr).ok_or_else(|| err::PyErr::fetch(py))
}
/// Convert from an arbitrary borrowed `PyObject`.
Expand Down
1 change: 1 addition & 0 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ macro_rules! impl_exception_boilerplate {
impl ::std::error::Error for $name {
fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> {
unsafe {
#[allow(deprecated)]
let cause: &$crate::exceptions::PyBaseException = self
.py()
.from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?;
Expand Down
10 changes: 5 additions & 5 deletions src/ffi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::Python;
#[cfg(not(Py_LIMITED_API))]
use crate::{
types::{PyDict, PyString},
IntoPy, Py, PyAny,
Bound, IntoPy, Py, PyAny,
};
#[cfg(not(any(Py_3_12, Py_LIMITED_API)))]
use libc::wchar_t;
Expand All @@ -16,9 +16,9 @@ use libc::wchar_t;
fn test_datetime_fromtimestamp() {
Python::with_gil(|py| {
let args: Py<PyAny> = (100,).into_py(py);
let dt: &PyAny = unsafe {
let dt = unsafe {
PyDateTime_IMPORT();
py.from_owned_ptr(PyDateTime_FromTimestamp(args.as_ptr()))
Bound::from_owned_ptr(py, PyDateTime_FromTimestamp(args.as_ptr()))
};
let locals = PyDict::new_bound(py);
locals.set_item("dt", dt).unwrap();
Expand All @@ -37,9 +37,9 @@ fn test_datetime_fromtimestamp() {
fn test_date_fromtimestamp() {
Python::with_gil(|py| {
let args: Py<PyAny> = (100,).into_py(py);
let dt: &PyAny = unsafe {
let dt = unsafe {
PyDateTime_IMPORT();
py.from_owned_ptr(PyDate_FromTimestamp(args.as_ptr()))
Bound::from_owned_ptr(py, PyDate_FromTimestamp(args.as_ptr()))
};
let locals = PyDict::new_bound(py);
locals.set_item("dt", dt).unwrap();
Expand Down
10 changes: 8 additions & 2 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ impl<'py, T> Bound<'py, T> {
where
T: HasPyGilRef,
{
unsafe { self.py().from_owned_ptr(self.into_ptr()) }
#[allow(deprecated)]
unsafe {
self.py().from_owned_ptr(self.into_ptr())
}
}
}

Expand Down Expand Up @@ -970,7 +973,10 @@ where
)
)]
pub fn into_ref(self, py: Python<'_>) -> &T::AsRefTarget {
unsafe { py.from_owned_ptr(self.into_ptr()) }
#[allow(deprecated)]
unsafe {
py.from_owned_ptr(self.into_ptr())
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,10 +885,18 @@ impl<'py> Python<'py> {
///
/// Callers must ensure that ensure that the cast is valid.
#[allow(clippy::wrong_self_convention)]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
)
)]
pub unsafe fn from_owned_ptr<T>(self, ptr: *mut ffi::PyObject) -> &'py T
where
T: FromPyPointer<'py>,
{
#[allow(deprecated)]
FromPyPointer::from_owned_ptr(self, ptr)
}

Expand All @@ -901,10 +909,18 @@ impl<'py> Python<'py> {
///
/// Callers must ensure that ensure that the cast is valid.
#[allow(clippy::wrong_self_convention)]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr_or_err(py, ptr)` or `Bound::from_owned_ptr_or_err(py, ptr)` instead"
)
)]
pub unsafe fn from_owned_ptr_or_err<T>(self, ptr: *mut ffi::PyObject) -> PyResult<&'py T>
where
T: FromPyPointer<'py>,
{
#[allow(deprecated)]
FromPyPointer::from_owned_ptr_or_err(self, ptr)
}

Expand All @@ -917,10 +933,18 @@ impl<'py> Python<'py> {
///
/// Callers must ensure that ensure that the cast is valid.
#[allow(clippy::wrong_self_convention)]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "part of the deprecated GIL Ref API; to migrate use `Py::from_owned_ptr_or_opt(py, ptr)` or `Bound::from_owned_ptr_or_opt(py, ptr)` instead"
)
)]
pub unsafe fn from_owned_ptr_or_opt<T>(self, ptr: *mut ffi::PyObject) -> Option<&'py T>
where
T: FromPyPointer<'py>,
{
#[allow(deprecated)]
FromPyPointer::from_owned_ptr_or_opt(self, ptr)
}

Expand Down
1 change: 1 addition & 0 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl<T: PyClass> PyCell<T> {
unsafe {
let initializer = value.into();
let self_ = initializer.create_cell(py)?;
#[allow(deprecated)]
FromPyPointer::from_owned_ptr_or_err(py, self_ as _)
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/types/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ impl PyByteArray {
)
)]
pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
unsafe {
src.py()
.from_owned_ptr_or_err(ffi::PyByteArray_FromObject(src.as_ptr()))
}
PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
}

/// Creates a new Python `bytearray` object from another Python object that
Expand Down
1 change: 1 addition & 0 deletions src/types/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ mod not_limited_impls {
fn neg(self) -> &'py PyComplex {
unsafe {
let val = (*self.as_ptr().cast::<ffi::PyComplexObject>()).cval;
#[allow(deprecated)]
self.py()
.from_owned_ptr(ffi::PyComplex_FromCComplex(ffi::_Py_c_neg(val)))
}
Expand Down
5 changes: 1 addition & 4 deletions src/types/memoryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ impl PyMemoryView {
)
)]
pub fn from(src: &PyAny) -> PyResult<&PyMemoryView> {
unsafe {
src.py()
.from_owned_ptr_or_err(ffi::PyMemoryView_FromObject(src.as_ptr()))
}
PyMemoryView::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref)
}

/// Creates a new Python `memoryview` object from another Python object that
Expand Down
11 changes: 9 additions & 2 deletions src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ impl PyModule {
pub fn new<'p>(py: Python<'p>, name: &str) -> PyResult<&'p PyModule> {
// Could use PyModule_NewObject, but it doesn't exist on PyPy.
let name = CString::new(name)?;
unsafe { py.from_owned_ptr_or_err(ffi::PyModule_New(name.as_ptr())) }
#[allow(deprecated)]
unsafe {
py.from_owned_ptr_or_err(ffi::PyModule_New(name.as_ptr()))
}
}

/// Imports the Python module with the specified name.
Expand All @@ -67,7 +70,10 @@ impl PyModule {
N: IntoPy<Py<PyString>>,
{
let name: Py<PyString> = name.into_py(py);
unsafe { py.from_owned_ptr_or_err(ffi::PyImport_Import(name.as_ptr())) }
#[allow(deprecated)]
unsafe {
py.from_owned_ptr_or_err(ffi::PyImport_Import(name.as_ptr()))
}
}

/// Creates and loads a module named `module_name`,
Expand Down Expand Up @@ -146,6 +152,7 @@ impl PyModule {
return Err(PyErr::fetch(py));
}

#[allow(deprecated)]
<&PyModule as FromPyObject>::extract(py.from_owned_ptr_or_err(mptr)?)
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/types/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ impl PySlice {
)
)]
pub fn full(py: Python<'_>) -> &PySlice {
unsafe {
let ptr = ffi::PySlice_New(ffi::Py_None(), ffi::Py_None(), ffi::Py_None());
py.from_owned_ptr(ptr)
}
PySlice::full_bound(py).into_gil_ref()
}

/// Constructs a new full slice that is equivalent to `::`.
Expand Down
1 change: 1 addition & 0 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl PyString {
#[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))]
{
let bytes = unsafe {
#[allow(deprecated)]
self.py()
.from_owned_ptr_or_err::<PyBytes>(ffi::PyUnicode_AsUTF8String(self.as_ptr()))
}?;
Expand Down

0 comments on commit 3911f70

Please sign in to comment.