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

Implement PyTypeInfo for PyEllipsis, PyNone, and PyNotImplemented #3577

Merged
merged 1 commit into from
Nov 25, 2023
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
1 change: 1 addition & 0 deletions newsfragments/3577.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement `PyTypeInfo` for `PyEllipsis`, `PyNone` and `PyNotImplemented`.
1 change: 1 addition & 0 deletions newsfragments/3577.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate `Py::is_ellipsis` and `PyAny::is_ellipsis` in favour of `any.is(py.Ellipsis())`.
2 changes: 2 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ impl<T> Py<T> {
/// Returns whether the object is Ellipsis, e.g. `...`.
///
/// This is equivalent to the Python expression `self is ...`.
#[deprecated(since = "0.20.0", note = "use `.is(py.Ellipsis())` instead")]
pub fn is_ellipsis(&self) -> bool {
unsafe { ffi::Py_Ellipsis() == self.as_ptr() }
}
Expand Down Expand Up @@ -1474,6 +1475,7 @@ a = A()
}

#[test]
#[allow(deprecated)]
fn test_is_ellipsis() {
Python::with_gil(|py| {
let v = py
Expand Down
2 changes: 2 additions & 0 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ impl PyAny {
/// Returns whether the object is Ellipsis, e.g. `...`.
///
/// This is equivalent to the Python expression `self is ...`.
#[deprecated(since = "0.20.0", note = "use `.is(py.Ellipsis())` instead")]
pub fn is_ellipsis(&self) -> bool {
Py2::<PyAny>::borrowed_from_gil_ref(&self).is_ellipsis()
}
Expand Down Expand Up @@ -2557,6 +2558,7 @@ class SimpleClass:
}

#[test]
#[allow(deprecated)]
fn test_is_ellipsis() {
Python::with_gil(|py| {
let v = py
Expand Down
47 changes: 28 additions & 19 deletions src/types/ellipsis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ffi, PyAny, PyDowncastError, PyTryFrom, Python};
use crate::{ffi, PyAny, PyTypeInfo, Python};

/// Represents the Python `Ellipsis` object.
#[repr(transparent)]
Expand All @@ -15,39 +15,48 @@ impl PyEllipsis {
}
}

impl<'v> PyTryFrom<'v> for PyEllipsis {
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, crate::PyDowncastError<'v>> {
let value: &PyAny = value.into();
if unsafe { ffi::Py_Ellipsis() == value.as_ptr() } {
return unsafe { Ok(value.downcast_unchecked()) };
}
Err(PyDowncastError::new(value, "ellipsis"))
unsafe impl PyTypeInfo for PyEllipsis {
const NAME: &'static str = "ellipsis";

const MODULE: Option<&'static str> = None;

adamreichold marked this conversation as resolved.
Show resolved Hide resolved
type AsRefTarget = PyEllipsis;

fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) }
}

fn try_from_exact<V: Into<&'v PyAny>>(
value: V,
) -> Result<&'v Self, crate::PyDowncastError<'v>> {
value.into().downcast()
#[inline]
fn is_type_of(object: &PyAny) -> bool {
// ellipsis is not usable as a base type
Self::is_exact_type_of(object)
}

unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
let ptr = value.into() as *const _ as *const PyEllipsis;
&*ptr
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is(Self::get(object.py()))
}
}

#[cfg(test)]
mod tests {
use crate::types::{PyDict, PyEllipsis};
use crate::Python;
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>());
})
}

#[test]
fn test_ellipsis_type_object_consistent() {
Python::with_gil(|py| {
assert!(PyEllipsis::get(py)
.downcast::<PyEllipsis>()
.unwrap()
.is_ellipsis());
.get_type()
.is(PyEllipsis::type_object(py)));
})
}

Expand Down
49 changes: 33 additions & 16 deletions src/types/none.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ffi, IntoPy, PyAny, PyDowncastError, PyObject, PyTryFrom, Python, ToPyObject};
use crate::{ffi, IntoPy, PyAny, PyObject, PyTypeInfo, Python, ToPyObject};

/// Represents the Python `None` object.
#[repr(transparent)]
Expand All @@ -15,24 +15,26 @@ impl PyNone {
}
}

impl<'v> PyTryFrom<'v> for PyNone {
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, crate::PyDowncastError<'v>> {
let value: &PyAny = value.into();
if value.is_none() {
return unsafe { Ok(value.downcast_unchecked()) };
}
Err(PyDowncastError::new(value, "NoneType"))
unsafe impl PyTypeInfo for PyNone {
const NAME: &'static str = "NoneType";

const MODULE: Option<&'static str> = None;

type AsRefTarget = PyNone;

fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_None()) }
}

fn try_from_exact<V: Into<&'v PyAny>>(
value: V,
) -> Result<&'v Self, crate::PyDowncastError<'v>> {
value.into().downcast()
#[inline]
fn is_type_of(object: &PyAny) -> bool {
// NoneType is not usable as a base type
Self::is_exact_type_of(object)
}

unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
let ptr = value.into() as *const _ as *const PyNone;
&*ptr
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is(Self::get(object.py()))
}
}

Expand All @@ -53,7 +55,22 @@ impl IntoPy<PyObject> for () {
#[cfg(test)]
mod tests {
use crate::types::{PyDict, PyNone};
use crate::{IntoPy, PyObject, Python, ToPyObject};
use crate::{IntoPy, PyObject, PyTypeInfo, Python, ToPyObject};

#[test]
fn test_none_is_itself() {
Python::with_gil(|py| {
assert!(PyNone::get(py).is_instance_of::<PyNone>());
assert!(PyNone::get(py).is_exact_instance_of::<PyNone>());
})
}

#[test]
fn test_none_type_object_consistent() {
Python::with_gil(|py| {
assert!(PyNone::get(py).get_type().is(PyNone::type_object(py)));
})
}

#[test]
fn test_none_is_none() {
Expand Down
47 changes: 28 additions & 19 deletions src/types/notimplemented.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ffi, PyAny, PyDowncastError, PyTryFrom, Python};
use crate::{ffi, PyAny, PyTypeInfo, Python};

/// Represents the Python `NotImplemented` object.
#[repr(transparent)]
Expand All @@ -15,39 +15,48 @@ impl PyNotImplemented {
}
}

impl<'v> PyTryFrom<'v> for PyNotImplemented {
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, crate::PyDowncastError<'v>> {
let value: &PyAny = value.into();
if unsafe { ffi::Py_NotImplemented() == value.as_ptr() } {
return unsafe { Ok(value.downcast_unchecked()) };
}
Err(PyDowncastError::new(value, "NotImplementedType"))
unsafe impl PyTypeInfo for PyNotImplemented {
const NAME: &'static str = "NotImplementedType";

const MODULE: Option<&'static str> = None;

type AsRefTarget = PyNotImplemented;

fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) }
}

fn try_from_exact<V: Into<&'v PyAny>>(
value: V,
) -> Result<&'v Self, crate::PyDowncastError<'v>> {
value.into().downcast()
#[inline]
fn is_type_of(object: &PyAny) -> bool {
// NotImplementedType is not usable as a base type
Self::is_exact_type_of(object)
}

unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
let ptr = value.into() as *const _ as *const PyNotImplemented;
&*ptr
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is(Self::get(object.py()))
}
}

#[cfg(test)]
mod tests {
use crate::types::{PyDict, PyNotImplemented};
use crate::Python;
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>());
})
}

#[test]
fn test_notimplemented_type_object_consistent() {
Python::with_gil(|py| {
assert!(PyNotImplemented::get(py)
.downcast::<PyNotImplemented>()
.unwrap()
.is(py.NotImplemented()));
.get_type()
.is(PyNotImplemented::type_object(py)));
})
}

Expand Down
Loading