Skip to content

Commit 620e4b9

Browse files
committed
Add __ne__ for array, bytearray
1 parent b57d943 commit 620e4b9

5 files changed

Lines changed: 42 additions & 0 deletions

File tree

tests/snippets/bytearray.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,3 +713,10 @@
713713
# mod
714714
assert bytearray('rust%bpython%b', 'utf-8') % (b' ', b'!') == bytearray(b'rust python!')
715715
assert bytearray('x=%i y=%f', 'utf-8') % (1, 2.5) == bytearray(b'x=1 y=2.500000')
716+
717+
# eq, ne
718+
a = bytearray(b'hello, world')
719+
b = a.copy()
720+
assert a.__ne__(b) is False
721+
b = bytearray(b'my bytearray')
722+
assert a.__ne__(b) is True

tests/snippets/stdlib_array.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@
1313
a1.extend([4, 5, 6, 7])
1414

1515
assert a1 == array("h", [3, 2, 1, 0, 4, 5, 6, 7])
16+
17+
# eq, ne
18+
a = array("b", [0, 1, 2, 3])
19+
b = a.copy()
20+
assert a.__ne__(b) is False
21+
b = array("B", [3, 2, 1, 0])
22+
assert a.__ne__(b) is True

vm/src/bytesinner.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ impl PyBytesInner {
274274
self.cmp(other, |a, b| a == b, vm)
275275
}
276276

277+
pub fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
278+
self.cmp(other, |a, b| a != b, vm)
279+
}
280+
277281
pub fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
278282
self.cmp(other, |a, b| a >= b, vm)
279283
}

vm/src/obj/objbytearray.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ impl PyByteArray {
123123
self.borrow_value().eq(other, vm)
124124
}
125125

126+
#[pymethod(name = "__ne__")]
127+
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
128+
self.borrow_value().ne(other, vm)
129+
}
130+
126131
#[pymethod(name = "__ge__")]
127132
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
128133
self.borrow_value().ge(other, vm)

vm/src/stdlib/array.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,25 @@ impl PyArray {
456456
}
457457
}
458458

459+
#[pymethod(name = "__ne__")]
460+
fn ne(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
461+
let lhs = class_or_notimplemented!(vm, Self, lhs);
462+
let rhs = class_or_notimplemented!(vm, Self, rhs);
463+
let lhs = lhs.borrow_value();
464+
let rhs = rhs.borrow_value();
465+
if lhs.len() != rhs.len() {
466+
Ok(vm.new_bool(true))
467+
} else {
468+
for (a, b) in lhs.iter(vm).zip(rhs.iter(vm)) {
469+
let ne = objbool::boolval(vm, vm._ne(a?, b?)?)?;
470+
if ne {
471+
return Ok(vm.new_bool(true));
472+
}
473+
}
474+
Ok(vm.new_bool(false))
475+
}
476+
}
477+
459478
#[pymethod(name = "__lt__")]
460479
fn lt(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
461480
let lhs = class_or_notimplemented!(vm, Self, lhs);

0 commit comments

Comments
 (0)