Merge pull request #3577 from davidhewitt/none-typeinfo
Implement `PyTypeInfo` for `PyEllipsis`, `PyNone`, and `PyNotImplemented`
This commit is contained in:
commit
81bc838acd
1
newsfragments/3577.added.md
Normal file
1
newsfragments/3577.added.md
Normal file
|
@ -0,0 +1 @@
|
|||
Implement `PyTypeInfo` for `PyEllipsis`, `PyNone` and `PyNotImplemented`.
|
1
newsfragments/3577.changed.md
Normal file
1
newsfragments/3577.changed.md
Normal file
|
@ -0,0 +1 @@
|
|||
Deprecate `Py::is_ellipsis` and `PyAny::is_ellipsis` in favour of `any.is(py.Ellipsis())`.
|
|
@ -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() }
|
||||
}
|
||||
|
@ -1474,6 +1475,7 @@ a = A()
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn test_is_ellipsis() {
|
||||
Python::with_gil(|py| {
|
||||
let v = py
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
@ -2557,6 +2558,7 @@ class SimpleClass:
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn test_is_ellipsis() {
|
||||
Python::with_gil(|py| {
|
||||
let v = py
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{ffi, PyAny, PyDowncastError, PyTryFrom, Python};
|
||||
use crate::{ffi, PyAny, PyTypeInfo, Python};
|
||||
|
||||
/// Represents the Python `Ellipsis` object.
|
||||
#[repr(transparent)]
|
||||
|
@ -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;
|
||||
|
||||
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)));
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)]
|
||||
|
@ -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()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{ffi, PyAny, PyDowncastError, PyTryFrom, Python};
|
||||
use crate::{ffi, PyAny, PyTypeInfo, Python};
|
||||
|
||||
/// Represents the Python `NotImplemented` object.
|
||||
#[repr(transparent)]
|
||||
|
@ -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)));
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue