From 5d0163e6c4452aa6705605ad46864085ab777b42 Mon Sep 17 00:00:00 2001 From: seryoungshim17 Date: Fri, 15 Jul 2022 16:54:33 +0900 Subject: [PATCH 1/3] Add mapping property to dict_keys --- vm/src/builtins/dict.rs | 7 ++++++- vm/src/builtins/mappingproxy.rs | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 4bcf68f96..fd8e1e9c0 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, + PyStrRef, PyTupleRef, PyType, PyTypeRef, PyMappingProxy, }; 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 {} diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index cd526ce7d..2dee5f6e9 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, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef, PyDictRef}; use crate::{ class::PyClassImpl, convert::ToPyObject, @@ -34,6 +34,22 @@ impl PyMappingProxy { } } +impl From for PyMappingProxy { + fn from(dict: PyTypeRef) -> Self { + Self { + mapping: MappingProxyInner::Class(dict), + } + } +} + +impl From for PyMappingProxy { + fn from(dict: PyDictRef) -> Self { + Self { + mapping: MappingProxyInner::Mapping(ArgMapping::from_dict_exact(dict)), + } + } +} + impl Constructor for PyMappingProxy { type Args = PyObjectRef; From f9a17b0e545e7b79adb30471909f2db82f52ad8a Mon Sep 17 00:00:00 2001 From: hyerim Date: Sat, 16 Jul 2022 14:25:29 +0900 Subject: [PATCH 2/3] Add: pyproperty dict values, items mapping --- vm/src/builtins/dict.rs | 15 ++++++++++++--- vm/src/builtins/mappingproxy.rs | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index fd8e1e9c0..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, PyMappingProxy, + set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, + PyMappingProxy, PySet, PyStrRef, PyTupleRef, PyType, PyTypeRef, }; use crate::{ builtins::{ @@ -1095,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 {} @@ -1123,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 2dee5f6e9..bf77c1ed7 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, PyDictRef}; +use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef}; use crate::{ class::PyClassImpl, convert::ToPyObject, @@ -44,7 +44,7 @@ impl From for PyMappingProxy { impl From for PyMappingProxy { fn from(dict: PyDictRef) -> Self { - Self { + Self { mapping: MappingProxyInner::Mapping(ArgMapping::from_dict_exact(dict)), } } From b81853fa717ddc8f8499961febe97ac25bcd1d47 Mon Sep 17 00:00:00 2001 From: hyerim Date: Sat, 16 Jul 2022 15:39:12 +0900 Subject: [PATCH 3/3] Fix: delete PyMappingProxy new function, fix new to from function --- vm/src/builtins/mappingproxy.rs | 8 -------- vm/src/builtins/type.rs | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index bf77c1ed7..ba80da043 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -26,14 +26,6 @@ impl PyPayload for PyMappingProxy { } } -impl PyMappingProxy { - pub fn new(class: PyTypeRef) -> Self { - Self { - mapping: MappingProxyInner::Class(class), - } - } -} - impl From for PyMappingProxy { fn from(dict: PyTypeRef) -> Self { Self { diff --git a/vm/src/builtins/type.rs b/vm/src/builtins/type.rs index d4e09f8c2..5bbeb67a2 100644 --- a/vm/src/builtins/type.rs +++ b/vm/src/builtins/type.rs @@ -577,7 +577,7 @@ impl PyType { #[pyproperty(magic)] fn dict(zelf: PyRef) -> PyMappingProxy { - PyMappingProxy::new(zelf) + PyMappingProxy::from(zelf) } #[pyproperty(magic, setter)]