Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58337:8d08cc91b583
Date: 2012-10-21 08:36 +0200
http://bitbucket.org/pypy/pypy/changeset/8d08cc91b583/

Log:    __eq__ should be called even when the object is compared with
        itself. This accounts for at least two failures, in test_weakref.py
        and test_decimal.py

diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -543,12 +543,6 @@
     left, right = specialnames
     op = getattr(operator, left)
     def comparison_impl(space, w_obj1, w_obj2):
-        # for == and !=, we do a quick check for identity.  This also
-        # guarantees that identity implies equality.
-        if left == '__eq__' or left == '__ne__':
-            if space.is_w(w_obj1, w_obj2):
-                return space.wrap(left == '__eq__')
-        #
         w_typ1 = space.type(w_obj1)
         w_typ2 = space.type(w_obj2)
         w_left_src, w_left_impl = space.lookup_in_type_where(w_typ1, left)
@@ -582,12 +576,11 @@
         # we did not find any special method, let's do the default logic for
         # == and !=
         if left == '__eq__':
-            # they are not identical, else it would have been caught by the if
-            # at the top of the function
-            assert not space.is_w(w_obj1, w_obj2)
-            return space.w_False
+            if space.is_w(w_obj1, w_obj2):
+                return space.w_True
+            else:
+                return space.w_False
         elif left == '__ne__':
-            assert not space.is_w(w_obj1, w_obj2)
             return space.not_(space.eq(w_obj1, w_obj2))
         #
         # if we arrived here, they are unorderable
diff --git a/pypy/objspace/test/test_descroperation.py 
b/pypy/objspace/test/test_descroperation.py
--- a/pypy/objspace/test/test_descroperation.py
+++ b/pypy/objspace/test/test_descroperation.py
@@ -396,6 +396,16 @@
         assert res1 == res2 == 123
         assert l == [A, B, B, A]
 
+    def test__eq__called(self):
+        l = []
+        class A(object):
+            def __eq__(self, other):
+                l.append((self, other))
+                return True
+        a = A()
+        a == a
+        assert l == [(a, a)]
+
     def test_subclass_comparison(self):
         # the __eq__ *is* called with reversed arguments
         l = []
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to