Merge pull request #3901 from rimi0108/add-dict-mapping

Add a mapping property
This commit is contained in:
Jeong YunWon
2022-07-16 16:12:46 +09:00
committed by GitHub
3 changed files with 30 additions and 8 deletions

View File

@@ -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<Self>, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
zelf.dict().contains(key, vm)
}
#[pyproperty]
fn mapping(zelf: PyRef<Self>) -> 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<Self>) -> 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<Self>) -> PyMappingProxy {
PyMappingProxy::from(zelf.dict().clone())
}
}
impl Unconstructible for PyDictValues {}
impl AsSequence for PyDictValues {

View File

@@ -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<PyTypeRef> for PyMappingProxy {
fn from(dict: PyTypeRef) -> Self {
Self {
mapping: MappingProxyInner::Class(class),
mapping: MappingProxyInner::Class(dict),
}
}
}
impl From<PyDictRef> for PyMappingProxy {
fn from(dict: PyDictRef) -> Self {
Self {
mapping: MappingProxyInner::Mapping(ArgMapping::from_dict_exact(dict)),
}
}
}

View File

@@ -576,7 +576,7 @@ impl PyType {
#[pyproperty(magic)]
fn dict(zelf: PyRef<Self>) -> PyMappingProxy {
PyMappingProxy::new(zelf)
PyMappingProxy::from(zelf)
}
#[pyproperty(magic, setter)]