diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 4bcf68f96..9072acd9e 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -1,6 +1,6 @@ use super::{ - set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, PySet, - PyStrRef, PyTupleRef, PyType, PyTypeRef, + set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, + PyMappingProxy, PySet, PyStrRef, PyTupleRef, PyType, PyTypeRef, }; use crate::{ builtins::{ @@ -1041,6 +1041,11 @@ impl PyDictKeys { fn contains(zelf: PyRef, key: PyObjectRef, vm: &VirtualMachine) -> PyResult { zelf.dict().contains(key, vm) } + + #[pyproperty] + fn mapping(zelf: PyRef) -> PyMappingProxy { + PyMappingProxy::from(zelf.dict().clone()) + } } impl Unconstructible for PyDictKeys {} @@ -1090,6 +1095,10 @@ impl PyDictItems { let found = PyDict::getitem(zelf.dict().clone(), key, vm)?; vm.identical_or_equal(&found, &value) } + #[pyproperty] + fn mapping(zelf: PyRef) -> PyMappingProxy { + PyMappingProxy::from(zelf.dict().clone()) + } } impl Unconstructible for PyDictItems {} @@ -1118,7 +1127,12 @@ impl AsSequence for PyDictItems { } #[pyimpl(with(DictView, Constructor, Iterable, AsSequence))] -impl PyDictValues {} +impl PyDictValues { + #[pyproperty] + fn mapping(zelf: PyRef) -> PyMappingProxy { + PyMappingProxy::from(zelf.dict().clone()) + } +} impl Unconstructible for PyDictValues {} impl AsSequence for PyDictValues { diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index cd526ce7d..ba80da043 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -1,4 +1,4 @@ -use super::{PyDict, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef}; +use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef}; use crate::{ class::PyClassImpl, convert::ToPyObject, @@ -26,10 +26,18 @@ impl PyPayload for PyMappingProxy { } } -impl PyMappingProxy { - pub fn new(class: PyTypeRef) -> Self { +impl From for PyMappingProxy { + fn from(dict: PyTypeRef) -> Self { Self { - mapping: MappingProxyInner::Class(class), + mapping: MappingProxyInner::Class(dict), + } + } +} + +impl From for PyMappingProxy { + fn from(dict: PyDictRef) -> Self { + Self { + mapping: MappingProxyInner::Mapping(ArgMapping::from_dict_exact(dict)), } } } diff --git a/vm/src/builtins/type.rs b/vm/src/builtins/type.rs index 12de79545..2ac9b0740 100644 --- a/vm/src/builtins/type.rs +++ b/vm/src/builtins/type.rs @@ -576,7 +576,7 @@ impl PyType { #[pyproperty(magic)] fn dict(zelf: PyRef) -> PyMappingProxy { - PyMappingProxy::new(zelf) + PyMappingProxy::from(zelf) } #[pyproperty(magic, setter)]