[pypy-commit] pypy py3k: Updated itertools functions for py3k.

prestontimmons noreply at buildbot.pypy.org
Wed Mar 14 19:34:41 CET 2012


Author: Preston Timmons <prestontimmons at gmail.com>
Branch: py3k
Changeset: r53569:3d88633c8e56
Date: 2012-03-13 18:45 +0000
http://bitbucket.org/pypy/pypy/changeset/3d88633c8e56/

Log:	Updated itertools functions for py3k.

	 - The izip function no longer exists
	  - izip_longest was renamed to zip_longest

diff --git a/pypy/module/itertools/__init__.py b/pypy/module/itertools/__init__.py
--- a/pypy/module/itertools/__init__.py
+++ b/pypy/module/itertools/__init__.py
@@ -10,7 +10,6 @@
     repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times
 
     Iterators terminating on the shortest input sequence:
-    izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... 
     ifilter(pred, seq) --> elements of seq where pred(elem) is True
     ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False
     islice(seq, [start,] stop [, step]) --> elements from
@@ -37,14 +36,13 @@
         'ifilterfalse'  : 'interp_itertools.W_IFilterFalse',
         'imap'          : 'interp_itertools.W_IMap',
         'islice'        : 'interp_itertools.W_ISlice',
-        'izip'          : 'interp_itertools.W_IZip',
-        'izip_longest'  : 'interp_itertools.W_IZipLongest',
         'permutations'  : 'interp_itertools.W_Permutations',
         'product'       : 'interp_itertools.W_Product',
         'repeat'        : 'interp_itertools.W_Repeat',
         'starmap'       : 'interp_itertools.W_StarMap',
         'takewhile'     : 'interp_itertools.W_TakeWhile',
         'tee'           : 'interp_itertools.tee',
+        'zip_longest'  : 'interp_itertools.W_ZipLongest',
     }
 
     appleveldefs = {
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
@@ -67,8 +67,7 @@
         __doc__ = """Make an iterator that returns consecutive integers starting
     with n.  If not specified n defaults to zero. Does not currently
     support python long integers. Often used as an argument to imap()
-    to generate consecutive data points.  Also, used with izip() to
-    add sequence numbers.
+    to generate consecutive data points.
 
     Equivalent to :
 
@@ -127,8 +126,7 @@
         __doc__  = """Make an iterator that returns object over and over again.
     Runs indefinitely unless the times argument is specified.  Used
     as argument to imap() for invariant parameters to the called
-    function. Also used with izip() to create an invariant part of a
-    tuple record.
+    function.
 
     Equivalent to :
 
@@ -581,45 +579,8 @@
     """)
 
 
-class W_IZip(W_IMap):
-    _error_name = "izip"
-
-    def next_w(self):
-        # argh.  izip(*args) is almost like imap(None, *args) except
-        # that the former needs a special case for len(args)==0
-        # while the latter just raises a TypeError in this situation.
-        if len(self.iterators_w) == 0:
-            raise OperationError(self.space.w_StopIteration, self.space.w_None)
-        return W_IMap.next_w(self)
-
-def W_IZip___new__(space, w_subtype, args_w):
-    r = space.allocate_instance(W_IZip, w_subtype)
-    r.__init__(space, space.w_None, args_w)
-    return space.wrap(r)
-
-W_IZip.typedef = TypeDef(
-        'izip',
-        __module__ = 'itertools',
-        __new__  = interp2app(W_IZip___new__),
-        __iter__ = interp2app(W_IZip.iter_w),
-        __next__ = interp2app(W_IZip.next_w),
-        __doc__  = """Make an iterator that aggregates elements from each of the
-    iterables.  Like zip() except that it returns an iterator instead
-    of a list. Used for lock-step iteration over several iterables at
-    a time.
-
-    Equivalent to :
-
-    def izip(*iterables):
-        iterables = map(iter, iterables)
-        while iterables:
-            result = [i.next() for i in iterables]
-            yield tuple(result)
-    """)
-
-
-class W_IZipLongest(W_IMap):
-    _error_name = "izip_longest"
+class W_ZipLongest(W_IMap):
+    _error_name = "zip_longest"
 
     def next_w(self):
         space = self.space
@@ -648,7 +609,7 @@
             objects_w[index] = w_value
         return space.newtuple(objects_w)
 
-def W_IZipLongest___new__(space, w_subtype, __args__):
+def W_ZipLongest___new__(space, w_subtype, __args__):
     arguments_w, kwds_w = __args__.unpack()
     w_fillvalue = space.w_None
     if kwds_w:
@@ -657,22 +618,22 @@
             del kwds_w["fillvalue"]
         if kwds_w:
             raise OperationError(space.w_TypeError, space.wrap(
-                "izip_longest() got unexpected keyword argument(s)"))
+                "zip_longest() got unexpected keyword argument(s)"))
 
-    self = space.allocate_instance(W_IZipLongest, w_subtype)
+    self = space.allocate_instance(W_ZipLongest, w_subtype)
     self.__init__(space, space.w_None, arguments_w)
     self.w_fillvalue = w_fillvalue
     self.active = len(self.iterators_w)
 
     return space.wrap(self)
 
-W_IZipLongest.typedef = TypeDef(
-        'izip_longest',
+W_ZipLongest.typedef = TypeDef(
+        'zip_longest',
         __module__ = 'itertools',
-        __new__  = interp2app(W_IZipLongest___new__),
-        __iter__ = interp2app(W_IZipLongest.iter_w),
-        __next__ = interp2app(W_IZipLongest.next_w),
-        __doc__  = """Return an izip_longest object whose .next() method returns a tuple where
+        __new__  = interp2app(W_ZipLongest___new__),
+        __iter__ = interp2app(W_ZipLongest.iter_w),
+        __next__ = interp2app(W_ZipLongest.next_w),
+        __doc__  = """Return a zip_longest object whose .next() method returns a tuple where
     the i-th element comes from the i-th iterable argument.  The .next()
     method continues until the longest iterable in the argument sequence
     is exhausted and then it raises StopIteration.  When the shorter iterables
@@ -1068,7 +1029,7 @@
 
        def compress(data, selectors):
            # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
-           return (d for d, s in izip(data, selectors) if s)
+           return (d for d, s in zip(data, selectors) if s)
 """)
 
 
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
@@ -356,53 +356,6 @@
         raises(TypeError, itertools.imap, bool)
         raises(TypeError, itertools.imap, 42)
 
-    def test_izip(self):
-        import itertools
-
-        it = itertools.izip()
-        raises(StopIteration, next, it)
-
-        obj_list = [object(), object(), object()]
-        it = itertools.izip(obj_list)
-        for x in obj_list:
-            assert next(it) == (x, )
-        raises(StopIteration, next, it)
-        
-        it = itertools.izip([1, 2, 3], [4], [5, 6])
-        assert next(it) == (1, 4, 5)
-        raises(StopIteration, next, it)
-        
-        it = itertools.izip([], [], [1], [])
-        raises(StopIteration, next, it)
-
-        # Up to one additional item may be consumed per iterable, as per python docs
-        it1 = iter([1, 2, 3, 4, 5, 6])
-        it2 = iter([5, 6])
-        it = itertools.izip(it1, it2)
-        for x in [(1, 5), (2, 6)]:
-            assert next(it) == x
-        raises(StopIteration, next, it)
-        assert next(it1) in [3, 4]
-        #---does not work in CPython 2.5
-        #raises(StopIteration, it.next)
-        #assert it1.next() in [4, 5]
-
-    def test_izip_wrongargs(self):
-        import itertools, re
-        
-        # Duplicate python 2.4 behaviour for invalid arguments
-        raises(TypeError, itertools.izip, None, 0)
-
-        # The error message should indicate which argument was dodgy
-        for x in range(10):
-            args = [()] * x + [None] + [()] * (9 - x)
-            try:
-                itertools.izip(*args)
-            except TypeError as e:
-                assert str(e).find("#" + str(x + 1) + " ") >= 0
-            else:
-                fail("TypeError expected")
-
     def test_cycle(self):
         import itertools
 
@@ -613,7 +566,6 @@
             itertools.ifilterfalse(None, []),
             itertools.imap(None, []),
             itertools.islice([], 0),
-            itertools.izip(),
             itertools.repeat(None),
             itertools.starmap(bool, []),
             itertools.takewhile(bool, []),
@@ -641,7 +593,6 @@
             itertools.ifilterfalse,
             itertools.imap,
             itertools.islice,
-            itertools.izip,
             itertools.repeat,
             itertools.starmap,
             itertools.takewhile,
@@ -704,8 +655,8 @@
         raises(ValueError, combinations, "abc", -2)
         assert list(combinations(range(4), 3)) == [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
 
-    def test_iziplongest(self):
-        from itertools import izip_longest, islice, count
+    def test_ziplongest(self):
+        from itertools import zip_longest, islice, count
         for args in [
                 ['abc', range(6)],
                 [range(6), 'abc'],
@@ -717,29 +668,29 @@
             # this is the replacement:
             target = [tuple([arg[i] if i < len(arg) else None for arg in args])
                       for i in range(max(map(len, args)))]
-            assert list(izip_longest(*args)) == target
-            assert list(izip_longest(*args, **{})) == target
+            assert list(zip_longest(*args)) == target
+            assert list(zip_longest(*args, **{})) == target
 
             # Replace None fills with 'X'
             target = [tuple((e is None and 'X' or e) for e in t) for t in target]
-            assert list(izip_longest(*args, **dict(fillvalue='X'))) ==  target
+            assert list(zip_longest(*args, **dict(fillvalue='X'))) ==  target
 
         # take 3 from infinite input
-        assert (list(islice(izip_longest('abcdef', count()),3)) ==
+        assert (list(islice(zip_longest('abcdef', count()),3)) ==
                 zip('abcdef', range(3)))
 
-        assert list(izip_longest()) == zip()
-        assert list(izip_longest([])) ==  zip([])
-        assert list(izip_longest('abcdef')) ==  zip('abcdef')
+        assert list(zip_longest()) == zip()
+        assert list(zip_longest([])) ==  zip([])
+        assert list(zip_longest('abcdef')) ==  zip('abcdef')
 
-        assert (list(izip_longest('abc', 'defg', **{})) ==
+        assert (list(zip_longest('abc', 'defg', **{})) ==
                 zip(list('abc') + [None], 'defg'))  # empty keyword dict
-        raises(TypeError, izip_longest, 3)
-        raises(TypeError, izip_longest, range(3), 3)
+        raises(TypeError, zip_longest, 3)
+        raises(TypeError, zip_longest, range(3), 3)
 
         for stmt in [
-            "izip_longest('abc', fv=1)",
-            "izip_longest('abc', fillvalue=1, bogus_keyword=None)",
+            "zip_longest('abc', fv=1)",
+            "zip_longest('abc', fillvalue=1, bogus_keyword=None)",
         ]:
             try:
                 eval(stmt, globals(), locals())
@@ -748,7 +699,7 @@
             else:
                 self.fail('Did not raise Type in:  ' + stmt)
 
-    def test_izip_longest2(self):
+    def test_zip_longest2(self):
         import itertools
         class Repeater(object):
             # this class is similar to itertools.repeat
@@ -771,7 +722,7 @@
         r2 = Repeater(2, 4, StopIteration)
         def run(r1, r2):
             result = []
-            for i, j in itertools.izip_longest(r1, r2, fillvalue=0):
+            for i, j in itertools.zip_longest(r1, r2, fillvalue=0):
                 result.append((i, j))
             return result
         assert run(r1, r2) ==  [(1,2), (1,2), (1,2), (0,2)]
@@ -939,7 +890,7 @@
         assert list(combinations_with_replacement([], 2)) == []
         assert list(combinations_with_replacement([], 0)) == [()]
 
-    def test_izip_longest3(self):
+    def test_zip_longest3(self):
         import itertools
         class Repeater(object):
             # this class is similar to itertools.repeat
@@ -960,7 +911,7 @@
         # and StopIteration would stop as expected
         r1 = Repeater(1, 3, RuntimeError)
         r2 = Repeater(2, 4, StopIteration)
-        it = itertools.izip_longest(r1, r2, fillvalue=0)
+        it = itertools.zip_longest(r1, r2, fillvalue=0)
         assert next(it) == (1, 2)
         assert next(it) == (1, 2)
         assert next(it)== (1, 2)
@@ -987,9 +938,7 @@
         assert type(A.from_iterable([])) is A
         class A(itertools.imap): pass
         assert type(A(lambda: 5, [])) is A
-        class A(itertools.izip): pass
-        assert type(A([], [])) is A
-        class A(itertools.izip_longest): pass
+        class A(itertools.zip_longest): pass
         assert type(A([], [])) is A
         class A(itertools.cycle): pass
         assert type(A([])) is A


More information about the pypy-commit mailing list