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
use super::objstr::PyStringRef; use super::objtype::{self, PyClassRef}; use crate::pyobject::{PyClassImpl, PyContext, PyRef, PyResult, PyValue}; use crate::vm::VirtualMachine; #[pyclass] #[derive(Debug)] pub struct PyMappingProxy { class: PyClassRef, } pub type PyMappingProxyRef = PyRef<PyMappingProxy>; impl PyValue for PyMappingProxy { fn class(vm: &VirtualMachine) -> PyClassRef { vm.ctx.types.mappingproxy_type.clone() } } #[pyimpl] impl PyMappingProxy { pub fn new(class: PyClassRef) -> PyMappingProxy { PyMappingProxy { class } } #[pymethod(name = "__getitem__")] pub fn getitem(&self, key: PyStringRef, vm: &VirtualMachine) -> PyResult { if let Some(value) = objtype::class_get_attr(&self.class, key.as_str()) { return Ok(value); } Err(vm.new_key_error(key.into_object())) } #[pymethod(name = "__contains__")] pub fn contains(&self, attr: PyStringRef, _vm: &VirtualMachine) -> bool { objtype::class_has_attr(&self.class, attr.as_str()) } } pub fn init(context: &PyContext) { PyMappingProxy::extend_class(context, &context.types.mappingproxy_type) }