]> granicus.if.org Git - python/commitdiff
Issue #16373: Prevent infinite recursion for ABC Set class comparisons.
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 6 Dec 2013 21:23:15 +0000 (23:23 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 6 Dec 2013 21:23:15 +0000 (23:23 +0200)
Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index 0438afda2851567d98e0487991d9729bc5fd8f7e..8b650a7763f423fb425fbc78a5c0577482ee6809 100644 (file)
@@ -165,12 +165,12 @@ class Set(Sized, Iterable, Container):
     def __gt__(self, other):
         if not isinstance(other, Set):
             return NotImplemented
-        return other < self
+        return other.__lt__(self)
 
     def __ge__(self, other):
         if not isinstance(other, Set):
             return NotImplemented
-        return other <= self
+        return other.__le__(self)
 
     def __eq__(self, other):
         if not isinstance(other, Set):
index 3aaecb2a6f9b64d1957844befaeb65fea66d98fa..784a31880e3bf1709609abb91b6573d35256850c 100644 (file)
@@ -594,6 +594,35 @@ class TestCollectionABCs(ABCTestCase):
         s |= s
         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()
+        self.assertFalse(ncs < cs)
+        self.assertFalse(ncs <= cs)
+        self.assertFalse(cs > ncs)
+        self.assertFalse(cs >= ncs)
+
     def test_Mapping(self):
         for sample in [dict]:
             self.assertIsInstance(sample(), Mapping)
index 1b59bfdb481679326547798c1976a67b6e62b4ed..059f7bfe09955d5529dd7e188f141bb5f7aa4530 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #16373: Prevent infinite recursion for ABC Set class comparisons.
+
 - Issue #19138: doctest's IGNORE_EXCEPTION_DETAIL now allows a match when
   no exception detail exists (no colon following the exception's name, or
   a colon does follow but no text follows the colon).