Skip to content

Commit 100416f

Browse files
committed
Add __eq__, __ne__ of none
Issue RustPython#1442
1 parent 1e8ade7 commit 100416f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

tests/snippets/none.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ def none2():
1818
assert str(None) == 'None'
1919
assert repr(None) == 'None'
2020
assert type(None)() is None
21+
22+
assert None.__eq__(3) is NotImplemented
23+
assert None.__ne__(3) is NotImplemented
24+
assert None.__eq__(None) is True
25+
assert None.__ne__(None) is False
26+

vm/src/obj/objnone.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ impl PyNoneRef {
111111
Err(vm.new_attribute_error(format!("{} has no attribute '{}'", self.as_object(), name)))
112112
}
113113
}
114+
115+
#[pymethod(name = "__eq__")]
116+
fn eq(self, rhs: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
117+
if vm.is_none(&rhs) {
118+
vm.ctx.new_bool(true)
119+
} else {
120+
vm.ctx.not_implemented()
121+
}
122+
}
123+
124+
#[pymethod(name = "__ne__")]
125+
fn ne(self, rhs: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
126+
if vm.is_none(&rhs) {
127+
vm.ctx.new_bool(false)
128+
} else {
129+
vm.ctx.not_implemented()
130+
}
131+
}
114132
}
115133

116134
pub fn init(context: &PyContext) {

0 commit comments

Comments
 (0)