Skip to content

Commit 5d0163e

Browse files
Add mapping property to dict_keys
1 parent d3e8e47 commit 5d0163e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

vm/src/builtins/dict.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
22
set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, PySet,
3-
PyStrRef, PyTupleRef, PyType, PyTypeRef,
3+
PyStrRef, PyTupleRef, PyType, PyTypeRef, PyMappingProxy,
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

vm/src/builtins/mappingproxy.rs

Lines changed: 17 additions & 1 deletion
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, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef, PyDictRef};
22
use crate::{
33
class::PyClassImpl,
44
convert::ToPyObject,
@@ -34,6 +34,22 @@ impl PyMappingProxy {
3434
}
3535
}
3636

37+
impl From<PyTypeRef> for PyMappingProxy {
38+
fn from(dict: PyTypeRef) -> Self {
39+
Self {
40+
mapping: MappingProxyInner::Class(dict),
41+
}
42+
}
43+
}
44+
45+
impl From<PyDictRef> for PyMappingProxy {
46+
fn from(dict: PyDictRef) -> Self {
47+
Self {
48+
mapping: MappingProxyInner::Mapping(ArgMapping::from_dict_exact(dict)),
49+
}
50+
}
51+
}
52+
3753
impl Constructor for PyMappingProxy {
3854
type Args = PyObjectRef;
3955

0 commit comments

Comments
 (0)