summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-08-23 23:57:24 (GMT)
committerGuido van Rossum <guido@python.org>2007-08-23 23:57:24 (GMT)
commit10ab4aeb86d72dc2a454ebd09693870a8e5cab4b (patch)
treea41c95cdcb34068304c15e119a734682ff17d4c3
parent928115af726eb620c051933d61c5ac06aa86bd86 (diff)
downloadcpython-10ab4aeb86d72dc2a454ebd09693870a8e5cab4b.zip
cpython-10ab4aeb86d72dc2a454ebd09693870a8e5cab4b.tar.gz
cpython-10ab4aeb86d72dc2a454ebd09693870a8e5cab4b.tar.bz2
Patch by Keir Mierle so that sets can be compared to other objects that know
how to compare themselves to sets. (Prep work for making dict views more set-like.)
-rw-r--r--Lib/test/test_set.py36
-rw-r--r--Objects/setobject.c8
2 files changed, 38 insertions, 6 deletions
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index df72fb2..3b8e747 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -492,6 +492,42 @@ class TestSet(TestJointOps):
s = None
self.assertRaises(ReferenceError, str, p)
+ def test_rich_compare(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 bulitin 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 = {1, 2, 3}
+
+ myobj = TestRichSetCompare()
+ myset < myobj
+ self.assert_(myobj.gt_called)
+
+ myobj = TestRichSetCompare()
+ myset > myobj
+ self.assert_(myobj.lt_called)
+
+ myobj = TestRichSetCompare()
+ myset <= myobj
+ self.assert_(myobj.ge_called)
+
+ myobj = TestRichSetCompare()
+ myset >= myobj
+ self.assert_(myobj.le_called)
+
# C API test only available in a debug build
if hasattr(set, "test_c_api"):
def test_c_api(self):
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 7ff27b1..079f404 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1607,12 +1607,8 @@ set_richcompare(PySetObject *v, PyObject *w, int op)
PyObject *r1, *r2;
if(!PyAnySet_Check(w)) {
- if (op == Py_EQ)
- Py_RETURN_FALSE;
- if (op == Py_NE)
- Py_RETURN_TRUE;
- PyErr_SetString(PyExc_TypeError, "can only compare to a set");
- return NULL;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
switch (op) {
case Py_EQ: