Merge pull request #3797 from snuderl/PyEllipsis-and-NotImplemented-get-bound-api

PyEllipsis and PyNotImplemented new get_bound api
This commit is contained in:
David Hewitt 2024-02-05 07:50:03 +00:00 committed by GitHub
commit c9c6f928a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 16 deletions

View File

@ -706,14 +706,14 @@ impl<'py> Python<'py> {
#[allow(non_snake_case)] // the Python keyword starts with uppercase
#[inline]
pub fn Ellipsis(self) -> PyObject {
PyEllipsis::get(self).into()
PyEllipsis::get_bound(self).into_py(self)
}
/// Gets the Python builtin value `NotImplemented`.
#[allow(non_snake_case)] // the Python keyword starts with uppercase
#[inline]
pub fn NotImplemented(self) -> PyObject {
PyNotImplemented::get(self).into()
PyNotImplemented::get_bound(self).into_py(self)
}
/// Gets the running Python interpreter version as a string.

View File

@ -1,4 +1,4 @@
use crate::{ffi, PyAny, PyTypeInfo, Python};
use crate::{ffi, ffi_ptr_ext::FfiPtrExt, Borrowed, PyAny, PyTypeInfo, Python};
/// Represents the Python `Ellipsis` object.
#[repr(transparent)]
@ -9,9 +9,22 @@ pyobject_native_type_extract!(PyEllipsis);
impl PyEllipsis {
/// Returns the `Ellipsis` object.
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyEllipsis::get` will be replaced by `PyEllipsis::get_bound` in a future PyO3 version"
)
)]
#[inline]
pub fn get(py: Python<'_>) -> &PyEllipsis {
unsafe { py.from_borrowed_ptr(ffi::Py_Ellipsis()) }
Self::get_bound(py).into_gil_ref()
}
/// Returns the `Ellipsis` object.
#[inline]
pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyEllipsis> {
unsafe { ffi::Py_Ellipsis().assume_borrowed(py).downcast_unchecked() }
}
}
@ -32,27 +45,28 @@ unsafe impl PyTypeInfo for PyEllipsis {
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is(Self::get(object.py()))
object.is(Self::get_bound(object.py()).as_ref())
}
}
#[cfg(test)]
mod tests {
use crate::types::any::PyAnyMethods;
use crate::types::{PyDict, PyEllipsis};
use crate::{PyTypeInfo, Python};
#[test]
fn test_ellipsis_is_itself() {
Python::with_gil(|py| {
assert!(PyEllipsis::get(py).is_instance_of::<PyEllipsis>());
assert!(PyEllipsis::get(py).is_exact_instance_of::<PyEllipsis>());
assert!(PyEllipsis::get_bound(py).is_instance_of::<PyEllipsis>());
assert!(PyEllipsis::get_bound(py).is_exact_instance_of::<PyEllipsis>());
})
}
#[test]
fn test_ellipsis_type_object_consistent() {
Python::with_gil(|py| {
assert!(PyEllipsis::get(py)
assert!(PyEllipsis::get_bound(py)
.get_type()
.is(PyEllipsis::type_object(py)));
})

View File

@ -15,12 +15,12 @@ impl PyNone {
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyNone::get` will be replaced by `PyBool::get_bound` in a future PyO3 version"
note = "`PyNone::get` will be replaced by `PyNone::get_bound` in a future PyO3 version"
)
)]
#[inline]
pub fn get(py: Python<'_>) -> &PyNone {
unsafe { py.from_borrowed_ptr(ffi::Py_None()) }
Self::get_bound(py).into_gil_ref()
}
/// Returns the `None` object.

View File

@ -1,4 +1,4 @@
use crate::{ffi, PyAny, PyTypeInfo, Python};
use crate::{ffi, ffi_ptr_ext::FfiPtrExt, Borrowed, PyAny, PyTypeInfo, Python};
/// Represents the Python `NotImplemented` object.
#[repr(transparent)]
@ -9,9 +9,26 @@ pyobject_native_type_extract!(PyNotImplemented);
impl PyNotImplemented {
/// Returns the `NotImplemented` object.
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyNotImplemented::get` will be replaced by `PyNotImplemented::get_bound` in a future PyO3 version"
)
)]
#[inline]
pub fn get(py: Python<'_>) -> &PyNotImplemented {
unsafe { py.from_borrowed_ptr(ffi::Py_NotImplemented()) }
Self::get_bound(py).into_gil_ref()
}
/// Returns the `NotImplemented` object.
#[inline]
pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyNotImplemented> {
unsafe {
ffi::Py_NotImplemented()
.assume_borrowed(py)
.downcast_unchecked()
}
}
}
@ -31,27 +48,28 @@ unsafe impl PyTypeInfo for PyNotImplemented {
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is(Self::get(object.py()))
object.is(Self::get_bound(object.py()).as_ref())
}
}
#[cfg(test)]
mod tests {
use crate::types::any::PyAnyMethods;
use crate::types::{PyDict, PyNotImplemented};
use crate::{PyTypeInfo, Python};
#[test]
fn test_notimplemented_is_itself() {
Python::with_gil(|py| {
assert!(PyNotImplemented::get(py).is_instance_of::<PyNotImplemented>());
assert!(PyNotImplemented::get(py).is_exact_instance_of::<PyNotImplemented>());
assert!(PyNotImplemented::get_bound(py).is_instance_of::<PyNotImplemented>());
assert!(PyNotImplemented::get_bound(py).is_exact_instance_of::<PyNotImplemented>());
})
}
#[test]
fn test_notimplemented_type_object_consistent() {
Python::with_gil(|py| {
assert!(PyNotImplemented::get(py)
assert!(PyNotImplemented::get_bound(py)
.get_type()
.is(PyNotImplemented::type_object(py)));
})