split more impl blocks (#4175)

This commit is contained in:
Icxolu 2024-05-11 15:48:17 +02:00 committed by GitHub
parent 444be3bafa
commit 033caa8fd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 243 additions and 210 deletions

View File

@ -1,9 +1,11 @@
#[cfg(feature = "experimental-inspect")] #[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo; use crate::inspect::types::TypeInfo;
#[cfg(feature = "gil-refs")]
use crate::PyNativeType;
use crate::{ use crate::{
exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound,
types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyNativeType, types::typeobject::PyTypeMethods, Borrowed, FromPyObject, IntoPy, PyAny, PyObject, PyResult,
PyObject, PyResult, Python, ToPyObject, Python, ToPyObject,
}; };
use super::any::PyAnyMethods; 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); pyobject_native_type!(PyBool, ffi::PyObject, pyobject_native_static_type_object!(ffi::PyBool_Type), #checkfunction=ffi::PyBool_Check);
impl PyBool { 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`. /// Depending on `val`, returns `true` or `false`.
/// ///
/// # Note /// # Note
@ -42,6 +30,22 @@ impl PyBool {
.downcast_unchecked() .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`. /// Gets whether this boolean is `true`.
#[inline] #[inline]

View File

@ -3,9 +3,9 @@ use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::{Borrowed, Bound}; use crate::instance::{Borrowed, Bound};
use crate::py_result_ext::PyResultExt; use crate::py_result_ext::PyResultExt;
use crate::types::any::PyAnyMethods; use crate::types::any::PyAnyMethods;
use crate::{ffi, PyAny, Python};
#[cfg(feature = "gil-refs")] #[cfg(feature = "gil-refs")]
use crate::AsPyPointer; use crate::{AsPyPointer, PyNativeType};
use crate::{ffi, PyAny, PyNativeType, Python};
use std::os::raw::c_char; use std::os::raw::c_char;
use std::slice; 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); pyobject_native_type_core!(PyByteArray, pyobject_native_static_type_object!(ffi::PyByteArray_Type), #checkfunction=ffi::PyByteArray_Check);
impl PyByteArray { 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. /// Creates a new Python bytearray object.
/// ///
/// The byte string is initialized by copying the data from the `&[u8]`. /// 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<F>(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. /// Creates a new Python `bytearray` object with an `init` closure to write its contents.
/// Before calling `init` the bytearray is zero-initialised. /// Before calling `init` the bytearray is zero-initialised.
/// * If Python raises a MemoryError on the allocation, `new_with` will return /// * 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 /// Creates a new Python `bytearray` object from another Python object that
/// implements the buffer protocol. /// implements the buffer protocol.
pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> { pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyByteArray>> {
@ -120,6 +87,39 @@ impl PyByteArray {
.downcast_into_unchecked() .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<F>(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. /// Gets the length of the bytearray.
#[inline] #[inline]
@ -300,7 +300,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
/// ///
/// # Safety /// # 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; fn data(&self) -> *mut u8;
/// Extracts a slice of the `ByteArray`'s entire buffer. /// Extracts a slice of the `ByteArray`'s entire buffer.
@ -311,7 +311,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
/// undefined. /// undefined.
/// ///
/// These mutations may occur in Python code as well as from Rust: /// 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. /// invalidate the slice.
/// - Actions like dropping objects or raising exceptions can invoke `__del__`methods or signal /// - 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 /// 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 /// # Safety
/// ///
/// Any other accesses of the `bytearray`'s buffer invalidate the slice. If it is used /// 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. /// apply to this function as well.
#[allow(clippy::mut_from_ref)] #[allow(clippy::mut_from_ref)]
unsafe fn as_bytes_mut(&self) -> &mut [u8]; 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`. /// Resizes the bytearray object to the new length `len`.
/// ///
/// Note that this will invalidate any pointers obtained by [PyByteArray::data], as well as /// Note that this will invalidate any pointers obtained by [PyByteArrayMethods::data], as well as
/// any (unsafe) slices obtained from [PyByteArray::as_bytes] and [PyByteArray::as_bytes_mut]. /// any (unsafe) slices obtained from [PyByteArrayMethods::as_bytes] and [PyByteArrayMethods::as_bytes_mut].
fn resize(&self, len: usize) -> PyResult<()>; fn resize(&self, len: usize) -> PyResult<()>;
} }

View File

@ -1,7 +1,9 @@
use crate::ffi_ptr_ext::FfiPtrExt; use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::{Borrowed, Bound}; use crate::instance::{Borrowed, Bound};
use crate::types::any::PyAnyMethods; 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::ops::Index;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::slice::SliceIndex; 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); pyobject_native_type_core!(PyBytes, pyobject_native_static_type_object!(ffi::PyBytes_Type), #checkfunction=ffi::PyBytes_Check);
impl PyBytes { 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. /// Creates a new Python bytestring object.
/// The bytestring is initialized by copying the data from the `&[u8]`. /// 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<F>(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. /// Creates a new Python `bytes` object with an `init` closure to write its contents.
/// Before calling `init` the bytes' contents are zero-initialised. /// Before calling `init` the bytes' contents are zero-initialised.
/// * If Python raises a MemoryError on the allocation, `new_with` will return /// * 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. /// Creates a new Python byte string object from a raw pointer and length.
/// ///
/// Panics if out of memory. /// Panics if out of memory.
@ -123,6 +89,42 @@ impl PyBytes {
.assume_owned(py) .assume_owned(py)
.downcast_into_unchecked() .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<F>(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. /// Gets the Python string as a byte slice.
#[inline] #[inline]
@ -172,6 +174,7 @@ impl Py<PyBytes> {
} }
/// This is the same way [Vec] is indexed. /// This is the same way [Vec] is indexed.
#[cfg(feature = "gil-refs")]
impl<I: SliceIndex<[u8]>> Index<I> for PyBytes { impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
type Output = I::Output; type Output = I::Output;

View File

@ -1,11 +1,12 @@
use crate::ffi_ptr_ext::FfiPtrExt; use crate::ffi_ptr_ext::FfiPtrExt;
use crate::py_result_ext::PyResultExt; 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::{Bound, Python};
use crate::{PyErr, PyResult}; use crate::{PyErr, PyResult};
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void}; use std::os::raw::{c_char, c_int, c_void};
/// Represents a Python Capsule /// Represents a Python Capsule
/// as described in [Capsules](https://docs.python.org/3/c-api/capsule.html#capsules): /// 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 /// > 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); pyobject_native_type_core!(PyCapsule, pyobject_native_static_type_object!(ffi::PyCapsule_Type), #checkfunction=ffi::PyCapsule_CheckExact);
impl PyCapsule { 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<T: 'static + Send + AssertNotZeroSized>(
py: Python<'_>,
value: T,
name: Option<CString>,
) -> PyResult<&Self> {
Self::new_bound(py, value, name).map(Bound::into_gil_ref)
}
/// Constructs a new capsule whose contents are `value`, associated with `name`. /// 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, /// `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"`. /// the name should be in the format `"modulename.attribute"`.
@ -99,24 +86,6 @@ impl PyCapsule {
Self::new_bound_with_destructor(py, value, name, |_, _| {}) 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<CString>,
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`. /// 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, /// 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::<T>()) Ok(&*ptr.cast::<T>())
} }
} }
}
#[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<T: 'static + Send + AssertNotZeroSized>(
py: Python<'_>,
value: T,
name: Option<CString>,
) -> 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<CString>,
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. /// Sets the context pointer in the capsule.
/// ///

View File

@ -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; use std::os::raw::c_double;
/// Represents a Python [`complex`](https://docs.python.org/3/library/functions.html#complex) object. /// Represents a Python [`complex`](https://docs.python.org/3/library/functions.html#complex) object.
@ -19,15 +21,6 @@ pyobject_native_type!(
); );
impl PyComplex { 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. /// Creates a new `PyComplex` from the given real and imaginary values.
pub fn from_doubles_bound( pub fn from_doubles_bound(
py: Python<'_>, py: Python<'_>,
@ -41,6 +34,19 @@ impl PyComplex {
.downcast_into_unchecked() .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. /// Returns the real part of the complex number.
pub fn real(&self) -> c_double { pub fn real(&self) -> c_double {
self.as_borrowed().real() self.as_borrowed().real()

View File

@ -22,6 +22,7 @@ use crate::ffi::{
PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND, PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND,
}; };
use crate::ffi_ptr_ext::FfiPtrExt; use crate::ffi_ptr_ext::FfiPtrExt;
#[cfg(feature = "gil-refs")]
use crate::instance::PyNativeType; use crate::instance::PyNativeType;
use crate::py_result_ext::PyResultExt; use crate::py_result_ext::PyResultExt;
use crate::types::any::PyAnyMethods; use crate::types::any::PyAnyMethods;
@ -249,6 +250,7 @@ impl PyDate {
} }
} }
#[cfg(feature = "gil-refs")]
impl PyDateAccess for PyDate { impl PyDateAccess for PyDate {
fn get_year(&self) -> i32 { fn get_year(&self) -> i32 {
self.as_borrowed().get_year() self.as_borrowed().get_year()
@ -461,6 +463,7 @@ impl PyDateTime {
} }
} }
#[cfg(feature = "gil-refs")]
impl PyDateAccess for PyDateTime { impl PyDateAccess for PyDateTime {
fn get_year(&self) -> i32 { fn get_year(&self) -> i32 {
self.as_borrowed().get_year() self.as_borrowed().get_year()
@ -489,6 +492,7 @@ impl PyDateAccess for Bound<'_, PyDateTime> {
} }
} }
#[cfg(feature = "gil-refs")]
impl PyTimeAccess for PyDateTime { impl PyTimeAccess for PyDateTime {
fn get_hour(&self) -> u8 { fn get_hour(&self) -> u8 {
self.as_borrowed().get_hour() 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 { impl<'py> PyTzInfoAccess<'py> for &'py PyDateTime {
fn get_tzinfo_bound(&self) -> Option<Bound<'py, PyTzInfo>> { fn get_tzinfo_bound(&self) -> Option<Bound<'py, PyTzInfo>> {
self.as_borrowed().get_tzinfo_bound() self.as_borrowed().get_tzinfo_bound()
@ -688,6 +693,7 @@ impl PyTime {
} }
} }
#[cfg(feature = "gil-refs")]
impl PyTimeAccess for PyTime { impl PyTimeAccess for PyTime {
fn get_hour(&self) -> u8 { fn get_hour(&self) -> u8 {
self.as_borrowed().get_hour() 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 { impl<'py> PyTzInfoAccess<'py> for &'py PyTime {
fn get_tzinfo_bound(&self) -> Option<Bound<'py, PyTzInfo>> { fn get_tzinfo_bound(&self) -> Option<Bound<'py, PyTzInfo>> {
self.as_borrowed().get_tzinfo_bound() self.as_borrowed().get_tzinfo_bound()
@ -878,6 +885,7 @@ impl PyDelta {
} }
} }
#[cfg(feature = "gil-refs")]
impl PyDeltaAccess for PyDelta { impl PyDeltaAccess for PyDelta {
fn get_days(&self) -> i32 { fn get_days(&self) -> i32 {
self.as_borrowed().get_days() self.as_borrowed().get_days()

View File

@ -1,13 +1,14 @@
use super::any::PyAnyMethods;
#[cfg(feature = "experimental-inspect")] #[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo; use crate::inspect::types::TypeInfo;
#[cfg(feature = "gil-refs")]
use crate::PyNativeType;
use crate::{ use crate::{
ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject,
PyObject, PyResult, Python, ToPyObject, PyResult, Python, ToPyObject,
}; };
use std::os::raw::c_double; use std::os::raw::c_double;
use super::any::PyAnyMethods;
/// Represents a Python `float` object. /// Represents a Python `float` object.
/// ///
/// You can usually avoid directly working with this type /// You can usually avoid directly working with this type
@ -24,17 +25,6 @@ pyobject_native_type!(
); );
impl PyFloat { 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. /// Creates a new Python `float` object.
pub fn new_bound(py: Python<'_>, val: c_double) -> Bound<'_, PyFloat> { pub fn new_bound(py: Python<'_>, val: c_double) -> Bound<'_, PyFloat> {
unsafe { unsafe {
@ -43,6 +33,19 @@ impl PyFloat {
.downcast_into_unchecked() .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. /// Gets the value of this float.
pub fn value(&self) -> c_double { pub fn value(&self) -> c_double {

View File

@ -2,7 +2,9 @@ use crate::err::{PyErr, PyResult};
use crate::ffi; use crate::ffi;
use crate::ffi_ptr_ext::FfiPtrExt; use crate::ffi_ptr_ext::FfiPtrExt;
use crate::types::any::PyAnyMethods; 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`. /// Represents a Python `slice`.
/// ///
@ -17,7 +19,7 @@ pyobject_native_type!(
#checkfunction=ffi::PySlice_Check #checkfunction=ffi::PySlice_Check
); );
/// Return value from [`PySlice::indices`]. /// Return value from [`PySliceMethods::indices`].
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct PySliceIndices { pub struct PySliceIndices {
/// Start of the slice /// Start of the slice
@ -47,16 +49,6 @@ impl PySliceIndices {
} }
impl PySlice { 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. /// Constructs a new slice with the given elements.
pub fn new_bound(py: Python<'_>, start: isize, stop: isize, step: isize) -> Bound<'_, PySlice> { pub fn new_bound(py: Python<'_>, start: isize, stop: isize, step: isize) -> Bound<'_, PySlice> {
unsafe { 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 `::`. /// Constructs a new full slice that is equivalent to `::`.
pub fn full_bound(py: Python<'_>) -> Bound<'_, PySlice> { pub fn full_bound(py: Python<'_>) -> Bound<'_, PySlice> {
unsafe { unsafe {
@ -88,6 +70,27 @@ impl PySlice {
.downcast_into_unchecked() .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, /// Retrieves the start, stop, and step indices from the slice object,
/// assuming a sequence of length `length`, and stores the length of the /// assuming a sequence of length `length`, and stores the length of the

View File

@ -1,7 +1,8 @@
use crate::err::{error_on_minusone, PyResult}; use crate::err::{error_on_minusone, PyResult};
use crate::types::{any::PyAnyMethods, string::PyStringMethods, PyString}; use crate::types::{any::PyAnyMethods, string::PyStringMethods, PyString};
use crate::{ffi, Bound}; #[cfg(feature = "gil-refs")]
use crate::{PyAny, PyNativeType}; use crate::PyNativeType;
use crate::{ffi, Bound, PyAny};
/// Represents a Python traceback. /// Represents a Python traceback.
#[repr(transparent)] #[repr(transparent)]
@ -13,6 +14,7 @@ pyobject_native_type_core!(
#checkfunction=ffi::PyTraceBack_Check #checkfunction=ffi::PyTraceBack_Check
); );
#[cfg(feature = "gil-refs")]
impl PyTraceback { impl PyTraceback {
/// Formats the traceback as a string. /// Formats the traceback as a string.
/// ///

View File

@ -1,11 +1,12 @@
use crate::err::{self, PyResult}; use crate::err::{self, PyResult};
use crate::instance::Borrowed; use crate::instance::Borrowed;
use crate::types::any::PyAnyMethods; 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; use std::borrow::Cow;
#[cfg(not(any(Py_LIMITED_API, PyPy)))] #[cfg(not(any(Py_LIMITED_API, PyPy)))]
use std::ffi::CStr; use std::ffi::CStr;
/// Represents a reference to a Python `type object`. /// Represents a reference to a Python `type object`.
#[repr(transparent)] #[repr(transparent)]
pub struct PyType(PyAny); 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); pyobject_native_type_core!(PyType, pyobject_native_static_type_object!(ffi::PyType_Type), #checkfunction=ffi::PyType_Check);
impl PyType { 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<T: PyTypeInfo>(py: Python<'_>) -> &PyType {
T::type_object_bound(py).into_gil_ref()
}
/// Creates a new type object. /// Creates a new type object.
#[inline] #[inline]
pub fn new_bound<T: PyTypeInfo>(py: Python<'_>) -> Bound<'_, PyType> { pub fn new_bound<T: PyTypeInfo>(py: Python<'_>) -> Bound<'_, PyType> {
T::type_object_bound(py) 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<PyType>`, to use in safe code. /// Converts the given FFI pointer into `Bound<PyType>`, to use in safe code.
/// ///
/// The function creates a new reference from the given pointer, and returns /// The function creates a new reference from the given pointer, and returns
@ -67,6 +36,39 @@ impl PyType {
.downcast_unchecked() .downcast_unchecked()
.to_owned() .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<T: PyTypeInfo>(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`. /// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`.
pub fn qualname(&self) -> PyResult<String> { pub fn qualname(&self) -> PyResult<String> {