Skip to content

Commit 1ab133d

Browse files
None.__ne__(None) should be NotImplemented (RustPython#5124)
1 parent 317f449 commit 1ab133d

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎extra_tests/snippets/builtin_none.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ def none2():
2222
assert None.__eq__(3) is NotImplemented
2323
assert None.__ne__(3) is NotImplemented
2424
assert None.__eq__(None) is True
25-
# assert None.__ne__(None) is False # changed in 3.12
25+
assert None.__ne__(None) is NotImplemented

‎vm/src/builtins/singletons.rs‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use super::{PyStrRef, PyType, PyTypeRef};
22
use crate::{
33
class::PyClassImpl,
44
convert::ToPyObject,
5+
function::{PyArithmeticValue, PyComparisonValue},
56
protocol::PyNumberMethods,
6-
types::{AsNumber, Constructor, Representable},
7-
Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
7+
types::{AsNumber, Comparable, Constructor, PyComparisonOp, Representable},
8+
Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
89
};
910

1011
#[pyclass(module = false, name = "NoneType")]
@@ -42,7 +43,7 @@ impl Constructor for PyNone {
4243
}
4344
}
4445

45-
#[pyclass(with(Constructor, AsNumber, Representable))]
46+
#[pyclass(with(Constructor, Comparable, AsNumber, Representable))]
4647
impl PyNone {
4748
#[pymethod(magic)]
4849
fn bool(&self) -> bool {
@@ -72,6 +73,28 @@ impl AsNumber for PyNone {
7273
}
7374
}
7475

76+
impl Comparable for PyNone {
77+
fn cmp(
78+
_zelf: &Py<Self>,
79+
other: &PyObject,
80+
op: PyComparisonOp,
81+
vm: &VirtualMachine,
82+
) -> PyResult<PyComparisonValue> {
83+
let value = match op {
84+
PyComparisonOp::Eq => {
85+
if vm.is_none(other) {
86+
PyArithmeticValue::Implemented(true)
87+
} else {
88+
PyArithmeticValue::NotImplemented
89+
}
90+
}
91+
_ => PyComparisonValue::NotImplemented,
92+
};
93+
94+
Ok(value)
95+
}
96+
}
97+
7598
#[pyclass(module = false, name = "NotImplementedType")]
7699
#[derive(Debug)]
77100
pub struct PyNotImplemented;

0 commit comments

Comments
 (0)