[pypy-svn] pypy default: Fix for itertools.product with iterables who can't have their len() taken.

alex_gaynor commits-noreply at bitbucket.org
Mon Jan 24 21:15:08 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41269:f0d22efaa1d3
Date: 2011-01-24 15:13 -0500
http://bitbucket.org/pypy/pypy/changeset/f0d22efaa1d3/

Log:	Fix for itertools.product with iterables who can't have their len()
	taken.

diff --git a/pypy/module/itertools/test/test_itertools.py b/pypy/module/itertools/test/test_itertools.py
--- a/pypy/module/itertools/test/test_itertools.py
+++ b/pypy/module/itertools/test/test_itertools.py
@@ -728,8 +728,10 @@
         m = ['a', 'b']
 
         prodlist = product(l, m)
-        assert list(prodlist) == [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
+        res = [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
+        assert list(prodlist) == res
         assert list(product([])) == []
+        assert list(product(iter(l), iter(m))) == res
 
     def test_product_repeat(self):
         from itertools import product

diff --git a/pypy/module/itertools/interp_itertools.py b/pypy/module/itertools/interp_itertools.py
--- a/pypy/module/itertools/interp_itertools.py
+++ b/pypy/module/itertools/interp_itertools.py
@@ -1038,11 +1038,15 @@
 
 class W_Product(Wrappable):
     def __init__(self, space, args_w, w_repeat):
-        self.gears_w = args_w * space.int_w(w_repeat)
+        self.gears_w = [
+            space.fixedview(arg_w) for arg_w in args_w
+        ] * space.int_w(w_repeat)
         self.num_gears = len(self.gears_w)
         # initialization of indicies to loop over
-        self.indicies = [(0, space.int_w(space.len(w_gear)))
-                         for w_gear in self.gears_w]
+        self.indicies = [
+            (0, len(w_gear))
+            for w_gear in self.gears_w
+        ]
         self.cont = True
 
     def roll_gears(self):
@@ -1082,10 +1086,10 @@
         l = [None] * self.num_gears
         for x in range(0, self.num_gears):
             index, limit = self.indicies[x]
-            if space.int_w(space.len(self.gears_w[x])) == 0:
+            if len(self.gears_w[x]) == 0:
                 self.cont = False
                 raise OperationError(space.w_StopIteration, space.w_None)
-            l[x] = space.getitem(self.gears_w[x], space.wrap(index))
+            l[x] = self.gears_w[x][index]
         self.roll_gears()
         return space.newtuple(l)
 


More information about the Pypy-commit mailing list