/
notimplemented.rs
62 lines (50 loc) · 1.58 KB
/
notimplemented.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::{ffi, PyAny, PyTypeInfo, Python};
/// Represents the Python `NotImplemented` object.
#[repr(transparent)]
pub struct PyNotImplemented(PyAny);
pyobject_native_type_named!(PyNotImplemented);
pyobject_native_type_extract!(PyNotImplemented);
impl PyNotImplemented {
/// Returns the `NotImplemented` object.
#[inline]
pub fn get(py: Python<'_>) -> &PyNotImplemented {
unsafe { py.from_borrowed_ptr(ffi::Py_NotImplemented()) }
}
}
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()) }
}
#[inline]
fn is_type_of(object: &PyAny) -> bool {
// NoneType is not usable as a base type
Self::is_exact_type_of(object)
}
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is(PyNotImplemented::get(object.py()))
}
}
#[cfg(test)]
mod tests {
use crate::types::{PyDict, PyNotImplemented};
use crate::Python;
#[test]
fn test_notimplemented_is_itself() {
Python::with_gil(|py| {
assert!(PyNotImplemented::get(py)
.downcast::<PyNotImplemented>()
.unwrap()
.is(&py.NotImplemented()));
})
}
#[test]
fn test_dict_is_not_notimplemented() {
Python::with_gil(|py| {
assert!(PyDict::new(py).downcast::<PyNotImplemented>().is_err());
})
}
}