Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Lib/test/test_dictviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
79 changes: 50 additions & 29 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,34 +746,6 @@ macro_rules! dict_view {
}
}

impl Comparable for $name {
fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
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
Expand Down Expand Up @@ -985,6 +957,33 @@ trait ViewSetOps: DictView {
let inner = zelf.difference(other, vm)?;
Ok(PySet { inner })
}

fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
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)?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this line is fine or not.
If you have any idea, please comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_set make copy of all elements. Iterating all elements in zelf and using PySet::contains may make sense like all_contained_in in cpython. Although we dont have PySequence_Contains, we have PySet type we can use it 😊

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'll check and fix it. 👍 😄

let zelf_set = PySet { inner }.into_object(vm);
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
}
_ => {
Ok(NotImplemented)
}
})
}
}

impl ViewSetOps for PyDictKeys {}
Expand All @@ -997,6 +996,17 @@ impl PyDictKeys {
}
impl Unconstructible for PyDictKeys {}

impl Comparable for PyDictKeys {
fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
ViewSetOps::cmp(zelf, other, op, vm)
}
}

impl ViewSetOps for PyDictItems {}
#[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))]
impl PyDictItems {
Expand All @@ -1022,7 +1032,18 @@ impl PyDictItems {
}
impl Unconstructible for PyDictItems {}

#[pyimpl(with(DictView, Constructor, Comparable, Iterable))]
impl Comparable for PyDictItems {
fn cmp(
zelf: &PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
ViewSetOps::cmp(zelf, other, op, vm)
}
}

#[pyimpl(with(DictView, Constructor, Iterable))]
impl PyDictValues {}
impl Unconstructible for PyDictValues {}

Expand Down