mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
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.)
This commit is contained in:
parent
928115af72
commit
10ab4aeb86
2 changed files with 38 additions and 6 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue