mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Merge issue #16373: Prevent infinite recursion for ABC Set class operations.
Patch by Serhiy Storchaka.
This commit is contained in:
commit
b904e4256e
2 changed files with 35 additions and 2 deletions
|
@ -200,12 +200,12 @@ class Set(Sized, Iterable, Container):
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
if not isinstance(other, Set):
|
if not isinstance(other, Set):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return other < self
|
return other.__lt__(self)
|
||||||
|
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
if not isinstance(other, Set):
|
if not isinstance(other, Set):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return other <= self
|
return other.__le__(self)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, Set):
|
if not isinstance(other, Set):
|
||||||
|
|
|
@ -663,6 +663,39 @@ class TestCollectionABCs(ABCTestCase):
|
||||||
s |= s
|
s |= s
|
||||||
self.assertEqual(s, full)
|
self.assertEqual(s, full)
|
||||||
|
|
||||||
|
def test_issue16373(self):
|
||||||
|
# Recursion error comparing comparable and noncomparable
|
||||||
|
# Set instances
|
||||||
|
class MyComparableSet(Set):
|
||||||
|
def __contains__(self, x):
|
||||||
|
return False
|
||||||
|
def __len__(self):
|
||||||
|
return 0
|
||||||
|
def __iter__(self):
|
||||||
|
return iter([])
|
||||||
|
class MyNonComparableSet(Set):
|
||||||
|
def __contains__(self, x):
|
||||||
|
return False
|
||||||
|
def __len__(self):
|
||||||
|
return 0
|
||||||
|
def __iter__(self):
|
||||||
|
return iter([])
|
||||||
|
def __le__(self, x):
|
||||||
|
return NotImplemented
|
||||||
|
def __lt__(self, x):
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
cs = MyComparableSet()
|
||||||
|
ncs = MyNonComparableSet()
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
ncs < cs
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
ncs <= cs
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
cs > ncs
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
cs >= ncs
|
||||||
|
|
||||||
def test_Mapping(self):
|
def test_Mapping(self):
|
||||||
for sample in [dict]:
|
for sample in [dict]:
|
||||||
self.assertIsInstance(sample(), Mapping)
|
self.assertIsInstance(sample(), Mapping)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue