[Python-checkins] cpython (2.7): Issue #16373: Prevent infinite recursion for ABC Set class comparisons.

serhiy.storchaka python-checkins at python.org
Fri Dec 6 22:24:28 CET 2013


http://hg.python.org/cpython/rev/9bf55766d935
changeset:   87797:9bf55766d935
branch:      2.7
parent:      87789:80142c15a920
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Dec 06 23:23:15 2013 +0200
summary:
  Issue #16373: Prevent infinite recursion for ABC Set class comparisons.

files:
  Lib/_abcoll.py               |   4 +-
  Lib/test/test_collections.py |  29 ++++++++++++++++++++++++
  Misc/NEWS                    |   2 +
  3 files changed, 33 insertions(+), 2 deletions(-)


diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py
--- a/Lib/_abcoll.py
+++ b/Lib/_abcoll.py
@@ -165,12 +165,12 @@
     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):
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -594,6 +594,35 @@
         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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,8 @@
 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).

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list