diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 14de21f3c67210..21503f1cfa7744 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4300,6 +4300,15 @@ dict_richcompare(PyObject *v, PyObject *w, int op) res = Py_NotImplemented; } else if (op == Py_EQ || op == Py_NE) { + if (v == w) { + switch (op) { + case Py_EQ: + /* a list is equal to itself */ + Py_RETURN_TRUE; + case Py_NE: + Py_RETURN_FALSE; + } + } cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); if (cmp < 0) return NULL; diff --git a/Objects/listobject.c b/Objects/listobject.c index 1722ea60cdc68f..3207f63597113a 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3403,6 +3403,23 @@ list_richcompare_impl(PyObject *v, PyObject *w, int op) vl = (PyListObject *)v; wl = (PyListObject *)w; + if (vl == wl) { + switch (op) { + case Py_EQ: + case Py_LE: + case Py_GE: + /* a list is equal to itself */ + Py_RETURN_TRUE; + case Py_NE: + case Py_LT: + case Py_GT: + Py_RETURN_FALSE; + default: + PyErr_BadArgument(); + return NULL; + } + } + if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) { /* Shortcut: if the lengths differ, the lists differ */ if (op == Py_EQ) diff --git a/Objects/setobject.c b/Objects/setobject.c index 85f4d7d403178a..53bc6a1ce352b4 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -2167,6 +2167,23 @@ set_richcompare(PyObject *self, PyObject *w, int op) if(!PyAnySet_Check(w)) Py_RETURN_NOTIMPLEMENTED; + if (v == w) { + switch (op) { + case Py_EQ: + case Py_LE: + case Py_GE: + /* a list is equal to itself */ + Py_RETURN_TRUE; + case Py_NE: + case Py_LT: + case Py_GT: + Py_RETURN_FALSE; + default: + PyErr_BadArgument(); + return NULL; + } + } + switch (op) { case Py_EQ: if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w)) diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 169ac69701da11..9f881f92260d1f 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -669,6 +669,23 @@ tuple_richcompare(PyObject *v, PyObject *w, int op) vt = (PyTupleObject *)v; wt = (PyTupleObject *)w; + if (vt == wt) { + switch (op) { + case Py_EQ: + case Py_LE: + case Py_GE: + /* a list is equal to itself */ + Py_RETURN_TRUE; + case Py_NE: + case Py_LT: + case Py_GT: + Py_RETURN_FALSE; + default: + PyErr_BadArgument(); + return NULL; + } + } + vlen = Py_SIZE(vt); wlen = Py_SIZE(wt);