From 99c581987c72346a63824bfcccc08cf3ea5c2f51 Mon Sep 17 00:00:00 2001 From: Lily Foote Date: Mon, 19 Feb 2024 22:52:04 +0000 Subject: [PATCH] Deprecate `py.from_owned_ptr` methods --- src/conversion.rs | 31 +++++++++++++++++++++++++++++++ src/exceptions.rs | 1 + src/ffi/tests.rs | 10 +++++----- src/instance.rs | 10 ++++++++-- src/marker.rs | 24 ++++++++++++++++++++++++ src/pycell.rs | 1 + src/types/bytearray.rs | 1 + src/types/complex.rs | 1 + src/types/memoryview.rs | 1 + src/types/module.rs | 11 +++++++++-- src/types/slice.rs | 1 + 11 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/conversion.rs b/src/conversion.rs index d9536aa9445..825751206f6 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -497,13 +497,28 @@ 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. @@ -511,7 +526,15 @@ pub unsafe trait FromPyPointer<'p>: Sized { /// # 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`. @@ -519,7 +542,15 @@ pub unsafe trait FromPyPointer<'p>: Sized { /// # 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`. diff --git a/src/exceptions.rs b/src/exceptions.rs index 6ae9febc695..29f4db136c1 100644 --- a/src/exceptions.rs +++ b/src/exceptions.rs @@ -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()))?; diff --git a/src/ffi/tests.rs b/src/ffi/tests.rs index 610edb1c92c..6fbc1908fae 100644 --- a/src/ffi/tests.rs +++ b/src/ffi/tests.rs @@ -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::{ @@ -16,9 +16,9 @@ use libc::wchar_t; fn test_datetime_fromtimestamp() { Python::with_gil(|py| { let args: Py = (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(); @@ -37,9 +37,9 @@ fn test_datetime_fromtimestamp() { fn test_date_fromtimestamp() { Python::with_gil(|py| { let args: Py = (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(); diff --git a/src/instance.rs b/src/instance.rs index b83b69a20d3..1d0900f0f0d 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -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()) + } } } @@ -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()) + } } } diff --git a/src/marker.rs b/src/marker.rs index 4b46d3badbd..8c47530c09f 100644 --- a/src/marker.rs +++ b/src/marker.rs @@ -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(self, ptr: *mut ffi::PyObject) -> &'py T where T: FromPyPointer<'py>, { + #[allow(deprecated)] FromPyPointer::from_owned_ptr(self, ptr) } @@ -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(self, ptr: *mut ffi::PyObject) -> PyResult<&'py T> where T: FromPyPointer<'py>, { + #[allow(deprecated)] FromPyPointer::from_owned_ptr_or_err(self, ptr) } @@ -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(self, ptr: *mut ffi::PyObject) -> Option<&'py T> where T: FromPyPointer<'py>, { + #[allow(deprecated)] FromPyPointer::from_owned_ptr_or_opt(self, ptr) } diff --git a/src/pycell.rs b/src/pycell.rs index 8409c5cc679..0785b4841f1 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -288,6 +288,7 @@ impl PyCell { unsafe { let initializer = value.into(); let self_ = initializer.create_cell(py)?; + #[allow(deprecated)] FromPyPointer::from_owned_ptr_or_err(py, self_ as _) } } diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index 7f0fdf9ebbe..9fb15e45efe 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -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())) } diff --git a/src/types/complex.rs b/src/types/complex.rs index dafda97dbd5..dc783169b1a 100644 --- a/src/types/complex.rs +++ b/src/types/complex.rs @@ -143,6 +143,7 @@ mod not_limited_impls { fn neg(self) -> &'py PyComplex { unsafe { let val = (*self.as_ptr().cast::()).cval; + #[allow(deprecated)] self.py() .from_owned_ptr(ffi::PyComplex_FromCComplex(ffi::_Py_c_neg(val))) } diff --git a/src/types/memoryview.rs b/src/types/memoryview.rs index 414bfc69cfa..227cd2e0f3d 100644 --- a/src/types/memoryview.rs +++ b/src/types/memoryview.rs @@ -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())) } diff --git a/src/types/module.rs b/src/types/module.rs index 8824dfcf030..137b1b37e3a 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -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. @@ -67,7 +70,10 @@ impl PyModule { N: IntoPy>, { let name: Py = 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`, @@ -146,6 +152,7 @@ impl PyModule { return Err(PyErr::fetch(py)); } + #[allow(deprecated)] <&PyModule as FromPyObject>::extract(py.from_owned_ptr_or_err(mptr)?) } } diff --git a/src/types/slice.rs b/src/types/slice.rs index 8e86ff7ceee..61eea63d060 100644 --- a/src/types/slice.rs +++ b/src/types/slice.rs @@ -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) } }