forked from Rust-related/RustPython
Add mappingproxy iterator method
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use super::objiter;
|
||||
use super::objstr::PyStringRef;
|
||||
use super::objtype::{self, PyClassRef};
|
||||
use crate::function::OptionalArg;
|
||||
@@ -78,6 +79,35 @@ impl PyMappingProxy {
|
||||
MappingProxyInner::Dict(obj) => vm._membership(obj.clone(), key),
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__iter__")]
|
||||
pub fn iter(&self, vm: &VirtualMachine) -> PyResult {
|
||||
match &self.mapping {
|
||||
MappingProxyInner::Dict(d) => objiter::get_iter(vm, d),
|
||||
MappingProxyInner::Class(_c) => Err(vm.new_type_error("Can't get iter".to_string())),
|
||||
}
|
||||
}
|
||||
#[pymethod]
|
||||
pub fn items(&self, vm: &VirtualMachine) -> PyResult {
|
||||
match &self.mapping {
|
||||
MappingProxyInner::Dict(d) => vm.call_method(d, "items", vec![]),
|
||||
MappingProxyInner::Class(_c) => Err(vm.new_type_error("Can't get iter".to_string())),
|
||||
}
|
||||
}
|
||||
#[pymethod]
|
||||
pub fn keys(&self, vm: &VirtualMachine) -> PyResult {
|
||||
match &self.mapping {
|
||||
MappingProxyInner::Dict(d) => vm.call_method(d, "keys", vec![]),
|
||||
MappingProxyInner::Class(_c) => Err(vm.new_type_error("Can't get iter".to_string())),
|
||||
}
|
||||
}
|
||||
#[pymethod]
|
||||
pub fn values(&self, vm: &VirtualMachine) -> PyResult {
|
||||
match &self.mapping {
|
||||
MappingProxyInner::Dict(d) => vm.call_method(d, "values", vec![]),
|
||||
MappingProxyInner::Class(_c) => Err(vm.new_type_error("Can't get iter".to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(context: &PyContext) {
|
||||
|
||||
Reference in New Issue
Block a user