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

andrew.svetlov python-checkins at python.org
Thu Nov 1 12:31:59 CET 2012


http://hg.python.org/cpython/rev/8e95a078d490
changeset:   80149:8e95a078d490
branch:      3.2
parent:      80133:838e2b19489e
user:        Andrew Svetlov <andrew.svetlov at gmail.com>
date:        Thu Nov 01 13:28:54 2012 +0200
summary:
  Issue #16373: Prevent infinite recursion for ABC Set class operations.

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


diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py
--- a/Lib/_abcoll.py
+++ b/Lib/_abcoll.py
@@ -184,12 +184,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
@@ -651,6 +651,39 @@
         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()
+        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):
         for sample in [dict]:
             self.assertIsInstance(sample(), Mapping)

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


More information about the Python-checkins mailing list