summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-03-27 22:37:34 (GMT)
committerGuido van Rossum <guido@python.org>2007-03-27 22:37:34 (GMT)
commite27dc7230845aa341d9a0bf323fa14912713cf33 (patch)
tree80e5451f5dac1bc5ac01ee3b70eaf9aff0461f31 /Objects
parenta4335b1e6bc357fb6a3d54298e74a1d957028bc3 (diff)
downloadcpython-e27dc7230845aa341d9a0bf323fa14912713cf33.zip
cpython-e27dc7230845aa341d9a0bf323fa14912713cf33.tar.gz
cpython-e27dc7230845aa341d9a0bf323fa14912713cf33.tar.bz2
By default, != returns the opposite of ==, unless the latter returns
NotImplemented. (Is this worth backporting to 2.6? It seems so useful...!)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index e626a17..be6f279 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2314,7 +2314,22 @@ object_richcompare(PyObject *self, PyObject *other, int op)
break;
case Py_NE:
- res = (self != other) ? Py_True : Py_False;
+ /* By default, != returns the opposite of ==,
+ unless the latter returns NotImplemented. */
+ res = PyObject_RichCompare(self, other, Py_EQ);
+ if (res != NULL && res != Py_NotImplemented) {
+ int ok = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (ok < 0)
+ res = NULL;
+ else {
+ if (ok)
+ res = Py_False;
+ else
+ res = Py_True;
+ Py_INCREF(res);
+ }
+ }
break;
default: