8000 PyList::cmp · RustPython/RustPython@db67018 · GitHub
[go: up one dir, main page]

Skip to content

Commit db67018

Browse files
committed
PyList::cmp
1 parent 226ebfa commit db67018

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

vm/src/obj/objlist.rs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -556,24 +556,30 @@ impl PyList {
556556
}
557557
}
558558

559+
#[inline]
560+
fn cmp<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyResult<PyComparisonValue>
561+
where
562+
F: Fn(&Vec<PyObjectRef>, &Vec<PyObjectRef>) -> PyResult<bool>,
563+
{
564+
let r = if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
565+
Implemented(op(&*self.borrow_elements(), &*other.borrow_elements())?)
566+
} else {
567+
NotImplemented
568+
};
569+
Ok(r)
570+
}
571+
559572
#[pymethod(name = "__eq__")]
560573
fn eq(
561574
zelf: PyRef<Self>,
562575
other: PyObjectRef,
563576
vm: &VirtualMachine,
564577
) -> PyResult<PyComparisonValue> {
565-
let value = if zelf.as_object().is(&other) {
566-
Implemented(true)
567-
} else if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
568-
Implemented(sequence::eq(
569-
vm,
570-
&zelf.borrow_sequence(),
571-
&other.borrow_sequence(),
572-
)?)
578+
if zelf.as_object().is(&other) {
579+
Ok(Implemented(true))
573580
} else {
574-
NotImplemented
575-
};
576-
Ok(value)
581+
zelf.cmp(other, |a, b| sequence::eq(vm, a, b), vm)
582+
}
577583
}
578584

579585
#[pymethod(name = "__ne__")]
@@ -586,43 +592,23 @@ impl PyList {
586592
}
587593

588594
#[pymethod(name = "__lt__")]
589-
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
590-
if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
591-
let res = sequence::lt(vm, &self.borrow_sequence(), &other.borrow_sequence())?;
592-
Ok(vm.new_bool(res))
593-
} else {
594-
Ok(vm.ctx.not_implemented())
595-
}
595+
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
596+
self.cmp(other, |a, b| sequence::lt(vm, a, b), vm)
596597
}
597598

598599
#[pymethod(name = "__gt__")]
599-
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
600-
if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
601-
let res = sequence::gt(vm, &self.borrow_sequence(), &other.borrow_sequence())?;
602-
Ok(vm.new_bool(res))
603-
} else {
604-
Ok(vm.ctx.not_implemented())
605-
}
600+
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
601+
self.cmp(other, |a, b| sequence::gt(vm, a, b), vm)
606602
}
607603

608604
#[pymethod(name = "__ge__")]
609-
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
610-
if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
611-
let res = sequence::ge(vm, &self.borrow_sequence(), &other.borrow_sequence())?;
612-
Ok(vm.new_bool(res))
613-
} else {
614-
Ok(vm.ctx.not_implemented())
615-
}
605+
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
606+
self.cmp(other, |a, b| sequence::ge(vm, a, b), vm)
616607
}
617608

618609
#[pymethod(name = "__le__")]
619-
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
620-
if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
621-
let res = sequence::le(vm, &self.borrow_sequence(), &other.borrow_sequence())?;
622-
Ok(vm.new_bool(res))
623-
} else {
624-
Ok(vm.ctx.not_implemented())
625-
}
610+
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
611+
self.cmp(other, |a, b| sequence::le(vm, a, b), vm)
626612
}
627613

628614
#[pymethod(name = "__delitem__")]

0 commit comments

Comments
 (0)
0