From 033caa8fd1fefbf51fba97f85f5fbcb191c264bb Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Sat, 11 May 2024 15:48:17 +0200 Subject: [PATCH] split more impl blocks (#4175) --- src/types/boolobject.rs | 36 ++++++++++--------- src/types/bytearray.rs | 80 ++++++++++++++++++++--------------------- src/types/bytes.rs | 77 ++++++++++++++++++++------------------- src/types/capsule.rs | 70 ++++++++++++++++++------------------ src/types/complex.rs | 26 ++++++++------ src/types/datetime.rs | 8 +++++ src/types/float.rs | 33 +++++++++-------- src/types/slice.rs | 47 ++++++++++++------------ src/types/traceback.rs | 6 ++-- src/types/typeobject.rs | 70 ++++++++++++++++++------------------ 10 files changed, 243 insertions(+), 210 deletions(-) diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 43184e31..9b5aa659 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -1,9 +1,11 @@ #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; use crate::{ exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, - types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyNativeType, - PyObject, PyResult, Python, ToPyObject, + types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyObject, PyResult, + Python, ToPyObject, }; use super::any::PyAnyMethods; @@ -15,20 +17,6 @@ pub struct PyBool(PyAny); pyobject_native_type!(PyBool, ffi::PyObject, pyobject_native_static_type_object!(ffi::PyBool_Type), #checkfunction=ffi::PyBool_Check); impl PyBool { - /// Deprecated form of [`PyBool::new_bound`] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyBool::new` will be replaced by `PyBool::new_bound` in a future PyO3 version" - )] - #[inline] - pub fn new(py: Python<'_>, val: bool) -> &PyBool { - #[allow(deprecated)] - unsafe { - py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() }) - } - } - /// Depending on `val`, returns `true` or `false`. /// /// # Note @@ -42,6 +30,22 @@ impl PyBool { .downcast_unchecked() } } +} + +#[cfg(feature = "gil-refs")] +impl PyBool { + /// Deprecated form of [`PyBool::new_bound`] + #[deprecated( + since = "0.21.0", + note = "`PyBool::new` will be replaced by `PyBool::new_bound` in a future PyO3 version" + )] + #[inline] + pub fn new(py: Python<'_>, val: bool) -> &PyBool { + #[allow(deprecated)] + unsafe { + py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() }) + } + } /// Gets whether this boolean is `true`. #[inline] diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index ef20509e..ec3d7eaf 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -3,9 +3,9 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::instance::{Borrowed, Bound}; use crate::py_result_ext::PyResultExt; use crate::types::any::PyAnyMethods; +use crate::{ffi, PyAny, Python}; #[cfg(feature = "gil-refs")] -use crate::AsPyPointer; -use crate::{ffi, PyAny, PyNativeType, Python}; +use crate::{AsPyPointer, PyNativeType}; use std::os::raw::c_char; use std::slice; @@ -16,16 +16,6 @@ pub struct PyByteArray(PyAny); pyobject_native_type_core!(PyByteArray, pyobject_native_static_type_object!(ffi::PyByteArray_Type), #checkfunction=ffi::PyByteArray_Check); impl PyByteArray { - /// Deprecated form of [`PyByteArray::new_bound`] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyByteArray::new` will be replaced by `PyByteArray::new_bound` in a future PyO3 version" - )] - pub fn new<'py>(py: Python<'py>, src: &[u8]) -> &'py PyByteArray { - Self::new_bound(py, src).into_gil_ref() - } - /// Creates a new Python bytearray object. /// /// The byte string is initialized by copying the data from the `&[u8]`. @@ -39,19 +29,6 @@ impl PyByteArray { } } - /// Deprecated form of [`PyByteArray::new_bound_with`] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyByteArray::new_with` will be replaced by `PyByteArray::new_bound_with` in a future PyO3 version" - )] - pub fn new_with(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray> - where - F: FnOnce(&mut [u8]) -> PyResult<()>, - { - Self::new_bound_with(py, len, init).map(Bound::into_gil_ref) - } - /// Creates a new Python `bytearray` object with an `init` closure to write its contents. /// Before calling `init` the bytearray is zero-initialised. /// * If Python raises a MemoryError on the allocation, `new_with` will return @@ -101,16 +78,6 @@ impl PyByteArray { } } - /// Deprecated form of [`PyByteArray::from_bound`] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyByteArray::from` will be replaced by `PyByteArray::from_bound` in a future PyO3 version" - )] - pub fn from(src: &PyAny) -> PyResult<&PyByteArray> { - PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref) - } - /// Creates a new Python `bytearray` object from another Python object that /// implements the buffer protocol. pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult> { @@ -120,6 +87,39 @@ impl PyByteArray { .downcast_into_unchecked() } } +} + +#[cfg(feature = "gil-refs")] +impl PyByteArray { + /// Deprecated form of [`PyByteArray::new_bound`] + #[deprecated( + since = "0.21.0", + note = "`PyByteArray::new` will be replaced by `PyByteArray::new_bound` in a future PyO3 version" + )] + pub fn new<'py>(py: Python<'py>, src: &[u8]) -> &'py PyByteArray { + Self::new_bound(py, src).into_gil_ref() + } + + /// Deprecated form of [`PyByteArray::new_bound_with`] + #[deprecated( + since = "0.21.0", + note = "`PyByteArray::new_with` will be replaced by `PyByteArray::new_bound_with` in a future PyO3 version" + )] + pub fn new_with(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray> + where + F: FnOnce(&mut [u8]) -> PyResult<()>, + { + Self::new_bound_with(py, len, init).map(Bound::into_gil_ref) + } + + /// Deprecated form of [`PyByteArray::from_bound`] + #[deprecated( + since = "0.21.0", + note = "`PyByteArray::from` will be replaced by `PyByteArray::from_bound` in a future PyO3 version" + )] + pub fn from(src: &PyAny) -> PyResult<&PyByteArray> { + PyByteArray::from_bound(&src.as_borrowed()).map(Bound::into_gil_ref) + } /// Gets the length of the bytearray. #[inline] @@ -300,7 +300,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed { /// /// # Safety /// - /// See the safety requirements of [`PyByteArray::as_bytes`] and [`PyByteArray::as_bytes_mut`]. + /// See the safety requirements of [`PyByteArrayMethods::as_bytes`] and [`PyByteArrayMethods::as_bytes_mut`]. fn data(&self) -> *mut u8; /// Extracts a slice of the `ByteArray`'s entire buffer. @@ -311,7 +311,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed { /// undefined. /// /// These mutations may occur in Python code as well as from Rust: - /// - Calling methods like [`PyByteArray::as_bytes_mut`] and [`PyByteArray::resize`] will + /// - Calling methods like [`PyByteArrayMethods::as_bytes_mut`] and [`PyByteArrayMethods::resize`] will /// invalidate the slice. /// - Actions like dropping objects or raising exceptions can invoke `__del__`methods or signal /// handlers, which may execute arbitrary Python code. This means that if Python code has a @@ -405,7 +405,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed { /// # Safety /// /// Any other accesses of the `bytearray`'s buffer invalidate the slice. If it is used - /// afterwards, the behavior is undefined. The safety requirements of [`PyByteArray::as_bytes`] + /// afterwards, the behavior is undefined. The safety requirements of [`PyByteArrayMethods::as_bytes`] /// apply to this function as well. #[allow(clippy::mut_from_ref)] unsafe fn as_bytes_mut(&self) -> &mut [u8]; @@ -432,8 +432,8 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed { /// Resizes the bytearray object to the new length `len`. /// - /// Note that this will invalidate any pointers obtained by [PyByteArray::data], as well as - /// any (unsafe) slices obtained from [PyByteArray::as_bytes] and [PyByteArray::as_bytes_mut]. + /// Note that this will invalidate any pointers obtained by [PyByteArrayMethods::data], as well as + /// any (unsafe) slices obtained from [PyByteArrayMethods::as_bytes] and [PyByteArrayMethods::as_bytes_mut]. fn resize(&self, len: usize) -> PyResult<()>; } diff --git a/src/types/bytes.rs b/src/types/bytes.rs index f3da65c7..1d6a2f8e 100644 --- a/src/types/bytes.rs +++ b/src/types/bytes.rs @@ -1,7 +1,9 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::instance::{Borrowed, Bound}; use crate::types::any::PyAnyMethods; -use crate::{ffi, Py, PyAny, PyNativeType, PyResult, Python}; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; +use crate::{ffi, Py, PyAny, PyResult, Python}; use std::ops::Index; use std::os::raw::c_char; use std::slice::SliceIndex; @@ -16,16 +18,6 @@ pub struct PyBytes(PyAny); pyobject_native_type_core!(PyBytes, pyobject_native_static_type_object!(ffi::PyBytes_Type), #checkfunction=ffi::PyBytes_Check); impl PyBytes { - /// Deprecated form of [`PyBytes::new_bound`]. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyBytes::new` will be replaced by `PyBytes::new_bound` in a future PyO3 version" - )] - pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes { - Self::new_bound(py, s).into_gil_ref() - } - /// Creates a new Python bytestring object. /// The bytestring is initialized by copying the data from the `&[u8]`. /// @@ -40,19 +32,6 @@ impl PyBytes { } } - /// Deprecated form of [`PyBytes::new_bound_with`]. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyBytes::new_with` will be replaced by `PyBytes::new_bound_with` in a future PyO3 version" - )] - pub fn new_with(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes> - where - F: FnOnce(&mut [u8]) -> PyResult<()>, - { - Self::new_bound_with(py, len, init).map(Bound::into_gil_ref) - } - /// Creates a new Python `bytes` object with an `init` closure to write its contents. /// Before calling `init` the bytes' contents are zero-initialised. /// * If Python raises a MemoryError on the allocation, `new_with` will return @@ -95,19 +74,6 @@ impl PyBytes { } } - /// Deprecated form of [`PyBytes::bound_from_ptr`]. - /// - /// # Safety - /// See [`PyBytes::bound_from_ptr`]. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyBytes::from_ptr` will be replaced by `PyBytes::bound_from_ptr` in a future PyO3 version" - )] - pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes { - Self::bound_from_ptr(py, ptr, len).into_gil_ref() - } - /// Creates a new Python byte string object from a raw pointer and length. /// /// Panics if out of memory. @@ -123,6 +89,42 @@ impl PyBytes { .assume_owned(py) .downcast_into_unchecked() } +} + +#[cfg(feature = "gil-refs")] +impl PyBytes { + /// Deprecated form of [`PyBytes::new_bound`]. + #[deprecated( + since = "0.21.0", + note = "`PyBytes::new` will be replaced by `PyBytes::new_bound` in a future PyO3 version" + )] + pub fn new<'p>(py: Python<'p>, s: &[u8]) -> &'p PyBytes { + Self::new_bound(py, s).into_gil_ref() + } + + /// Deprecated form of [`PyBytes::new_bound_with`]. + #[deprecated( + since = "0.21.0", + note = "`PyBytes::new_with` will be replaced by `PyBytes::new_bound_with` in a future PyO3 version" + )] + pub fn new_with(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes> + where + F: FnOnce(&mut [u8]) -> PyResult<()>, + { + Self::new_bound_with(py, len, init).map(Bound::into_gil_ref) + } + + /// Deprecated form of [`PyBytes::bound_from_ptr`]. + /// + /// # Safety + /// See [`PyBytes::bound_from_ptr`]. + #[deprecated( + since = "0.21.0", + note = "`PyBytes::from_ptr` will be replaced by `PyBytes::bound_from_ptr` in a future PyO3 version" + )] + pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes { + Self::bound_from_ptr(py, ptr, len).into_gil_ref() + } /// Gets the Python string as a byte slice. #[inline] @@ -172,6 +174,7 @@ impl Py { } /// This is the same way [Vec] is indexed. +#[cfg(feature = "gil-refs")] impl> Index for PyBytes { type Output = I::Output; diff --git a/src/types/capsule.rs b/src/types/capsule.rs index c2b73a0d..2851970b 100644 --- a/src/types/capsule.rs +++ b/src/types/capsule.rs @@ -1,11 +1,12 @@ use crate::ffi_ptr_ext::FfiPtrExt; use crate::py_result_ext::PyResultExt; -use crate::{ffi, PyAny, PyNativeType}; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; +use crate::{ffi, PyAny}; use crate::{Bound, Python}; use crate::{PyErr, PyResult}; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_int, c_void}; - /// Represents a Python Capsule /// as described in [Capsules](https://docs.python.org/3/c-api/capsule.html#capsules): /// > This subtype of PyObject represents an opaque value, useful for C extension @@ -46,20 +47,6 @@ pub struct PyCapsule(PyAny); pyobject_native_type_core!(PyCapsule, pyobject_native_static_type_object!(ffi::PyCapsule_Type), #checkfunction=ffi::PyCapsule_CheckExact); impl PyCapsule { - /// Deprecated form of [`PyCapsule::new_bound`]. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyCapsule::new` will be replaced by `PyCapsule::new_bound` in a future PyO3 version" - )] - pub fn new( - py: Python<'_>, - value: T, - name: Option, - ) -> PyResult<&Self> { - Self::new_bound(py, value, name).map(Bound::into_gil_ref) - } - /// Constructs a new capsule whose contents are `value`, associated with `name`. /// `name` is the identifier for the capsule; if it is stored as an attribute of a module, /// the name should be in the format `"modulename.attribute"`. @@ -99,24 +86,6 @@ impl PyCapsule { Self::new_bound_with_destructor(py, value, name, |_, _| {}) } - /// Deprecated form of [`PyCapsule::new_bound_with_destructor`]. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyCapsule::new_with_destructor` will be replaced by `PyCapsule::new_bound_with_destructor` in a future PyO3 version" - )] - pub fn new_with_destructor< - T: 'static + Send + AssertNotZeroSized, - F: FnOnce(T, *mut c_void) + Send, - >( - py: Python<'_>, - value: T, - name: Option, - destructor: F, - ) -> PyResult<&'_ Self> { - Self::new_bound_with_destructor(py, value, name, destructor).map(Bound::into_gil_ref) - } - /// Constructs a new capsule whose contents are `value`, associated with `name`. /// /// Also provides a destructor: when the `PyCapsule` is destroyed, it will be passed the original object, @@ -172,6 +141,39 @@ impl PyCapsule { Ok(&*ptr.cast::()) } } +} + +#[cfg(feature = "gil-refs")] +impl PyCapsule { + /// Deprecated form of [`PyCapsule::new_bound`]. + #[deprecated( + since = "0.21.0", + note = "`PyCapsule::new` will be replaced by `PyCapsule::new_bound` in a future PyO3 version" + )] + pub fn new( + py: Python<'_>, + value: T, + name: Option, + ) -> PyResult<&Self> { + Self::new_bound(py, value, name).map(Bound::into_gil_ref) + } + + /// Deprecated form of [`PyCapsule::new_bound_with_destructor`]. + #[deprecated( + since = "0.21.0", + note = "`PyCapsule::new_with_destructor` will be replaced by `PyCapsule::new_bound_with_destructor` in a future PyO3 version" + )] + pub fn new_with_destructor< + T: 'static + Send + AssertNotZeroSized, + F: FnOnce(T, *mut c_void) + Send, + >( + py: Python<'_>, + value: T, + name: Option, + destructor: F, + ) -> PyResult<&'_ Self> { + Self::new_bound_with_destructor(py, value, name, destructor).map(Bound::into_gil_ref) + } /// Sets the context pointer in the capsule. /// diff --git a/src/types/complex.rs b/src/types/complex.rs index cd3d5810..65b08cc9 100644 --- a/src/types/complex.rs +++ b/src/types/complex.rs @@ -1,4 +1,6 @@ -use crate::{ffi, types::any::PyAnyMethods, Bound, PyAny, PyNativeType, Python}; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; +use crate::{ffi, types::any::PyAnyMethods, Bound, PyAny, Python}; use std::os::raw::c_double; /// Represents a Python [`complex`](https://docs.python.org/3/library/functions.html#complex) object. @@ -19,15 +21,6 @@ pyobject_native_type!( ); impl PyComplex { - /// Deprecated form of [`PyComplex::from_doubles_bound`] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyComplex::from_doubles` will be replaced by `PyComplex::from_doubles_bound` in a future PyO3 version" - )] - pub fn from_doubles(py: Python<'_>, real: c_double, imag: c_double) -> &PyComplex { - Self::from_doubles_bound(py, real, imag).into_gil_ref() - } /// Creates a new `PyComplex` from the given real and imaginary values. pub fn from_doubles_bound( py: Python<'_>, @@ -41,6 +34,19 @@ impl PyComplex { .downcast_into_unchecked() } } +} + +#[cfg(feature = "gil-refs")] +impl PyComplex { + /// Deprecated form of [`PyComplex::from_doubles_bound`] + #[deprecated( + since = "0.21.0", + note = "`PyComplex::from_doubles` will be replaced by `PyComplex::from_doubles_bound` in a future PyO3 version" + )] + pub fn from_doubles(py: Python<'_>, real: c_double, imag: c_double) -> &PyComplex { + Self::from_doubles_bound(py, real, imag).into_gil_ref() + } + /// Returns the real part of the complex number. pub fn real(&self) -> c_double { self.as_borrowed().real() diff --git a/src/types/datetime.rs b/src/types/datetime.rs index 12db67ab..cdf3b011 100644 --- a/src/types/datetime.rs +++ b/src/types/datetime.rs @@ -22,6 +22,7 @@ use crate::ffi::{ PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND, }; use crate::ffi_ptr_ext::FfiPtrExt; +#[cfg(feature = "gil-refs")] use crate::instance::PyNativeType; use crate::py_result_ext::PyResultExt; use crate::types::any::PyAnyMethods; @@ -249,6 +250,7 @@ impl PyDate { } } +#[cfg(feature = "gil-refs")] impl PyDateAccess for PyDate { fn get_year(&self) -> i32 { self.as_borrowed().get_year() @@ -461,6 +463,7 @@ impl PyDateTime { } } +#[cfg(feature = "gil-refs")] impl PyDateAccess for PyDateTime { fn get_year(&self) -> i32 { self.as_borrowed().get_year() @@ -489,6 +492,7 @@ impl PyDateAccess for Bound<'_, PyDateTime> { } } +#[cfg(feature = "gil-refs")] impl PyTimeAccess for PyDateTime { fn get_hour(&self) -> u8 { self.as_borrowed().get_hour() @@ -533,6 +537,7 @@ impl PyTimeAccess for Bound<'_, PyDateTime> { } } +#[cfg(feature = "gil-refs")] impl<'py> PyTzInfoAccess<'py> for &'py PyDateTime { fn get_tzinfo_bound(&self) -> Option> { self.as_borrowed().get_tzinfo_bound() @@ -688,6 +693,7 @@ impl PyTime { } } +#[cfg(feature = "gil-refs")] impl PyTimeAccess for PyTime { fn get_hour(&self) -> u8 { self.as_borrowed().get_hour() @@ -732,6 +738,7 @@ impl PyTimeAccess for Bound<'_, PyTime> { } } +#[cfg(feature = "gil-refs")] impl<'py> PyTzInfoAccess<'py> for &'py PyTime { fn get_tzinfo_bound(&self) -> Option> { self.as_borrowed().get_tzinfo_bound() @@ -878,6 +885,7 @@ impl PyDelta { } } +#[cfg(feature = "gil-refs")] impl PyDeltaAccess for PyDelta { fn get_days(&self) -> i32 { self.as_borrowed().get_days() diff --git a/src/types/float.rs b/src/types/float.rs index 2ed3d292..8499d1e5 100644 --- a/src/types/float.rs +++ b/src/types/float.rs @@ -1,13 +1,14 @@ +use super::any::PyAnyMethods; #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; use crate::{ - ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, - PyObject, PyResult, Python, ToPyObject, + ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, + PyResult, Python, ToPyObject, }; use std::os::raw::c_double; -use super::any::PyAnyMethods; - /// Represents a Python `float` object. /// /// You can usually avoid directly working with this type @@ -24,17 +25,6 @@ pyobject_native_type!( ); impl PyFloat { - /// Deprecated form of [`PyFloat::new_bound`]. - #[inline] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyFloat::new` will be replaced by `PyFloat::new_bound` in a future PyO3 version" - )] - pub fn new(py: Python<'_>, val: f64) -> &'_ Self { - Self::new_bound(py, val).into_gil_ref() - } - /// Creates a new Python `float` object. pub fn new_bound(py: Python<'_>, val: c_double) -> Bound<'_, PyFloat> { unsafe { @@ -43,6 +33,19 @@ impl PyFloat { .downcast_into_unchecked() } } +} + +#[cfg(feature = "gil-refs")] +impl PyFloat { + /// Deprecated form of [`PyFloat::new_bound`]. + #[inline] + #[deprecated( + since = "0.21.0", + note = "`PyFloat::new` will be replaced by `PyFloat::new_bound` in a future PyO3 version" + )] + pub fn new(py: Python<'_>, val: f64) -> &'_ Self { + Self::new_bound(py, val).into_gil_ref() + } /// Gets the value of this float. pub fn value(&self) -> c_double { diff --git a/src/types/slice.rs b/src/types/slice.rs index 70285c9c..7daa2c03 100644 --- a/src/types/slice.rs +++ b/src/types/slice.rs @@ -2,7 +2,9 @@ use crate::err::{PyErr, PyResult}; use crate::ffi; use crate::ffi_ptr_ext::FfiPtrExt; use crate::types::any::PyAnyMethods; -use crate::{Bound, PyAny, PyNativeType, PyObject, Python, ToPyObject}; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; +use crate::{Bound, PyAny, PyObject, Python, ToPyObject}; /// Represents a Python `slice`. /// @@ -17,7 +19,7 @@ pyobject_native_type!( #checkfunction=ffi::PySlice_Check ); -/// Return value from [`PySlice::indices`]. +/// Return value from [`PySliceMethods::indices`]. #[derive(Debug, Eq, PartialEq)] pub struct PySliceIndices { /// Start of the slice @@ -47,16 +49,6 @@ impl PySliceIndices { } impl PySlice { - /// Deprecated form of `PySlice::new_bound`. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PySlice::new` will be replaced by `PySlice::new_bound` in a future PyO3 version" - )] - pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> &PySlice { - Self::new_bound(py, start, stop, step).into_gil_ref() - } - /// Constructs a new slice with the given elements. pub fn new_bound(py: Python<'_>, start: isize, stop: isize, step: isize) -> Bound<'_, PySlice> { unsafe { @@ -70,16 +62,6 @@ impl PySlice { } } - /// Deprecated form of `PySlice::full_bound`. - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PySlice::full` will be replaced by `PySlice::full_bound` in a future PyO3 version" - )] - pub fn full(py: Python<'_>) -> &PySlice { - PySlice::full_bound(py).into_gil_ref() - } - /// Constructs a new full slice that is equivalent to `::`. pub fn full_bound(py: Python<'_>) -> Bound<'_, PySlice> { unsafe { @@ -88,6 +70,27 @@ impl PySlice { .downcast_into_unchecked() } } +} + +#[cfg(feature = "gil-refs")] +impl PySlice { + /// Deprecated form of `PySlice::new_bound`. + #[deprecated( + since = "0.21.0", + note = "`PySlice::new` will be replaced by `PySlice::new_bound` in a future PyO3 version" + )] + pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> &PySlice { + Self::new_bound(py, start, stop, step).into_gil_ref() + } + + /// Deprecated form of `PySlice::full_bound`. + #[deprecated( + since = "0.21.0", + note = "`PySlice::full` will be replaced by `PySlice::full_bound` in a future PyO3 version" + )] + pub fn full(py: Python<'_>) -> &PySlice { + PySlice::full_bound(py).into_gil_ref() + } /// Retrieves the start, stop, and step indices from the slice object, /// assuming a sequence of length `length`, and stores the length of the diff --git a/src/types/traceback.rs b/src/types/traceback.rs index c4cedd79..dbbdb6a8 100644 --- a/src/types/traceback.rs +++ b/src/types/traceback.rs @@ -1,7 +1,8 @@ use crate::err::{error_on_minusone, PyResult}; use crate::types::{any::PyAnyMethods, string::PyStringMethods, PyString}; -use crate::{ffi, Bound}; -use crate::{PyAny, PyNativeType}; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; +use crate::{ffi, Bound, PyAny}; /// Represents a Python traceback. #[repr(transparent)] @@ -13,6 +14,7 @@ pyobject_native_type_core!( #checkfunction=ffi::PyTraceBack_Check ); +#[cfg(feature = "gil-refs")] impl PyTraceback { /// Formats the traceback as a string. /// diff --git a/src/types/typeobject.rs b/src/types/typeobject.rs index 2261834e..e6c4a218 100644 --- a/src/types/typeobject.rs +++ b/src/types/typeobject.rs @@ -1,11 +1,12 @@ use crate::err::{self, PyResult}; use crate::instance::Borrowed; use crate::types::any::PyAnyMethods; -use crate::{ffi, Bound, PyAny, PyNativeType, PyTypeInfo, Python}; +#[cfg(feature = "gil-refs")] +use crate::PyNativeType; +use crate::{ffi, Bound, PyAny, PyTypeInfo, Python}; use std::borrow::Cow; #[cfg(not(any(Py_LIMITED_API, PyPy)))] use std::ffi::CStr; - /// Represents a reference to a Python `type object`. #[repr(transparent)] pub struct PyType(PyAny); @@ -13,44 +14,12 @@ pub struct PyType(PyAny); pyobject_native_type_core!(PyType, pyobject_native_static_type_object!(ffi::PyType_Type), #checkfunction=ffi::PyType_Check); impl PyType { - /// Deprecated form of [`PyType::new_bound`]. - #[inline] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "`PyType::new` will be replaced by `PyType::new_bound` in a future PyO3 version" - )] - pub fn new(py: Python<'_>) -> &PyType { - T::type_object_bound(py).into_gil_ref() - } - /// Creates a new type object. #[inline] pub fn new_bound(py: Python<'_>) -> Bound<'_, PyType> { T::type_object_bound(py) } - /// Retrieves the underlying FFI pointer associated with this Python object. - #[inline] - pub fn as_type_ptr(&self) -> *mut ffi::PyTypeObject { - self.as_borrowed().as_type_ptr() - } - - /// Deprecated form of [`PyType::from_borrowed_type_ptr`]. - /// - /// # Safety - /// - /// - The pointer must a valid non-null reference to a `PyTypeObject`. - #[inline] - #[cfg(feature = "gil-refs")] - #[deprecated( - since = "0.21.0", - note = "Use `PyType::from_borrowed_type_ptr` instead" - )] - pub unsafe fn from_type_ptr(py: Python<'_>, p: *mut ffi::PyTypeObject) -> &PyType { - Self::from_borrowed_type_ptr(py, p).into_gil_ref() - } - /// Converts the given FFI pointer into `Bound`, to use in safe code. /// /// The function creates a new reference from the given pointer, and returns @@ -67,6 +36,39 @@ impl PyType { .downcast_unchecked() .to_owned() } +} + +#[cfg(feature = "gil-refs")] +impl PyType { + /// Deprecated form of [`PyType::new_bound`]. + #[inline] + #[deprecated( + since = "0.21.0", + note = "`PyType::new` will be replaced by `PyType::new_bound` in a future PyO3 version" + )] + pub fn new(py: Python<'_>) -> &PyType { + T::type_object_bound(py).into_gil_ref() + } + + /// Retrieves the underlying FFI pointer associated with this Python object. + #[inline] + pub fn as_type_ptr(&self) -> *mut ffi::PyTypeObject { + self.as_borrowed().as_type_ptr() + } + + /// Deprecated form of [`PyType::from_borrowed_type_ptr`]. + /// + /// # Safety + /// + /// - The pointer must a valid non-null reference to a `PyTypeObject`. + #[inline] + #[deprecated( + since = "0.21.0", + note = "Use `PyType::from_borrowed_type_ptr` instead" + )] + pub unsafe fn from_type_ptr(py: Python<'_>, p: *mut ffi::PyTypeObject) -> &PyType { + Self::from_borrowed_type_ptr(py, p).into_gil_ref() + } /// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`. pub fn qualname(&self) -> PyResult {