Skip to content

Commit 0887b55

Browse files
committed
Add object.{__lt__, __le__, __gt__, __gt__}
1 parent 9177252 commit 0887b55

5 files changed

Lines changed: 69 additions & 13 deletions

File tree

tests/snippets/object.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ class MyObject:
99

1010
assert MyObject().__eq__(MyObject()) == NotImplemented
1111
assert MyObject().__ne__(MyObject()) == NotImplemented
12+
assert MyObject().__lt__(MyObject()) == NotImplemented
13+
assert MyObject().__le__(MyObject()) == NotImplemented
14+
assert MyObject().__gt__(MyObject()) == NotImplemented
15+
assert MyObject().__ge__(MyObject()) == NotImplemented

vm/src/frame.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,10 @@ impl Frame {
990990
let value = match *op {
991991
bytecode::ComparisonOperator::Equal => vm._eq(a, b)?,
992992
bytecode::ComparisonOperator::NotEqual => vm._ne(a, b)?,
993-
bytecode::ComparisonOperator::Less => vm._lt(&a, b)?,
994-
bytecode::ComparisonOperator::LessOrEqual => vm._le(&a, b)?,
995-
bytecode::ComparisonOperator::Greater => vm._gt(&a, b)?,
996-
bytecode::ComparisonOperator::GreaterOrEqual => vm._ge(&a, b)?,
993+
bytecode::ComparisonOperator::Less => vm._lt(a, b)?,
994+
bytecode::ComparisonOperator::LessOrEqual => vm._le(a, b)?,
995+
bytecode::ComparisonOperator::Greater => vm._gt(a, b)?,
996+
bytecode::ComparisonOperator::GreaterOrEqual => vm._ge(a, b)?,
997997
bytecode::ComparisonOperator::Is => vm.ctx.new_bool(self._is(a, b)),
998998
bytecode::ComparisonOperator::IsNot => self._is_not(vm, a, b)?,
999999
bytecode::ComparisonOperator::In => self._in(vm, a, b)?,

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn float_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
100100
.ctx
101101
.new_bool(v1 < objint::get_value(i2).to_f64().unwrap()))
102102
} else {
103-
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
103+
Ok(vm.ctx.not_implemented())
104104
}
105105
}
106106

vm/src/obj/objobject.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@ fn object_ne(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4343
Ok(vm.ctx.not_implemented())
4444
}
4545

46+
fn object_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
47+
arg_check!(
48+
vm,
49+
args,
50+
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
51+
);
52+
53+
Ok(vm.ctx.not_implemented())
54+
}
55+
56+
fn object_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
57+
arg_check!(
58+
vm,
59+
args,
60+
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
61+
);
62+
63+
Ok(vm.ctx.not_implemented())
64+
}
65+
66+
fn object_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
67+
arg_check!(
68+
vm,
69+
args,
70+
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
71+
);
72+
73+
Ok(vm.ctx.not_implemented())
74+
}
75+
76+
fn object_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
77+
arg_check!(
78+
vm,
79+
args,
80+
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
81+
);
82+
83+
Ok(vm.ctx.not_implemented())
84+
}
85+
4686
fn object_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4787
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.object()))]);
4888

@@ -91,6 +131,10 @@ pub fn init(context: &PyContext) {
91131
context.set_attr(&object, "__init__", context.new_rustfunc(object_init));
92132
context.set_attr(&object, "__eq__", context.new_rustfunc(object_eq));
93133
context.set_attr(&object, "__ne__", context.new_rustfunc(object_ne));
134+
context.set_attr(&object, "__lt__", context.new_rustfunc(object_lt));
135+
context.set_attr(&object, "__le__", context.new_rustfunc(object_le));
136+
context.set_attr(&object, "__gt__", context.new_rustfunc(object_gt));
137+
context.set_attr(&object, "__ge__", context.new_rustfunc(object_ge));
94138
context.set_attr(&object, "__delattr__", context.new_rustfunc(object_delattr));
95139
context.set_attr(
96140
&object,

vm/src/vm.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,20 +620,28 @@ impl VirtualMachine {
620620
})
621621
}
622622

623-
pub fn _lt(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
624-
self.call_method(a, "__lt__", vec![b])
623+
pub fn _lt(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
624+
self.call_or_unsupported(a, b, "__lt__", "__lt__", |vm, a, b| {
625+
Err(vm.new_unsupported_operand_error(a, b, "<"))
626+
})
625627
}
626628

627-
pub fn _le(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
628-
self.call_method(a, "__le__", vec![b])
629+
pub fn _le(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
630+
self.call_or_unsupported(a, b, "__le__", "__le__", |vm, a, b| {
631+
Err(vm.new_unsupported_operand_error(a, b, "<="))
632+
})
629633
}
630634

631-
pub fn _gt(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
632-
self.call_method(a, "__gt__", vec![b])
635+
pub fn _gt(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
636+
self.call_or_unsupported(a, b, "__gt__", "__gt__", |vm, a, b| {
637+
Err(vm.new_unsupported_operand_error(a, b, ">"))
638+
})
633639
}
634640

635-
pub fn _ge(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
636-
self.call_method(a, "__ge__", vec![b])
641+
pub fn _ge(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
642+
self.call_or_unsupported(a, b, "__ge__", "__ge__", |vm, a, b| {
643+
Err(vm.new_unsupported_operand_error(a, b, ">="))
644+
})
637645
}
638646
}
639647

0 commit comments

Comments
 (0)