Add mappingproxy iterator method

This commit is contained in:
coolreader18
2019-10-24 22:48:22 -05:00
committed by Noah
parent 0297189af4
commit f58218e13d

View File

@@ -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) {