Commits

Lukas Diekmann committed ffa5d9d

added test and fix for set(<generator>)

Comments (0)

Files changed (2)

pypy/objspace/std/setobject.py

 from pypy.objspace.std.frozensettype import frozenset_typedef as frozensettypedef
 from pypy.rlib import rerased
 from pypy.rlib.objectmodel import instantiate
+from pypy.interpreter.generator import GeneratorIterator
+from pypy.objspace.std.listobject import W_ListObject
 
 def get_strategy_from_w_iterable(space, w_iterable=None):
     from pypy.objspace.std.intobject import W_IntObject
 def _initialize_set(space, w_obj, w_iterable=None):
     w_obj.clear()
     if w_iterable is not None:
+        if  isinstance(w_iterable, GeneratorIterator):
+            w_iterable = W_ListObject(space.listview(w_iterable))
         w_obj.strategy = get_strategy_from_w_iterable(space, w_iterable)
         w_obj.strategy.init_from_w_iterable(w_obj, w_iterable)
 

pypy/objspace/std/test/test_setobject.py

         c = a.union(b)
         assert c == set([1,2,3,4])
 
+    def test_generator(self):
+        def foo():
+            for i in [1,2,3,4,5]:
+                yield i
+        b = set(foo())
+        assert b == set([1,2,3,4,5])
+
+        a = set(x for x in [1,2,3])
+        assert a == set([1,2,3])
+
     def test_or(self):
         a = set([0,1,2])
         b = a | set([1,2,3])