mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Fix the total_ordering decorator to handle cross-type comparisons
that could lead to infinite recursion (closes #10042).
This commit is contained in:
parent
5136867c1b
commit
374274db7f
4 changed files with 36 additions and 8 deletions
|
@ -361,6 +361,8 @@ class TestTotalOrdering(unittest.TestCase):
|
|||
self.value = value
|
||||
def __lt__(self, other):
|
||||
return self.value < other.value
|
||||
def __eq__(self, other):
|
||||
return self.value == other.value
|
||||
self.assertTrue(A(1) < A(2))
|
||||
self.assertTrue(A(2) > A(1))
|
||||
self.assertTrue(A(1) <= A(2))
|
||||
|
@ -375,6 +377,8 @@ class TestTotalOrdering(unittest.TestCase):
|
|||
self.value = value
|
||||
def __le__(self, other):
|
||||
return self.value <= other.value
|
||||
def __eq__(self, other):
|
||||
return self.value == other.value
|
||||
self.assertTrue(A(1) < A(2))
|
||||
self.assertTrue(A(2) > A(1))
|
||||
self.assertTrue(A(1) <= A(2))
|
||||
|
@ -389,6 +393,8 @@ class TestTotalOrdering(unittest.TestCase):
|
|||
self.value = value
|
||||
def __gt__(self, other):
|
||||
return self.value > other.value
|
||||
def __eq__(self, other):
|
||||
return self.value == other.value
|
||||
self.assertTrue(A(1) < A(2))
|
||||
self.assertTrue(A(2) > A(1))
|
||||
self.assertTrue(A(1) <= A(2))
|
||||
|
@ -403,6 +409,8 @@ class TestTotalOrdering(unittest.TestCase):
|
|||
self.value = value
|
||||
def __ge__(self, other):
|
||||
return self.value >= other.value
|
||||
def __eq__(self, other):
|
||||
return self.value == other.value
|
||||
self.assertTrue(A(1) < A(2))
|
||||
self.assertTrue(A(2) > A(1))
|
||||
self.assertTrue(A(1) <= A(2))
|
||||
|
@ -428,6 +436,22 @@ class TestTotalOrdering(unittest.TestCase):
|
|||
class A:
|
||||
pass
|
||||
|
||||
def test_bug_10042(self):
|
||||
@functools.total_ordering
|
||||
class TestTO:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, TestTO):
|
||||
return self.value == other.value
|
||||
return False
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, TestTO):
|
||||
return self.value < other.value
|
||||
raise TypeError
|
||||
with self.assertRaises(TypeError):
|
||||
TestTO(8) <= ()
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_classes = (
|
||||
TestPartial,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue