diff Objects/typeobject.c @ 41311:834da52eb2a4

By default, != returns the opposite of ==, unless the latter returns NotImplemented. (Is this worth backporting to 2.6? It seems so useful...!)
author Guido van Rossum <guido@python.org>
date Tue, 27 Mar 2007 22:37:34 +0000
parents f2f0b41f270d
children 519e72d630d2
line wrap: on
line diff
--- a/Objects/typeobject.c	Tue Mar 27 22:34:34 2007 +0000
+++ b/Objects/typeobject.c	Tue Mar 27 22:37:34 2007 +0000
@@ -2314,7 +2314,22 @@
 		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: