Skip to content

Add object.{__lt__, __le__, __gt__, __gt__} #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add object.{__lt__, __le__, __gt__, __gt__}
  • Loading branch information
janczer committed Feb 12, 2019
commit 0887b55da8e83a8da2398423993a8439b100649d
4 changes: 4 additions & 0 deletions tests/snippets/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ class MyObject:

assert MyObject().__eq__(MyObject()) == NotImplemented
assert MyObject().__ne__(MyObject()) == NotImplemented
assert MyObject().__lt__(MyObject()) == NotImplemented
assert MyObject().__le__(MyObject()) == NotImplemented
assert MyObject().__gt__(MyObject()) == NotImplemented
assert MyObject().__ge__(MyObject()) == NotImplemented
8 changes: 4 additions & 4 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,10 +990,10 @@ impl Frame {
let value = match *op {
bytecode::ComparisonOperator::Equal => vm._eq(a, b)?,
bytecode::ComparisonOperator::NotEqual => vm._ne(a, b)?,
bytecode::ComparisonOperator::Less => vm._lt(&a, b)?,
bytecode::ComparisonOperator::LessOrEqual => vm._le(&a, b)?,
bytecode::ComparisonOperator::Greater => vm._gt(&a, b)?,
bytecode::ComparisonOperator::GreaterOrEqual => vm._ge(&a, b)?,
bytecode::ComparisonOperator::Less => vm._lt(a, b)?,
bytecode::ComparisonOperator::LessOrEqual => vm._le(a, b)?,
bytecode::ComparisonOperator::Greater => vm._gt(a, b)?,
bytecode::ComparisonOperator::GreaterOrEqual => vm._ge(a, b)?,
bytecode::ComparisonOperator::Is => vm.ctx.new_bool(self._is(a, b)),
bytecode::ComparisonOperator::IsNot => self._is_not(vm, a, b)?,
bytecode::ComparisonOperator::In => self._in(vm, a, b)?,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/obj/objfloat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn float_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
.ctx
.new_bool(v1 < objint::get_value(i2).to_f64().unwrap()))
} else {
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
Ok(vm.ctx.not_implemented())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think float_{gt,ge,le} need this change as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks

}
}

Expand Down
44 changes: 44 additions & 0 deletions vm/src/obj/objobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ fn object_ne(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.not_implemented())
}

fn object_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
}

fn object_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
}

fn object_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
}

fn object_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);

Ok(vm.ctx.not_implemented())
}

fn object_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.object()))]);

Expand Down Expand Up @@ -91,6 +131,10 @@ pub fn init(context: &PyContext) {
context.set_attr(&object, "__init__", context.new_rustfunc(object_init));
context.set_attr(&object, "__eq__", context.new_rustfunc(object_eq));
context.set_attr(&object, "__ne__", context.new_rustfunc(object_ne));
context.set_attr(&object, "__lt__", context.new_rustfunc(object_lt));
context.set_attr(&object, "__le__", context.new_rustfunc(object_le));
context.set_attr(&object, "__gt__", context.new_rustfunc(object_gt));
context.set_attr(&object, "__ge__", context.new_rustfunc(object_ge));
context.set_attr(&object, "__delattr__", context.new_rustfunc(object_delattr));
context.set_attr(
&object,
Expand Down
24 changes: 16 additions & 8 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,20 +620,28 @@ impl VirtualMachine {
})
}

pub fn _lt(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__lt__", vec![b])
pub fn _lt(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__lt__", "__lt__", |vm, a, b| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reverse methods for these shouldn't be the same, i.e. they're not commutative like __eq__ and __ne__; a > b != b > a. Instead, as described here:

lt() and gt() are each other’s reflection, le() and ge() are each other’s reflection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Err(vm.new_unsupported_operand_error(a, b, "<"))
})
}

pub fn _le(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__le__", vec![b])
pub fn _le(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__le__", "__le__", |vm, a, b| {
Err(vm.new_unsupported_operand_error(a, b, "<="))
})
}

pub fn _gt(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__gt__", vec![b])
pub fn _gt(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__gt__", "__gt__", |vm, a, b| {
Err(vm.new_unsupported_operand_error(a, b, ">"))
})
}

pub fn _ge(&mut self, a: &PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_method(a, "__ge__", vec![b])
pub fn _ge(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__ge__", "__ge__", |vm, a, b| {
Err(vm.new_unsupported_operand_error(a, b, ">="))
})
}
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy