Merge pull request #3259 from Jack-R-lantern/__contains__

Add contains in dict_items
This commit is contained in:
Jeong YunWon
2021-10-10 14:01:11 +09:00
committed by GitHub
3 changed files with 32 additions and 2 deletions

View File

@@ -1300,8 +1300,6 @@ class DictTest(unittest.TestCase):
except RuntimeError: # implementation defined
pass
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_dictitems_contains_use_after_free(self):
class X:
def __eq__(self, other):

View File

@@ -26,3 +26,14 @@ with assert_raises(StopIteration):
next(dict_reversed)
assert 'dict' in dict().__doc__
d = {'a': 123, 'b': 456}
assert 1 not in d.items()
assert 'a' not in d.items()
assert 'a', 123 not in d.items()
assert () not in d.items()
assert (1) not in d.items()
assert ('a') not in d.items()
assert ('a', 123) in d.items()
assert ('b', 456) in d.items()
assert ('a', 123, 3) not in d.items()
assert ('a', 123, 'b', 456) not in d.items()

View File

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