forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmappingproxy.rs
More file actions
144 lines (133 loc) · 4.45 KB
/
mappingproxy.rs
File metadata and controls
144 lines (133 loc) · 4.45 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use super::dict::PyDict;
use super::pystr::PyStrRef;
use super::pytype::PyTypeRef;
use crate::function::OptionalArg;
use crate::iterator;
use crate::slots::Iterable;
use crate::vm::VirtualMachine;
use crate::{
IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
TryFromObject,
};
#[pyclass(module = false, name = "mappingproxy")]
#[derive(Debug)]
pub struct PyMappingProxy {
mapping: MappingProxyInner,
}
#[derive(Debug)]
enum MappingProxyInner {
Class(PyTypeRef),
Dict(PyObjectRef),
}
impl PyValue for PyMappingProxy {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.mappingproxy_type
}
}
impl PyMappingProxy {
pub fn new(class: PyTypeRef) -> Self {
Self {
mapping: MappingProxyInner::Class(class),
}
}
}
#[pyimpl(with(Iterable))]
impl PyMappingProxy {
#[pyslot]
fn tp_new(cls: PyTypeRef, mapping: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
Self {
mapping: MappingProxyInner::Dict(mapping),
}
.into_ref_with_type(vm, cls)
}
fn get_inner(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
let opt = match &self.mapping {
MappingProxyInner::Class(class) => {
let key = PyStrRef::try_from_object(vm, key)?;
class.get_attr(key.as_str())
}
MappingProxyInner::Dict(obj) => obj.get_item(key, vm).ok(),
};
Ok(opt)
}
#[pymethod]
fn get(
&self,
key: PyObjectRef,
default: OptionalArg,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
let default = default.into_option();
let value = self.get_inner(key, vm)?.or(default);
Ok(value)
}
#[pymethod(name = "__getitem__")]
pub fn getitem(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.get_inner(key.clone(), vm)?
.ok_or_else(|| vm.new_key_error(key))
}
#[pymethod(name = "__contains__")]
pub fn contains(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
match &self.mapping {
MappingProxyInner::Class(class) => {
let key = PyStrRef::try_from_object(vm, key)?;
Ok(vm.ctx.new_bool(class.has_attr(key.as_str())))
}
MappingProxyInner::Dict(obj) => vm._membership(obj.clone(), key),
}
}
#[pymethod]
pub fn items(&self, vm: &VirtualMachine) -> PyResult {
let obj = match &self.mapping {
MappingProxyInner::Dict(d) => d.clone(),
MappingProxyInner::Class(c) => {
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
vm.call_method(&obj, "items", ())
}
#[pymethod]
pub fn keys(&self, vm: &VirtualMachine) -> PyResult {
let obj = match &self.mapping {
MappingProxyInner::Dict(d) => d.clone(),
MappingProxyInner::Class(c) => {
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
vm.call_method(&obj, "keys", ())
}
#[pymethod]
pub fn values(&self, vm: &VirtualMachine) -> PyResult {
let obj = match &self.mapping {
MappingProxyInner::Dict(d) => d.clone(),
MappingProxyInner::Class(c) => {
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
vm.call_method(&obj, "values", ())
}
#[pymethod]
pub fn copy(&self, vm: &VirtualMachine) -> PyResult {
match &self.mapping {
MappingProxyInner::Dict(d) => vm.call_method(d, "copy", ()),
MappingProxyInner::Class(c) => {
Ok(PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm))
}
}
}
}
impl Iterable for PyMappingProxy {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let obj = match &zelf.mapping {
MappingProxyInner::Dict(d) => d.clone(),
MappingProxyInner::Class(c) => {
// TODO: something that's much more efficient than this
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
iterator::get_iter(vm, obj)
}
}
pub fn init(context: &PyContext) {
PyMappingProxy::extend_class(context, &context.types.mappingproxy_type)
}