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 19, 2024
1 parent 0bb9cab commit 99c5819
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 9 deletions.
31 changes: 31 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,29 +497,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` or `Bound::from_owned_ptr_or_opt` 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_or_panic` or `Bound::from_owned_ptr_or_panic` 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` or `Bound::from_owned_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` or `Bound::from_owned_ptr_or_err` 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
@@ -1,6 +1,6 @@
use crate::ffi::*;
use crate::types::any::PyAnyMethods;
use crate::Python;
use crate::{Bound, Python};

#[cfg(not(Py_LIMITED_API))]
use crate::{
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 @@ -957,7 +960,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 @@ -883,10 +883,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 `Bound::from_owned_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 @@ -899,10 +907,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` or `Bound::from_owned_ptr_or_err` 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 @@ -915,10 +931,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` or `Bound::from_owned_ptr_or_opt` 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 @@ -288,6 +288,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
1 change: 1 addition & 0 deletions src/types/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl PyByteArray {
)]
pub fn from(src: &PyAny) -> PyResult<&PyByteArray> {
unsafe {
#[allow(deprecated)]
src.py()
.from_owned_ptr_or_err(ffi::PyByteArray_FromObject(src.as_ptr()))
}
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
1 change: 1 addition & 0 deletions src/types/memoryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl PyMemoryView {
)]
pub fn from(src: &PyAny) -> PyResult<&PyMemoryView> {
unsafe {
#[allow(deprecated)]
src.py()
.from_owned_ptr_or_err(ffi::PyMemoryView_FromObject(src.as_ptr()))
}
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
1 change: 1 addition & 0 deletions src/types/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl PySlice {
pub fn full(py: Python<'_>) -> &PySlice {
unsafe {
let ptr = ffi::PySlice_New(ffi::Py_None(), ffi::Py_None(), ffi::Py_None());
#[allow(deprecated)]
py.from_owned_ptr(ptr)
}
}
Expand Down

0 comments on commit 99c5819

Please sign in to comment.