1use crate::ffi_ptr_ext::FfiPtrExt;
2use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyObject, PyTypeInfo, Python};
3#[allow(deprecated)]
4use crate::{IntoPy, ToPyObject};
5
6#[repr(transparent)]
11pub struct PyNone(PyAny);
12
13pyobject_native_type_named!(PyNone);
14
15impl PyNone {
16 #[inline]
18 pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyNone> {
19 unsafe { ffi::Py_None().assume_borrowed(py).downcast_unchecked() }
20 }
21
22 #[deprecated(since = "0.23.0", note = "renamed to `PyNone::get`")]
24 #[inline]
25 pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyNone> {
26 Self::get(py)
27 }
28}
29
30unsafe impl PyTypeInfo for PyNone {
31 const NAME: &'static str = "NoneType";
32
33 const MODULE: Option<&'static str> = None;
34
35 fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
36 unsafe { ffi::Py_TYPE(ffi::Py_None()) }
37 }
38
39 #[inline]
40 fn is_type_of(object: &Bound<'_, PyAny>) -> bool {
41 Self::is_exact_type_of(object)
43 }
44
45 #[inline]
46 fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
47 object.is(&**Self::get(object.py()))
48 }
49}
50
51#[allow(deprecated)]
53impl ToPyObject for () {
54 fn to_object(&self, py: Python<'_>) -> PyObject {
55 PyNone::get(py).into_py(py)
56 }
57}
58
59#[allow(deprecated)]
60impl IntoPy<PyObject> for () {
61 #[inline]
62 fn into_py(self, py: Python<'_>) -> PyObject {
63 PyNone::get(py).into_py(py)
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use crate::types::any::PyAnyMethods;
70 use crate::types::{PyDict, PyNone};
71 use crate::{PyObject, PyTypeInfo, Python};
72
73 #[test]
74 fn test_none_is_itself() {
75 Python::with_gil(|py| {
76 assert!(PyNone::get(py).is_instance_of::<PyNone>());
77 assert!(PyNone::get(py).is_exact_instance_of::<PyNone>());
78 })
79 }
80
81 #[test]
82 fn test_none_type_object_consistent() {
83 Python::with_gil(|py| {
84 assert!(PyNone::get(py).get_type().is(&PyNone::type_object(py)));
85 })
86 }
87
88 #[test]
89 fn test_none_is_none() {
90 Python::with_gil(|py| {
91 assert!(PyNone::get(py).downcast::<PyNone>().unwrap().is_none());
92 })
93 }
94
95 #[test]
96 #[allow(deprecated)]
97 fn test_unit_to_object_is_none() {
98 use crate::ToPyObject;
99 Python::with_gil(|py| {
100 assert!(().to_object(py).downcast_bound::<PyNone>(py).is_ok());
101 })
102 }
103
104 #[test]
105 #[allow(deprecated)]
106 fn test_unit_into_py_is_none() {
107 use crate::IntoPy;
108 Python::with_gil(|py| {
109 let obj: PyObject = ().into_py(py);
110 assert!(obj.downcast_bound::<PyNone>(py).is_ok());
111 })
112 }
113
114 #[test]
115 fn test_dict_is_not_none() {
116 Python::with_gil(|py| {
117 assert!(PyDict::new(py).downcast::<PyNone>().is_err());
118 })
119 }
120}