forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingletons.rs
More file actions
113 lines (95 loc) · 2.74 KB
/
singletons.rs
File metadata and controls
113 lines (95 loc) · 2.74 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use once_cell::sync::Lazy;
use super::{PyType, PyTypeRef};
use crate::{
atomic_func,
class::PyClassImpl,
convert::ToPyObject,
protocol::PyNumberMethods,
types::{AsNumber, Constructor},
Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
};
#[pyclass(module = false, name = "NoneType")]
#[derive(Debug)]
pub struct PyNone;
impl PyPayload for PyNone {
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
vm.ctx.types.none_type
}
}
// This allows a built-in function to not return a value, mapping to
// Python's behavior of returning `None` in this situation.
impl ToPyObject for () {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.none()
}
}
impl<T: ToPyObject> ToPyObject for Option<T> {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
match self {
Some(x) => x.to_pyobject(vm),
None => vm.ctx.none(),
}
}
}
impl Constructor for PyNone {
type Args = ();
fn py_new(_: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.none.clone().into())
}
}
#[pyclass(with(Constructor, AsNumber))]
impl PyNone {
#[pymethod(magic)]
fn repr(&self) -> String {
"None".to_owned()
}
#[pymethod(magic)]
fn bool(&self) -> bool {
false
}
}
impl AsNumber for PyNone {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
boolean: atomic_func!(|_number, _vm| Ok(false)),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}
#[pyclass(module = false, name = "NotImplementedType")]
#[derive(Debug)]
pub struct PyNotImplemented;
impl PyPayload for PyNotImplemented {
fn class(vm: &VirtualMachine) -> &'static Py<PyType> {
vm.ctx.types.not_implemented_type
}
}
impl Constructor for PyNotImplemented {
type Args = ();
fn py_new(_: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.not_implemented.clone().into())
}
}
#[pyclass(with(Constructor))]
impl PyNotImplemented {
// TODO: As per https://bugs.python.org/issue35712, using NotImplemented
// in boolean contexts will need to raise a DeprecationWarning in 3.9
// and, eventually, a TypeError.
#[pymethod(magic)]
fn bool(&self) -> bool {
true
}
#[pymethod(magic)]
fn repr(&self) -> String {
"NotImplemented".to_owned()
}
#[pymethod(magic)]
fn reduce(&self) -> String {
"NotImplemented".to_owned()
}
}
pub fn init(context: &Context) {
PyNone::extend_class(context, context.types.none_type);
PyNotImplemented::extend_class(context, context.types.not_implemented_type);
}