From a3fed90fc2d76e97b88f005d9fdbe859cd28cc85 Mon Sep 17 00:00:00 2001 From: zetwhite Date: Sat, 23 Oct 2021 14:38:28 +0900 Subject: [PATCH 1/3] Implements Comparable for PyDictKeys, PyDictItems --- vm/src/builtins/dict.rs | 88 +++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 29 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index ea31954251..73b73fab12 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -746,34 +746,6 @@ macro_rules! dict_view { } } - impl Comparable for $name { - fn cmp( - zelf: &PyObjectView, - other: &PyObject, - op: PyComparisonOp, - vm: &VirtualMachine, - ) -> PyResult { - match_class!(match other { - ref dictview @ Self => { - PyDict::inner_cmp( - &zelf.dict, - &dictview.dict, - op, - !zelf.class().is(&vm.ctx.types.dict_keys_type), - vm, - ) - } - ref _set @ PySet => { - // TODO: Implement comparison for set - Ok(NotImplemented) - } - _ => { - Ok(NotImplemented) - } - }) - } - } - impl PyValue for $name { fn class(vm: &VirtualMachine) -> &PyTypeRef { &vm.ctx.types.$class @@ -997,6 +969,35 @@ impl PyDictKeys { } impl Unconstructible for PyDictKeys {} +impl Comparable for PyDictKeys { + fn cmp( + zelf: &PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + match_class!(match other { + ref dictview @ Self => { + PyDict::inner_cmp( + &zelf.dict, + &dictview.dict, + op, + !zelf.class().is(&vm.ctx.types.dict_keys_type), + vm, + ) + } + ref _set @ PySet => { + let inner = Self::to_set(zelf.to_owned(), vm)?; + let zelf_set = PySet { inner }.into_object(vm); + PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm) + } + _ => { + Ok(NotImplemented) + } + }) + } +} + impl ViewSetOps for PyDictItems {} #[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))] impl PyDictItems { @@ -1022,7 +1023,36 @@ impl PyDictItems { } impl Unconstructible for PyDictItems {} -#[pyimpl(with(DictView, Constructor, Comparable, Iterable))] +impl Comparable for PyDictItems { + fn cmp( + zelf: &PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + match_class!(match other { + ref dictview @ Self => { + PyDict::inner_cmp( + &zelf.dict, + &dictview.dict, + op, + !zelf.class().is(&vm.ctx.types.dict_keys_type), + vm, + ) + } + ref _set @ PySet => { + let inner = Self::to_set(zelf.to_owned(), vm)?; + let zelf_set = PySet { inner }.into_object(vm); + PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm) + } + _ => { + Ok(NotImplemented) + } + }) + } +} + +#[pyimpl(with(DictView, Constructor, Iterable))] impl PyDictValues {} impl Unconstructible for PyDictValues {} From a401d8c190c8f9b9db2c702caa0ecab474c45491 Mon Sep 17 00:00:00 2001 From: zetwhite Date: Sun, 24 Oct 2021 16:54:33 +0900 Subject: [PATCH 2/3] make work dictview compare test --- Lib/test/test_dictviews.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index 46bb81e96d..22142358a0 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -17,8 +17,6 @@ def test_constructors_not_callable(self): self.assertRaises(TypeError, vt, {}) self.assertRaises(TypeError, vt) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_dict_keys(self): d = {1: 10, "a": "ABC"} keys = d.keys() @@ -39,8 +37,6 @@ def test_dict_keys(self): del e["a"] self.assertNotEqual(d.keys(), e.keys()) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_dict_items(self): d = {1: 10, "a": "ABC"} items = d.items() From f9bb87ee84e25482a5050c80b262b8abf53a9598 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 27 Oct 2021 23:51:11 +0900 Subject: [PATCH 3/3] ViewSetOps::cmp --- vm/src/builtins/dict.rs | 57 +++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 73b73fab12..5196d2ecc9 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -957,19 +957,7 @@ trait ViewSetOps: DictView { let inner = zelf.difference(other, vm)?; Ok(PySet { inner }) } -} -impl ViewSetOps for PyDictKeys {} -#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))] -impl PyDictKeys { - #[pymethod(magic)] - fn contains(zelf: PyRef, key: PyObjectRef, vm: &VirtualMachine) -> PyResult { - zelf.dict().contains(key, vm) - } -} -impl Unconstructible for PyDictKeys {} - -impl Comparable for PyDictKeys { fn cmp( zelf: &PyObjectView, other: &PyObject, @@ -979,8 +967,8 @@ impl Comparable for PyDictKeys { match_class!(match other { ref dictview @ Self => { PyDict::inner_cmp( - &zelf.dict, - &dictview.dict, + zelf.dict(), + dictview.dict(), op, !zelf.class().is(&vm.ctx.types.dict_keys_type), vm, @@ -998,6 +986,27 @@ impl Comparable for PyDictKeys { } } +impl ViewSetOps for PyDictKeys {} +#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))] +impl PyDictKeys { + #[pymethod(magic)] + fn contains(zelf: PyRef, key: PyObjectRef, vm: &VirtualMachine) -> PyResult { + zelf.dict().contains(key, vm) + } +} +impl Unconstructible for PyDictKeys {} + +impl Comparable for PyDictKeys { + fn cmp( + zelf: &PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + ViewSetOps::cmp(zelf, other, op, vm) + } +} + impl ViewSetOps for PyDictItems {} #[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))] impl PyDictItems { @@ -1030,25 +1039,7 @@ impl Comparable for PyDictItems { op: PyComparisonOp, vm: &VirtualMachine, ) -> PyResult { - match_class!(match other { - ref dictview @ Self => { - PyDict::inner_cmp( - &zelf.dict, - &dictview.dict, - op, - !zelf.class().is(&vm.ctx.types.dict_keys_type), - vm, - ) - } - ref _set @ PySet => { - let inner = Self::to_set(zelf.to_owned(), vm)?; - let zelf_set = PySet { inner }.into_object(vm); - PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm) - } - _ => { - Ok(NotImplemented) - } - }) + ViewSetOps::cmp(zelf, other, op, vm) } }