From aef89ceaecabe681ec414372ac59c285eb8e182b Mon Sep 17 00:00:00 2001 From: Jack-R-lantern Date: Sat, 9 Oct 2021 17:32:03 +0900 Subject: [PATCH] Add __contains__ in dict_items --- extra_tests/snippets/builtin_dict.py | 10 ++++++++-- vm/src/builtins/dict.rs | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/extra_tests/snippets/builtin_dict.py b/extra_tests/snippets/builtin_dict.py index 4a18c9f0d..f240b431d 100644 --- a/extra_tests/snippets/builtin_dict.py +++ b/extra_tests/snippets/builtin_dict.py @@ -16,7 +16,7 @@ assert {'a': 123}.get('b') == None assert {'a': 123}.get('b', 456) == 456 d = {'a': 123, 'b': 456} - +assert list(reversed(d)) == ['b', 'a'] assert list(reversed(d.keys())) == ['b', 'a'] assert list(reversed(d.values())) == [456, 123] assert list(reversed(d.items())) == [('b', 456), ('a', 123)] @@ -27,7 +27,13 @@ with assert_raises(StopIteration): 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 () not in d.items() \ No newline at end of file +assert ('a', 123, 'b', 456) not in d.items() \ No newline at end of file diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 706abf2d7..85e2fad83 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -3,7 +3,7 @@ use super::{ PyTypeRef, }; use crate::{ - builtins::PyTupleRef, + builtins::PyTuple, common::ascii, dictdatatype::{self, DictKey}, function::{ArgIterable, FuncArgs, IntoPyObject, KwArgs, OptionalArg}, @@ -985,7 +985,13 @@ impl PyDictItems { } #[pymethod(magic)] - fn contains(zelf: PyRef, needle: PyTupleRef, vm: &VirtualMachine) -> PyResult { + fn contains(zelf: PyRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { + let needle = match_class! { + match needle { + tuple @ PyTuple => tuple, + _ => return Ok(false), + } + }; if needle.len() != 2 { return Ok(false); }