Skip to content

Commit ad59192

Browse files
None.__ne__(None) should be NotImplemented
1 parent af884cb commit ad59192

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

extra_tests/snippets/builtin_none.py

+1-1
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

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use super::{PyStrRef, PyType, PyTypeRef};
22
use crate::{
33
class::PyClassImpl,
44
convert::ToPyObject,
5+
function::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
};
10+
use rustpython_vm::AsObject;
911

1012
#[pyclass(module = false, name = "NoneType")]
1113
#[derive(Debug)]
@@ -118,6 +120,22 @@ impl Representable for PyNotImplemented {
118120
}
119121
}
120122

123+
impl Comparable for PyNone {
124+
#[allow(unused_variables)]
125+
fn cmp(
126+
zelf: &Py<Self>,
127+
other: &PyObject,
128+
op: PyComparisonOp,
129+
vm: &VirtualMachine,
130+
) -> PyResult<PyComparisonValue> {
131+
if zelf.is(other) {
132+
Ok(PyComparisonValue::Implemented(true))
133+
} else {
134+
Ok(PyComparisonValue::NotImplemented)
135+
}
136+
}
137+
}
138+
121139
pub fn init(context: &Context) {
122140
PyNone::extend_class(context, context.types.none_type);
123141
PyNotImplemented::extend_class(context, context.types.not_implemented_type);

0 commit comments

Comments
 (0)