]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #1393: object_richcompare() returns NotImplemented instead of
authorGuido van Rossum <guido@python.org>
Sun, 6 Jan 2008 00:09:11 +0000 (00:09 +0000)
committerGuido van Rossum <guido@python.org>
Sun, 6 Jan 2008 00:09:11 +0000 (00:09 +0000)
 False if the objects aren't equal, to give the other side a chance.

Lib/test/test_compare.py
Misc/NEWS
Objects/typeobject.c

index c6608379a13dbca0fe256cbd467a236eaa96ceab..15fe3c20222fb0b3bb17f8553b2919e48a4351c8 100644 (file)
@@ -16,6 +16,13 @@ class Cmp:
     def __eq__(self, other):
         return self.arg == other
 
+class Anything:
+    def __eq__(self, other):
+        return True
+
+    def __ne__(self, other):
+        return False
+
 class ComparisonTest(unittest.TestCase):
     set1 = [2, 2.0, 2, 2+0j, Cmp(2.0)]
     set2 = [[1], (3,), None, Empty()]
@@ -45,6 +52,15 @@ class ComparisonTest(unittest.TestCase):
         self.assertTrue(a == b)
         self.assertFalse(a != b)
 
+    def test_issue_1393(self):
+        x = lambda: None
+        self.assertEqual(x, Anything())
+        self.assertEqual(Anything(), x)
+        y = object()
+        self.assertEqual(y, Anything())
+        self.assertEqual(Anything(), y)
+
+
 def test_main():
     test_support.run_unittest(ComparisonTest)
 
index 22c4f869c51ad8d28a86f6964589bb7a44c687f6..72a28840e15ce15d126ee79ebdfcb071f284a9da 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.0a3?
 Core and Builtins
 -----------------
 
+- Issue #1393: object_richcompare() returns NotImplemented instead of
+  False if the objects aren't equal, to give the other side a chance.
+
 - Issue #1692: Interpreter was not displaying location of SyntaxError
 
 - Improve some exception messages when Windows fails to load an extension
index 982eedb42558c5a8c4fd32c8fb85f8dd1ea37c91..2a0dd2458aa9b0ee00fd4f45033e7cfa21d0ffb9 100644 (file)
@@ -2484,7 +2484,10 @@ object_richcompare(PyObject *self, PyObject *other, int op)
        switch (op) {
 
        case Py_EQ:
-               res = (self == other) ? Py_True : Py_False;
+               /* Return NotImplemented instead of False, so if two
+                  objects are compared, both get a chance at the
+                  comparison.  See issue #1393. */
+               res = (self == other) ? Py_True : Py_NotImplemented;
                Py_INCREF(res);
                break;