Author: Lukas Diekmann <[email protected]>
Branch: set-strategies
Changeset: r49148:ffa5d9dadcfe
Date: 2011-05-11 11:19 +0200
http://bitbucket.org/pypy/pypy/changeset/ffa5d9dadcfe/
Log: added test and fix for set(<generator>)
diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -10,6 +10,8 @@
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
@@ -510,6 +512,8 @@
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)
diff --git a/pypy/objspace/std/test/test_setobject.py
b/pypy/objspace/std/test/test_setobject.py
--- a/pypy/objspace/std/test/test_setobject.py
+++ b/pypy/objspace/std/test/test_setobject.py
@@ -59,6 +59,16 @@
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])
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit