forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapping.rs
More file actions
173 lines (153 loc) · 5.07 KB
/
mapping.rs
File metadata and controls
173 lines (153 loc) · 5.07 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{
builtins::{
dict::{PyDictItems, PyDictKeys, PyDictValues},
PyDict,
},
common::lock::OnceCell,
function::IntoPyResult,
AsPyObject, PyObject, PyObjectRef, PyResult, VirtualMachine,
};
// Mapping protocol
// https://docs.python.org/3/c-api/mapping.html
#[allow(clippy::type_complexity)]
#[derive(Default, Copy, Clone)]
pub struct PyMappingMethods {
pub length: Option<fn(&PyMapping, &VirtualMachine) -> PyResult<usize>>,
pub subscript: Option<fn(&PyMapping, &PyObject, &VirtualMachine) -> PyResult>,
pub ass_subscript:
Option<fn(&PyMapping, &PyObject, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>>,
}
#[derive(Clone)]
pub struct PyMapping<'a> {
pub obj: &'a PyObject,
methods: OnceCell<PyMappingMethods>,
}
impl<'a> From<&'a PyObject> for PyMapping<'a> {
fn from(obj: &'a PyObject) -> Self {
Self {
obj,
methods: OnceCell::new(),
}
}
}
impl AsRef<PyObject> for PyMapping<'_> {
fn as_ref(&self) -> &PyObject {
self.obj
}
}
impl<'a> PyMapping<'a> {
pub fn with_methods(obj: &'a PyObject, methods: PyMappingMethods) -> Self {
Self {
obj,
methods: OnceCell::from(methods),
}
}
pub fn try_protocol(obj: &'a PyObject, vm: &VirtualMachine) -> PyResult<Self> {
let zelf = Self::from(obj);
if zelf.check(vm) {
Ok(zelf)
} else {
Err(vm.new_type_error(format!("{} is not a mapping object", zelf.obj.class())))
}
}
}
impl PyMapping<'_> {
// PyMapping::Check
pub fn check(&self, vm: &VirtualMachine) -> bool {
self.methods(vm).subscript.is_some()
}
pub fn methods(&self, vm: &VirtualMachine) -> &PyMappingMethods {
self.methods.get_or_init(|| {
if let Some(f) = self
.obj
.class()
.mro_find_map(|cls| cls.slots.as_mapping.load())
{
f(self.obj, vm)
} else {
PyMappingMethods::default()
}
})
}
pub fn length_opt(&self, vm: &VirtualMachine) -> Option<PyResult<usize>> {
self.methods(vm).length.map(|f| f(self, vm))
}
pub fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
self.length_opt(vm).ok_or_else(|| {
vm.new_type_error(format!(
"object of type '{}' has no len() or not a mapping",
self.obj.class()
))
})?
}
pub fn subscript(&self, needle: &impl AsPyObject, vm: &VirtualMachine) -> PyResult {
self._subscript(needle.as_object(), vm)
}
pub fn ass_subscript(
&self,
needle: &impl AsPyObject,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
self._ass_subscript(needle.as_object(), value, vm)
}
fn _subscript(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
let f = self
.methods(vm)
.subscript
.ok_or_else(|| vm.new_type_error(format!("{} is not a mapping", self.obj.class())))?;
f(self, needle, vm)
}
fn _ass_subscript(
&self,
needle: &PyObject,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
let f = self.methods(vm).ass_subscript.ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object does not support item assignment",
self.obj.class()
))
})?;
f(self, needle, value, vm)
}
pub fn keys(&self, vm: &VirtualMachine) -> PyResult {
if let Some(dict) = self.obj.downcast_ref_if_exact::<PyDict>(vm) {
PyDictKeys::new(dict.to_owned()).into_pyresult(vm)
} else {
self.method_output_as_list("keys", vm)
}
}
pub fn values(&self, vm: &VirtualMachine) -> PyResult {
if let Some(dict) = self.obj.downcast_ref_if_exact::<PyDict>(vm) {
PyDictValues::new(dict.to_owned()).into_pyresult(vm)
} else {
self.method_output_as_list("values", vm)
}
}
pub fn items(&self, vm: &VirtualMachine) -> PyResult {
if let Some(dict) = self.obj.downcast_ref_if_exact::<PyDict>(vm) {
PyDictItems::new(dict.to_owned()).into_pyresult(vm)
} else {
self.method_output_as_list("items", vm)
}
}
fn method_output_as_list(&self, method_name: &str, vm: &VirtualMachine) -> PyResult {
let meth_output = vm.call_method(self.obj.to_owned(), method_name, ())?;
if meth_output.is(&vm.ctx.types.list_type) {
return Ok(meth_output);
}
let iter = meth_output.clone().get_iter(vm).map_err(|_| {
vm.new_type_error(format!(
"{}.{}() returned a non-iterable (type {})",
self.obj.class(),
method_name,
meth_output.class()
))
})?;
// TODO
// PySequence::from(&iter).list(vm).map(|x| x.into())
vm.ctx.new_list(iter.try_to_value(vm)?).into_pyresult(vm)
}
}