Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyEllipsis and PyNotImplemented new get_bound api #3797

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 20 additions & 6 deletions src/types/ellipsis.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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() }
}
}

Expand All @@ -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)));
})
Expand Down
4 changes: 2 additions & 2 deletions src/types/none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch! 👍

)
)]
#[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.
Expand Down
30 changes: 24 additions & 6 deletions src/types/notimplemented.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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()
}
}
}

Expand All @@ -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)));
})
Expand Down
Loading