Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r64242:5ea7a1017b30
Date: 2013-05-16 13:24 -0700
http://bitbucket.org/pypy/pypy/changeset/5ea7a1017b30/

Log:    loosen set's rich comparisons

diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -196,8 +196,7 @@
     # correct answer here!
     def descr_lt(self, space, w_other):
         if not isinstance(w_other, W_BaseSetObject):
-            raise OperationError(self.space.w_TypeError,
-                                 self.space.wrap('can only compare to a set'))
+            return space.w_NotImplemented
 
         if self.length() >= w_other.length():
             return space.w_False
@@ -206,8 +205,7 @@
 
     def descr_le(self, space, w_other):
         if not isinstance(w_other, W_BaseSetObject):
-            raise OperationError(self.space.w_TypeError,
-                                 self.space.wrap('can only compare to a set'))
+            return space.w_NotImplemented
 
         if self.length() > w_other.length():
             return space.w_False
@@ -215,8 +213,7 @@
 
     def descr_gt(self, space, w_other):
         if not isinstance(w_other, W_BaseSetObject):
-            raise OperationError(self.space.w_TypeError,
-                                 self.space.wrap('can only compare to a set'))
+            return space.w_NotImplemented
 
         if self.length() <= w_other.length():
             return space.w_False
@@ -225,8 +222,7 @@
 
     def descr_ge(self, space, w_other):
         if not isinstance(w_other, W_BaseSetObject):
-            raise OperationError(self.space.w_TypeError,
-                                 self.space.wrap('can only compare to a set'))
+            return space.w_NotImplemented
 
         if self.length() < w_other.length():
             return space.w_False
diff --git a/pypy/objspace/std/test/test_setobject.py 
b/pypy/objspace/std/test/test_setobject.py
--- a/pypy/objspace/std/test/test_setobject.py
+++ b/pypy/objspace/std/test/test_setobject.py
@@ -385,6 +385,42 @@
         assert set() != set('abc')
         assert set('abc') != set('abd')
 
+    def test_compare_other(self):
+        class TestRichSetCompare:
+            def __gt__(self, some_set):
+                self.gt_called = True
+                return False
+            def __lt__(self, some_set):
+                self.lt_called = True
+                return False
+            def __ge__(self, some_set):
+                self.ge_called = True
+                return False
+            def __le__(self, some_set):
+                self.le_called = True
+                return False
+
+        # This first tries the builtin rich set comparison, which doesn't know
+        # how to handle the custom object. Upon returning NotImplemented, the
+        # corresponding comparison on the right object is invoked.
+        myset = set(range(3))
+
+        myobj = TestRichSetCompare()
+        myset < myobj
+        assert myobj.gt_called
+
+        myobj = TestRichSetCompare()
+        myset > myobj
+        assert myobj.lt_called
+
+        myobj = TestRichSetCompare()
+        myset <= myobj
+        assert myobj.ge_called
+
+        myobj = TestRichSetCompare()
+        myset >= myobj
+        assert myobj.le_called
+
     def test_libpython_equality(self):
         for thetype in [frozenset, set]:
             word = "aaaaaaaaawfpasrtarspawparst"
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to