pyo3/types/
notimplemented.rs1use crate::{
2 ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
3 Python,
4};
5
6#[repr(transparent)]
11pub struct PyNotImplemented(PyAny);
12
13pyobject_native_type_named!(PyNotImplemented);
14pyobject_native_type_extract!(PyNotImplemented);
15
16impl PyNotImplemented {
17 #[cfg(feature = "gil-refs")]
19 #[deprecated(
20 since = "0.21.0",
21 note = "`PyNotImplemented::get` will be replaced by `PyNotImplemented::get_bound` in a future PyO3 version"
22 )]
23 #[inline]
24 pub fn get(py: Python<'_>) -> &PyNotImplemented {
25 Self::get_bound(py).into_gil_ref()
26 }
27
28 #[inline]
30 pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyNotImplemented> {
31 unsafe {
32 ffi::Py_NotImplemented()
33 .assume_borrowed(py)
34 .downcast_unchecked()
35 }
36 }
37}
38
39unsafe impl PyTypeInfo for PyNotImplemented {
40 const NAME: &'static str = "NotImplementedType";
41 const MODULE: Option<&'static str> = None;
42
43 fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
44 unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) }
45 }
46
47 #[inline]
48 fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool {
49 Self::is_exact_type_of_bound(object)
51 }
52
53 #[inline]
54 fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool {
55 object.is(&**Self::get_bound(object.py()))
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use crate::types::any::PyAnyMethods;
62 use crate::types::{PyDict, PyNotImplemented};
63 use crate::{PyTypeInfo, Python};
64
65 #[test]
66 fn test_notimplemented_is_itself() {
67 Python::with_gil(|py| {
68 assert!(PyNotImplemented::get_bound(py).is_instance_of::<PyNotImplemented>());
69 assert!(PyNotImplemented::get_bound(py).is_exact_instance_of::<PyNotImplemented>());
70 })
71 }
72
73 #[test]
74 fn test_notimplemented_type_object_consistent() {
75 Python::with_gil(|py| {
76 assert!(PyNotImplemented::get_bound(py)
77 .get_type()
78 .is(&PyNotImplemented::type_object_bound(py)));
79 })
80 }
81
82 #[test]
83 fn test_dict_is_not_notimplemented() {
84 Python::with_gil(|py| {
85 assert!(PyDict::new_bound(py)
86 .downcast::<PyNotImplemented>()
87 .is_err());
88 })
89 }
90}