Skip to content
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
Prev Previous commit
Move delegation of __ne__ to __eq__ out of objobject
  • Loading branch information
OddCoincidence committed Feb 10, 2019
commit 057a22529e4434db6dd6ea5e6c9f63b7beffb4d4
11 changes: 11 additions & 0 deletions tests/snippets/object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MyObject:
pass

assert not MyObject() == MyObject()
assert MyObject() != MyObject()
myobj = MyObject()
assert myobj == myobj
assert not myobj != myobj

assert MyObject().__eq__(MyObject()) == NotImplemented
assert MyObject().__ne__(MyObject()) == NotImplemented
10 changes: 4 additions & 6 deletions vm/src/obj/objobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::super::pyobject::{
TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objbool;
use super::objstr;
use super::objtype;
use std::cell::RefCell;
Expand All @@ -29,20 +28,19 @@ fn object_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, Some(vm.ctx.object())), (other, None)]
required = [(_zelf, Some(vm.ctx.object())), (_other, None)]
);
Ok(vm.ctx.new_bool(zelf.is(other)))
Ok(vm.ctx.not_implemented())
}

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

let eq = vm._eq(zelf.clone(), other.clone())?;
objbool::not(vm, &eq)
Ok(vm.ctx.not_implemented())
}

fn object_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Expand Down
4 changes: 3 additions & 1 deletion vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::sync::{Mutex, MutexGuard};
use super::builtins;
use super::bytecode;
use super::frame::Frame;
use super::obj::objbool;
use super::obj::objcode;
use super::obj::objgenerator;
use super::obj::objiter;
Expand Down Expand Up @@ -616,7 +617,8 @@ impl VirtualMachine {

pub fn _ne(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
self.call_or_unsupported(a, b, "__ne__", "__ne__", |vm, a, b| {
Ok(vm.new_bool(!a.is(&b)))
let eq = vm._eq(a, b)?;
objbool::not(vm, &eq)
})
}

Expand Down