From f27271619e7606338d194d669bbab4823517f7be Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 7 Dec 2023 14:41:00 +0100 Subject: [PATCH] =?utf8?q?[3.12]=20gh-112125:=20Fix=20None.=5F=5Fne=5F=5F(?= =?utf8?q?None)=20returning=20NotImplemented=20instead=20of=20=E2=80=A6=20?= =?utf8?q?(#112827)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit gh-112125: Fix None.__ne__(None) returning NotImplemented instead of False (#112504) (cherry picked from commit 9c3458e05865093dd55d7608810a9d0ef0765978) Co-authored-by: andrewluotechnologies <44252973+andrewluotechnologies@users.noreply.github.com> --- Include/internal/pycore_typeobject.h | 2 ++ Lib/test/test_builtin.py | 5 +++++ .../2023-12-07-13-19-55.gh-issue-112125.4ADN7i.rst | 1 + Objects/object.c | 2 +- Objects/typeobject.c | 6 ++++++ 5 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-12-07-13-19-55.gh-issue-112125.4ADN7i.rst diff --git a/Include/internal/pycore_typeobject.h b/Include/internal/pycore_typeobject.h index 8f3fbbcdb5ff..63f76fc55c9b 100644 --- a/Include/internal/pycore_typeobject.h +++ b/Include/internal/pycore_typeobject.h @@ -133,6 +133,8 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *suppress_missing PyObject * _Py_type_getattro(PyTypeObject *type, PyObject *name); +extern PyObject* _Py_BaseObject_RichCompare(PyObject* self, PyObject* other, int op); + PyObject *_Py_slot_tp_getattro(PyObject *self, PyObject *name); PyObject *_Py_slot_tp_getattr_hook(PyObject *self, PyObject *name); diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index c3857195177b..de83b523f986 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -602,6 +602,11 @@ class BuiltinTest(unittest.TestCase): # test that object has a __dir__() self.assertEqual(sorted([].__dir__()), dir([])) + def test___ne__(self): + self.assertFalse(None.__ne__(None)) + self.assertTrue(None.__ne__(0)) + self.assertTrue(None.__ne__("abc")) + def test_divmod(self): self.assertEqual(divmod(12, 7), (1, 5)) self.assertEqual(divmod(-12, 7), (-2, 2)) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-07-13-19-55.gh-issue-112125.4ADN7i.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-07-13-19-55.gh-issue-112125.4ADN7i.rst new file mode 100644 index 000000000000..52cd45029fb8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-12-07-13-19-55.gh-issue-112125.4ADN7i.rst @@ -0,0 +1 @@ +Fix None.__ne__(None) returning NotImplemented instead of False diff --git a/Objects/object.c b/Objects/object.c index 8a4010fb1366..aac707d6a26c 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1878,7 +1878,7 @@ PyTypeObject _PyNone_Type = { 0, /*tp_doc */ 0, /*tp_traverse */ 0, /*tp_clear */ - 0, /*tp_richcompare */ + _Py_BaseObject_RichCompare, /*tp_richcompare */ 0, /*tp_weaklistoffset */ 0, /*tp_iter */ 0, /*tp_iternext */ diff --git a/Objects/typeobject.c b/Objects/typeobject.c index d0c7c5f9439c..71d20687479a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5593,6 +5593,12 @@ object_richcompare(PyObject *self, PyObject *other, int op) return res; } +PyObject* +_Py_BaseObject_RichCompare(PyObject* self, PyObject* other, int op) +{ + return object_richcompare(self, other, op); +} + static PyObject * object_get_class(PyObject *self, void *closure) { -- 2.47.3