mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Merge pull request #2028 from ugaemi/master
Implement __ne__ methods for array, bytearray
This commit is contained in:
6 files changed
+57
-34
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
|
||||
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.eq(other, vm).map(|v| !v)
|
||||
}
|
||||
|
||||
pub fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
|
||||
self.cmp(other, |a, b| a >= b, vm)
|
||||
}
|
||||
|
||||
+1
-1
@@ -221,7 +221,7 @@ macro_rules! class_or_notimplemented {
|
||||
($vm:expr, $t:ty, $obj:expr) => {
|
||||
match $crate::pyobject::PyObject::downcast::<$t>($obj) {
|
||||
Ok(pyref) => pyref,
|
||||
Err(_) => return Ok($vm.ctx.not_implemented()),
|
||||
Err(_) => return Ok(PyArithmaticValue::NotImplemented),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
+33
-33
@@ -5,8 +5,8 @@ use crate::obj::objstr::PyStringRef;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::obj::{objbool, objiter};
|
||||
use crate::pyobject::{
|
||||
BorrowValue, Either, IntoPyObject, PyClassImpl, PyIterable, PyObjectRef, PyRef, PyResult,
|
||||
PyValue, TryFromObject,
|
||||
BorrowValue, Either, IntoPyObject, PyArithmaticValue, PyClassImpl, PyComparisonValue,
|
||||
PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
|
||||
};
|
||||
use crate::VirtualMachine;
|
||||
|
||||
@@ -438,94 +438,94 @@ impl PyArray {
|
||||
}
|
||||
|
||||
#[pymethod(name = "__eq__")]
|
||||
fn eq(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();
|
||||
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let lhs = self.borrow_value();
|
||||
let rhs = class_or_notimplemented!(vm, Self, other);
|
||||
let rhs = rhs.borrow_value();
|
||||
if lhs.len() != rhs.len() {
|
||||
Ok(vm.ctx.new_bool(false))
|
||||
Ok(PyArithmaticValue::Implemented(false))
|
||||
} 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.ctx.new_bool(false));
|
||||
return Ok(PyArithmaticValue::Implemented(false));
|
||||
}
|
||||
}
|
||||
Ok(vm.ctx.new_bool(true))
|
||||
Ok(PyArithmaticValue::Implemented(true))
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ne__")]
|
||||
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
Ok(self.eq(other, vm)?.map(|v| !v))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__lt__")]
|
||||
fn lt(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();
|
||||
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let lhs = self.borrow_value();
|
||||
let rhs = class_or_notimplemented!(vm, Self, other);
|
||||
let rhs = rhs.borrow_value();
|
||||
|
||||
for (a, b) in lhs.iter(vm).zip(rhs.iter(vm)) {
|
||||
let lt = objbool::boolval(vm, vm._lt(a, b)?)?;
|
||||
|
||||
if lt {
|
||||
return Ok(vm.ctx.new_bool(true));
|
||||
return Ok(PyArithmaticValue::Implemented(true));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(vm.ctx.new_bool(lhs.len() < rhs.len()))
|
||||
Ok(PyArithmaticValue::Implemented(lhs.len() < rhs.len()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__le__")]
|
||||
fn le(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();
|
||||
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let lhs = self.borrow_value();
|
||||
let rhs = class_or_notimplemented!(vm, Self, other);
|
||||
let rhs = rhs.borrow_value();
|
||||
|
||||
for (a, b) in lhs.iter(vm).zip(rhs.iter(vm)) {
|
||||
let le = objbool::boolval(vm, vm._le(a, b)?)?;
|
||||
|
||||
if le {
|
||||
return Ok(vm.ctx.new_bool(true));
|
||||
return Ok(PyArithmaticValue::Implemented(true));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(vm.ctx.new_bool(lhs.len() <= rhs.len()))
|
||||
Ok(PyArithmaticValue::Implemented(lhs.len() <= rhs.len()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__gt__")]
|
||||
fn gt(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();
|
||||
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let lhs = self.borrow_value();
|
||||
let rhs = class_or_notimplemented!(vm, Self, other);
|
||||
let rhs = rhs.borrow_value();
|
||||
|
||||
for (a, b) in lhs.iter(vm).zip(rhs.iter(vm)) {
|
||||
let gt = objbool::boolval(vm, vm._gt(a, b)?)?;
|
||||
|
||||
if gt {
|
||||
return Ok(vm.ctx.new_bool(true));
|
||||
return Ok(PyArithmaticValue::Implemented(true));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(vm.ctx.new_bool(lhs.len() > rhs.len()))
|
||||
Ok(PyArithmaticValue::Implemented(lhs.len() > rhs.len()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ge__")]
|
||||
fn ge(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();
|
||||
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let lhs = self.borrow_value();
|
||||
let rhs = class_or_notimplemented!(vm, Self, other);
|
||||
let rhs = rhs.borrow_value();
|
||||
|
||||
for (a, b) in lhs.iter(vm).zip(rhs.iter(vm)) {
|
||||
let ge = objbool::boolval(vm, vm._ge(a, b)?)?;
|
||||
|
||||
if ge {
|
||||
return Ok(vm.ctx.new_bool(true));
|
||||
return Ok(PyArithmaticValue::Implemented(true));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(vm.ctx.new_bool(lhs.len() >= rhs.len()))
|
||||
Ok(PyArithmaticValue::Implemented(lhs.len() >= rhs.len()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
|
||||
Reference in new issue
Block a user