pyo3/types/
ellipsis.rs

1use crate::{
2    ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
3    Python,
4};
5
6/// Represents the Python `Ellipsis` object.
7///
8/// Values of this type are accessed via PyO3's smart pointers, e.g. as
9/// [`Py<PyEllipsis>`][crate::Py] or [`Bound<'py, PyEllipsis>`][Bound].
10#[repr(transparent)]
11pub struct PyEllipsis(PyAny);
12
13pyobject_native_type_named!(PyEllipsis);
14
15impl PyEllipsis {
16    /// Returns the `Ellipsis` object.
17    #[inline]
18    pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyEllipsis> {
19        // SAFETY: `Py_Ellipsis` is a global singleton which is known to be the ellipsis object
20        unsafe {
21            ffi::Py_Ellipsis()
22                .assume_borrowed_unchecked(py)
23                .cast_unchecked()
24        }
25    }
26}
27
28unsafe impl PyTypeInfo for PyEllipsis {
29    const NAME: &'static str = "ellipsis";
30
31    const MODULE: Option<&'static str> = None;
32
33    fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
34        unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) }
35    }
36
37    #[inline]
38    fn is_type_of(object: &Bound<'_, PyAny>) -> bool {
39        // ellipsis is not usable as a base type
40        Self::is_exact_type_of(object)
41    }
42
43    #[inline]
44    fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
45        object.is(&**Self::get(object.py()))
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use crate::types::any::PyAnyMethods;
52    use crate::types::{PyDict, PyEllipsis};
53    use crate::{PyTypeInfo, Python};
54
55    #[test]
56    fn test_ellipsis_is_itself() {
57        Python::attach(|py| {
58            assert!(PyEllipsis::get(py).is_instance_of::<PyEllipsis>());
59            assert!(PyEllipsis::get(py).is_exact_instance_of::<PyEllipsis>());
60        })
61    }
62
63    #[test]
64    fn test_ellipsis_type_object_consistent() {
65        Python::attach(|py| {
66            assert!(PyEllipsis::get(py)
67                .get_type()
68                .is(PyEllipsis::type_object(py)));
69        })
70    }
71
72    #[test]
73    fn test_dict_is_not_ellipsis() {
74        Python::attach(|py| {
75            assert!(PyDict::new(py).cast::<PyEllipsis>().is_err());
76        })
77    }
78}