Skip to content

Commit dd24727

Browse files
committed
Use __eq__ methods inequality too.
1 parent dc021fd commit dd24727

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

vm/src/obj/objobject.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::super::objbool;
12
use super::super::pyobject::{
23
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
34
PyResult, TypeProtocol,
@@ -38,6 +39,16 @@ fn object_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3839
Ok(vm.ctx.new_bool(zelf.is(other)))
3940
}
4041

42+
fn object_ne(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
43+
arg_check!(
44+
vm,
45+
args,
46+
required = [(zelf, Some(vm.ctx.object())), (other, None)]
47+
);
48+
let eq = vm.call_method(zelf.clone(), "__eq__", vec![other.clone()])?;
49+
objbool::not(vm, &eq)
50+
}
51+
4152
fn object_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4253
arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]);
4354
let type_name = objtype::get_type_name(&obj.typ());
@@ -49,6 +60,7 @@ pub fn init(context: &PyContext) {
4960
let ref object = context.object;
5061
object.set_attr("__new__", context.new_rustfunc(new_instance));
5162
object.set_attr("__eq__", context.new_rustfunc(object_eq));
63+
object.set_attr("__ne__", context.new_rustfunc(object_ne));
5264
object.set_attr("__dict__", context.new_member_descriptor(object_dict));
5365
object.set_attr("__str__", context.new_rustfunc(object_str));
5466
}

vm/src/objbool.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,23 @@ fn bool_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4545
required = [(zelf, Some(vm.ctx.bool_type())), (other, None)]
4646
);
4747

48-
let result = if objtype::isinstance(zelf.clone(), vm.ctx.bool_type()) {
48+
let result = if objtype::isinstance(other.clone(), vm.ctx.bool_type()) {
4949
get_value(zelf) == get_value(other)
5050
} else {
5151
false
5252
};
5353
Ok(vm.ctx.new_bool(result))
5454
}
5555

56+
pub fn not(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyResult {
57+
if objtype::isinstance(obj.clone(), vm.ctx.bool_type()) {
58+
let value = get_value(obj);
59+
Ok(vm.ctx.new_bool(!value))
60+
} else {
61+
Err(vm.new_type_error(format!("Can only invert a bool, on {:?}", obj)))
62+
}
63+
}
64+
5665
// Retrieve inner int value:
5766
pub fn get_value(obj: &PyObjectRef) -> bool {
5867
if let PyObjectKind::Boolean { value } = &obj.borrow().kind {

vm/src/vm.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,7 @@ impl VirtualMachine {
437437
}
438438

439439
fn _ne(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
440-
let b2 = &*b.borrow();
441-
let a2 = &*a.borrow();
442-
let result_bool = a2 != b2;
443-
let result = self.ctx.new_bool(result_bool);
444-
Ok(result)
440+
self.call_method(a, "__ne__", vec![b])
445441
}
446442

447443
fn _lt(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {

0 commit comments

Comments
 (0)