mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Add __ne__ for array, bytearray
This commit is contained in:
1 parent
b57d943031
commit
620e4b93ea
5 files changed
+42
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user