Skip to content

Commit 196e495

Browse files
committed
fixed set.__ror__ operator to behave according to type
1 parent 310578c commit 196e495

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Lib/test/test_dict.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,6 @@ def test_dictview_set_operations_on_items(self):
670670
self.assertEqual(k1 ^ k2, {(3,3)})
671671
self.assertEqual(k1 ^ k3, {(1,1), (2,2), (4,4)})
672672

673-
# TODO: RUSTPYTHON
674-
@unittest.expectedFailure
675673
def test_dictview_mixed_set_operations(self):
676674
# Just a few for .keys()
677675
self.assertTrue({1:1}.keys() == {1})

vm/src/builtins/set.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::{
1515
Unconstructible, Unhashable,
1616
},
1717
vm::{ReprGuard, VirtualMachine},
18-
IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObject, PyObjectRef, PyRef, PyResult,
19-
PyValue, TryFromObject, TypeProtocol,
18+
IdProtocol, PyArithmeticValue, PyClassImpl, PyComparisonValue, PyContext, PyObject,
19+
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
2020
};
2121
use std::{fmt, ops::Deref};
2222

@@ -485,8 +485,13 @@ impl PySet {
485485

486486
#[pymethod(name = "__ror__")]
487487
#[pymethod(magic)]
488-
fn or(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
489-
self.union(other.iterable, vm)
488+
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<Self>> {
489+
if other.isinstance(&vm.ctx.types.set_type) || other.isinstance(&vm.ctx.types.frozenset_type) {
490+
let val = PosArgs::new(vec![ArgIterable::try_from_object(vm, other)?]);
491+
Ok(PyArithmeticValue::Implemented(self.union(val, vm)?))
492+
} else {
493+
Ok(PyArithmeticValue::NotImplemented)
494+
}
490495
}
491496

492497
#[pymethod(name = "__rand__")]
@@ -770,8 +775,13 @@ impl PyFrozenSet {
770775

771776
#[pymethod(name = "__ror__")]
772777
#[pymethod(magic)]
773-
fn or(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
774-
self.union(other.iterable, vm)
778+
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<Self>> {
779+
if other.isinstance(&vm.ctx.types.set_type) || other.isinstance(&vm.ctx.types.frozenset_type) {
780+
let val = PosArgs::new(vec![ArgIterable::try_from_object(vm, other)?]);
781+
Ok(PyArithmeticValue::Implemented(self.union(val, vm)?))
782+
} else {
783+
Ok(PyArithmeticValue::NotImplemented)
784+
}
775785
}
776786

777787
#[pymethod(name = "__rand__")]

0 commit comments

Comments
 (0)