diff --git a/tests/snippets/bytearray.py b/tests/snippets/bytearray.py index 02331a598..31c2ae68b 100644 --- a/tests/snippets/bytearray.py +++ b/tests/snippets/bytearray.py @@ -713,3 +713,10 @@ assert a == bytearray(b'test') # mod assert bytearray('rust%bpython%b', 'utf-8') % (b' ', b'!') == bytearray(b'rust python!') assert bytearray('x=%i y=%f', 'utf-8') % (1, 2.5) == bytearray(b'x=1 y=2.500000') + +# eq, ne +a = bytearray(b'hello, world') +b = a.copy() +assert a.__ne__(b) is False +b = bytearray(b'my bytearray') +assert a.__ne__(b) is True diff --git a/tests/snippets/stdlib_array.py b/tests/snippets/stdlib_array.py index fb3a87ee1..0886b22c3 100644 --- a/tests/snippets/stdlib_array.py +++ b/tests/snippets/stdlib_array.py @@ -13,3 +13,10 @@ assert a1 == array("B", [3, 2, 1, 0]) a1.extend([4, 5, 6, 7]) assert a1 == array("h", [3, 2, 1, 0, 4, 5, 6, 7]) + +# eq, ne +a = array("b", [0, 1, 2, 3]) +b = a.copy() +assert a.__ne__(b) is False +b = array("B", [3, 2, 1, 0]) +assert a.__ne__(b) is True \ No newline at end of file diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 3d10d153c..b0177ea62 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -274,6 +274,10 @@ impl PyBytesInner { self.cmp(other, |a, b| a == b, vm) } + pub fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue { + self.cmp(other, |a, b| a != b, vm) + } + pub fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue { self.cmp(other, |a, b| a >= b, vm) } diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index e8f439783..6d2ac48da 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -123,6 +123,11 @@ impl PyByteArray { self.borrow_value().eq(other, vm) } + #[pymethod(name = "__ne__")] + fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue { + self.borrow_value().ne(other, vm) + } + #[pymethod(name = "__ge__")] fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue { self.borrow_value().ge(other, vm) diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index 5161f1779..dda45292d 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -456,6 +456,25 @@ impl PyArray { } } + #[pymethod(name = "__ne__")] + fn ne(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult { + let lhs = class_or_notimplemented!(vm, Self, lhs); + let rhs = class_or_notimplemented!(vm, Self, rhs); + let lhs = lhs.borrow_value(); + let rhs = rhs.borrow_value(); + if lhs.len() != rhs.len() { + Ok(vm.new_bool(true)) + } else { + for (a, b) in lhs.iter(vm).zip(rhs.iter(vm)) { + let ne = objbool::boolval(vm, vm._ne(a?, b?)?)?; + if ne { + return Ok(vm.new_bool(true)); + } + } + Ok(vm.new_bool(false)) + } + } + #[pymethod(name = "__lt__")] fn lt(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult { let lhs = class_or_notimplemented!(vm, Self, lhs);