diff options
-rw-r--r-- | Lib/sets.py | 2 | ||||
-rw-r--r-- | Lib/test/test_sets.py | 25 |
2 files changed, 27 insertions, 0 deletions
diff --git a/Lib/sets.py b/Lib/sets.py index 5f0f0a2..bbb93a0 100644 --- a/Lib/sets.py +++ b/Lib/sets.py @@ -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: diff --git a/Lib/test/test_sets.py b/Lib/test/test_sets.py index 840036c..76b56b1 100644 --- a/Lib/test/test_sets.py +++ b/Lib/test/test_sets.py @@ -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, |