From be08988c7c5cae9949f8a74a5c269cd02fd7dcdf Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Sat, 25 Nov 2023 14:17:08 -0600 Subject: [PATCH 1/4] None.__ne__(None) should be NotImplemented --- extra_tests/snippets/builtin_none.py | 2 +- vm/src/builtins/singletons.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/extra_tests/snippets/builtin_none.py b/extra_tests/snippets/builtin_none.py index c75f04ea73..c8a07f518d 100644 --- a/extra_tests/snippets/builtin_none.py +++ b/extra_tests/snippets/builtin_none.py @@ -22,4 +22,4 @@ def none2(): assert None.__eq__(3) is NotImplemented assert None.__ne__(3) is NotImplemented assert None.__eq__(None) is True -# assert None.__ne__(None) is False # changed in 3.12 +assert None.__ne__(None) is NotImplemented diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index 65b171a262..7200eccd64 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -2,10 +2,12 @@ use super::{PyStrRef, PyType, PyTypeRef}; use crate::{ class::PyClassImpl, convert::ToPyObject, + function::{PyArithmeticValue, PyComparisonValue}, protocol::PyNumberMethods, - types::{AsNumber, Constructor, Representable}, - Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine, + types::{AsNumber, Comparable, Constructor, PyComparisonOp, Representable}, + Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; +use rustpython_vm::AsObject; #[pyclass(module = false, name = "NoneType")] #[derive(Debug)] @@ -42,7 +44,7 @@ impl Constructor for PyNone { } } -#[pyclass(with(Constructor, AsNumber, Representable))] +#[pyclass(with(Constructor, Comparable, AsNumber, Representable))] impl PyNone { #[pymethod(magic)] fn bool(&self) -> bool { @@ -72,6 +74,26 @@ impl AsNumber for PyNone { } } +impl Comparable for PyNone { + fn cmp( + zelf: &Py, + other: &PyObject, + op: PyComparisonOp, + _vm: &VirtualMachine, + ) -> PyResult { + match op { + PyComparisonOp::Eq => { + if zelf.is(other) { + Ok(PyArithmeticValue::Implemented(true)) + } else { + Ok(PyArithmeticValue::NotImplemented) + } + } + _ => Ok(PyComparisonValue::NotImplemented), + } + } +} + #[pyclass(module = false, name = "NotImplementedType")] #[derive(Debug)] pub struct PyNotImplemented; From 37e63cd3625c2700065a58d6b2b4892b2a8afbec Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Wed, 20 Dec 2023 19:17:50 -0600 Subject: [PATCH 2/4] Reviewer comments, pt. 1 Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> --- vm/src/builtins/singletons.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index 7200eccd64..0f177e53e2 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -83,7 +83,7 @@ impl Comparable for PyNone { ) -> PyResult { match op { PyComparisonOp::Eq => { - if zelf.is(other) { + if vm.is_none(other) { Ok(PyArithmeticValue::Implemented(true)) } else { Ok(PyArithmeticValue::NotImplemented) From ef11ebbea98fae935cc1798215f5decd60bb0bff Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Sat, 23 Dec 2023 19:17:04 -0600 Subject: [PATCH 3/4] Reviwer comments, pt. 2 --- vm/src/builtins/singletons.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index 0f177e53e2..c1c8dbcca2 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -79,18 +79,20 @@ impl Comparable for PyNone { zelf: &Py, other: &PyObject, op: PyComparisonOp, - _vm: &VirtualMachine, + vm: &VirtualMachine, ) -> PyResult { - match op { + let value = match op { PyComparisonOp::Eq => { if vm.is_none(other) { - Ok(PyArithmeticValue::Implemented(true)) + PyArithmeticValue::Implemented(true) } else { - Ok(PyArithmeticValue::NotImplemented) + PyArithmeticValue::NotImplemented } } - _ => Ok(PyComparisonValue::NotImplemented), - } + _ => PyComparisonValue::NotImplemented, + }; + + Ok(value) } } From 0ac64e13ce4574e26668afaca509b812ea6c9b08 Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Thu, 28 Dec 2023 22:39:10 -0600 Subject: [PATCH 4/4] Fix clippy, rebase to upstream/main --- vm/src/builtins/singletons.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index c1c8dbcca2..d4487b586d 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -7,7 +7,6 @@ use crate::{ types::{AsNumber, Comparable, Constructor, PyComparisonOp, Representable}, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine, }; -use rustpython_vm::AsObject; #[pyclass(module = false, name = "NoneType")] #[derive(Debug)] @@ -76,7 +75,7 @@ impl AsNumber for PyNone { impl Comparable for PyNone { fn cmp( - zelf: &Py, + _zelf: &Py, other: &PyObject, op: PyComparisonOp, vm: &VirtualMachine,