Skip to content

Commit f272716

Browse files
[3.12] gh-112125: Fix None.__ne__(None) returning NotImplemented instead of … (#112827)
gh-112125: Fix None.__ne__(None) returning NotImplemented instead of False (#112504) (cherry picked from commit 9c3458e) Co-authored-by: andrewluotechnologies <44252973+andrewluotechnologies@users.noreply.github.com>
1 parent e21a7a9 commit f272716

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

Include/internal/pycore_typeobject.h

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *suppress_missing
133133
PyObject *
134134
_Py_type_getattro(PyTypeObject *type, PyObject *name);
135135

136+
extern PyObject* _Py_BaseObject_RichCompare(PyObject* self, PyObject* other, int op);
137+
136138
PyObject *_Py_slot_tp_getattro(PyObject *self, PyObject *name);
137139
PyObject *_Py_slot_tp_getattr_hook(PyObject *self, PyObject *name);
138140

Lib/test/test_builtin.py

+5
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,11 @@ def __dir__(self):
602602
# test that object has a __dir__()
603603
self.assertEqual(sorted([].__dir__()), dir([]))
604604

605+
def test___ne__(self):
606+
self.assertFalse(None.__ne__(None))
607+
self.assertTrue(None.__ne__(0))
608+
self.assertTrue(None.__ne__("abc"))
609+
605610
def test_divmod(self):
606611
self.assertEqual(divmod(12, 7), (1, 5))
607612
self.assertEqual(divmod(-12, 7), (-2, 2))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix None.__ne__(None) returning NotImplemented instead of False

Objects/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ PyTypeObject _PyNone_Type = {
18781878
0, /*tp_doc */
18791879
0, /*tp_traverse */
18801880
0, /*tp_clear */
1881-
0, /*tp_richcompare */
1881+
_Py_BaseObject_RichCompare, /*tp_richcompare */
18821882
0, /*tp_weaklistoffset */
18831883
0, /*tp_iter */
18841884
0, /*tp_iternext */

Objects/typeobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -5593,6 +5593,12 @@ object_richcompare(PyObject *self, PyObject *other, int op)
55935593
return res;
55945594
}
55955595

5596+
PyObject*
5597+
_Py_BaseObject_RichCompare(PyObject* self, PyObject* other, int op)
5598+
{
5599+
return object_richcompare(self, other, op);
5600+
}
5601+
55965602
static PyObject *
55975603
object_get_class(PyObject *self, void *closure)
55985604
{

0 commit comments

Comments
 (0)