Add __contains__ in dict_items

This commit is contained in:
Jack-R-lantern
2021-10-09 15:58:24 +09:00
parent 6676f2e9fa
commit cf11f5aeea

View File

@@ -3,6 +3,7 @@ use super::{
PyTypeRef,
};
use crate::{
builtins::PyTupleRef,
common::ascii,
dictdatatype::{self, DictKey},
function::{ArgIterable, FuncArgs, IntoPyObject, KwArgs, OptionalArg},
@@ -926,6 +927,24 @@ impl PyDictItems {
let inner = zelf.symmetric_difference(other, vm)?;
Ok(PySet { inner })
}
#[pymethod(magic)]
fn contains(zelf: PyRef<Self>, needle: PyTupleRef, vm: &VirtualMachine) -> PyResult<bool> {
if needle.len() != 2 {
return Ok(false);
}
let key = needle.fast_getitem(0);
let found = zelf.dict().contains(key.clone(), vm).unwrap();
if found == false {
return Ok(false);
}
let value = needle.fast_getitem(1);
let found = PyDict::getitem(zelf.dict().clone(), key, vm).unwrap();
if !vm.identical_or_equal(&found, &value)? {
return Ok(false);
}
Ok(true)
}
}
pub(crate) fn init(context: &PyContext) {