]> granicus.if.org Git - python/commitdiff
Closes SF bug #628246.
authorRaymond Hettinger <python@rcn.com>
Fri, 8 Nov 2002 05:03:21 +0000 (05:03 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 8 Nov 2002 05:03:21 +0000 (05:03 +0000)
The _update method detected mutable elements by trapping TypeErrors.
Unfortunately, this masked useful TypeErrors raised by the iterable
itself.  For cases where it is possible for an iterable to raise
a TypeError, the iterable is pre-converted to a list outside the
try/except so that any TypeErrors propagate through.

Lib/sets.py
Lib/test/test_sets.py

index 5f0f0a2d38c18772d6e4e56a44dc162142112bd1..bbb93a091ebd7c2976e0a63fa630650500fac647 100644 (file)
@@ -320,6 +320,8 @@ class BaseSet(object):
             return
 
         value = True
+        if type(iterable) not in (list, tuple, dict, file, xrange, str):
+            iterable = list(iterable)
         it = iter(iterable)
         while True:
             try:
index 840036cc654d8c9f97b700ffdebf8cac8770bc37..76b56b118d861f3449b9e76636c9c0e1cdf4c031 100644 (file)
@@ -132,6 +132,30 @@ class TestBasicOpsTriple(TestBasicOps):
 
 #==============================================================================
 
+def baditer():
+    raise TypeError
+    yield True
+
+def gooditer():
+    yield True
+
+class TestExceptionPropagation(unittest.TestCase):
+    """SF 628246:  Set constructor should not trap iterator TypeErrors"""
+
+    def test_instanceWithException(self):
+        self.assertRaises(TypeError, Set, baditer())
+
+    def test_instancesWithoutException(self):
+        """All of these iterables should load without exception."""
+        Set([1,2,3])
+        Set((1,2,3))
+        Set({'one':1, 'two':2, 'three':3})
+        Set(xrange(3))
+        Set('abc')
+        Set(gooditer())
+
+#==============================================================================
+
 class TestSetOfSets(unittest.TestCase):
     def test_constructor(self):
         inner = Set([1])
@@ -604,6 +628,7 @@ class TestCopyingNested(TestCopying):
 def makeAllTests():
     suite = unittest.TestSuite()
     for klass in (TestSetOfSets,
+                  TestExceptionPropagation,
                   TestBasicOpsEmpty,
                   TestBasicOpsSingleton,
                   TestBasicOpsTuple,