Skip to content

Commit ceb7d68

Browse files
authored
Merge pull request #3901 from rimi0108/add-dict-mapping
Add a mapping property
2 parents 10327b5 + b81853f commit ceb7d68

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

vm/src/builtins/dict.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, PySet,
3-
PyStrRef, PyTupleRef, PyType, PyTypeRef,
2+
set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias,
3+
PyMappingProxy, PySet, PyStrRef, PyTupleRef, PyType, PyTypeRef,
44
};
55
use crate::{
66
builtins::{
@@ -1041,6 +1041,11 @@ impl PyDictKeys {
10411041
fn contains(zelf: PyRef<Self>, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
10421042
zelf.dict().contains(key, vm)
10431043
}
1044+
1045+
#[pyproperty]
1046+
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
1047+
PyMappingProxy::from(zelf.dict().clone())
1048+
}
10441049
}
10451050
impl Unconstructible for PyDictKeys {}
10461051

@@ -1090,6 +1095,10 @@ impl PyDictItems {
10901095
let found = PyDict::getitem(zelf.dict().clone(), key, vm)?;
10911096
vm.identical_or_equal(&found, &value)
10921097
}
1098+
#[pyproperty]
1099+
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
1100+
PyMappingProxy::from(zelf.dict().clone())
1101+
}
10931102
}
10941103
impl Unconstructible for PyDictItems {}
10951104

@@ -1118,7 +1127,12 @@ impl AsSequence for PyDictItems {
11181127
}
11191128

11201129
#[pyimpl(with(DictView, Constructor, Iterable, AsSequence))]
1121-
impl PyDictValues {}
1130+
impl PyDictValues {
1131+
#[pyproperty]
1132+
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
1133+
PyMappingProxy::from(zelf.dict().clone())
1134+
}
1135+
}
11221136
impl Unconstructible for PyDictValues {}
11231137

11241138
impl AsSequence for PyDictValues {

vm/src/builtins/mappingproxy.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyDict, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef};
1+
use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef};
22
use crate::{
33
class::PyClassImpl,
44
convert::ToPyObject,
@@ -26,10 +26,18 @@ impl PyPayload for PyMappingProxy {
2626
}
2727
}
2828

29-
impl PyMappingProxy {
30-
pub fn new(class: PyTypeRef) -> Self {
29+
impl From<PyTypeRef> for PyMappingProxy {
30+
fn from(dict: PyTypeRef) -> Self {
31+
Self {
32+
mapping: MappingProxyInner::Class(dict),
33+
}
34+
}
35+
}
36+
37+
impl From<PyDictRef> for PyMappingProxy {
38+
fn from(dict: PyDictRef) -> Self {
3139
Self {
32-
mapping: MappingProxyInner::Class(class),
40+
mapping: MappingProxyInner::Mapping(ArgMapping::from_dict_exact(dict)),
3341
}
3442
}
3543
}

vm/src/builtins/type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ impl PyType {
576576

577577
#[pyproperty(magic)]
578578
fn dict(zelf: PyRef<Self>) -> PyMappingProxy {
579-
PyMappingProxy::new(zelf)
579+
PyMappingProxy::from(zelf)
580580
}
581581

582582
#[pyproperty(magic, setter)]

0 commit comments

Comments
 (0)