Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5a4208

Browse files
committed
Modified itertools.izip() to match the behavior of __builtin__.zip()
which can now take zero arguments.
1 parent 77fe69b commit b5a4208

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

Doc/lib/libitertools.tex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,13 @@ \subsection{Itertool functions \label{itertools-functions}}
226226
\begin{verbatim}
227227
def izip(*iterables):
228228
iterables = map(iter, iterables)
229-
while True:
229+
while iterables:
230230
result = [i.next() for i in iterables]
231231
yield tuple(result)
232232
\end{verbatim}
233+
234+
\versionchanged[When no iterables are specified, returns a zero length
235+
iterator instead of raising a TypeError exception]{2.4}
233236
\end{funcdesc}
234237

235238
\begin{funcdesc}{repeat}{object\optional{, times}}

Lib/test/test_itertools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_izip(self):
8787
self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3)))
8888
self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3)))
8989
self.assertEqual(list(izip('abcdef')), zip('abcdef'))
90-
self.assertRaises(TypeError, izip)
90+
self.assertEqual(list(izip()), zip())
9191
self.assertRaises(TypeError, izip, 3)
9292
self.assertRaises(TypeError, izip, range(3), 3)
9393
# Check tuple re-use (implementation detail)
@@ -199,6 +199,8 @@ def test_dropwhile(self):
199199
self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next)
200200

201201
def test_StopIteration(self):
202+
self.assertRaises(StopIteration, izip().next)
203+
202204
for f in (chain, cycle, izip):
203205
self.assertRaises(StopIteration, f([]).next)
204206
self.assertRaises(StopIteration, f(StopNow()).next)
@@ -540,6 +542,9 @@ def test_dropwhile(self):
540542
>>> no(lambda x: x%2==0, [1, 2, 5, 9])
541543
False
542544
545+
>>> quantify(lambda x: x%2==0, xrange(99))
546+
50
547+
543548
>>> list(window('abc'))
544549
[('a', 'b'), ('b', 'c')]
545550

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Extension modules
2323
Library
2424
-------
2525

26+
- itertools.izip() with no arguments now returns an empty iterator instead
27+
of raising a TypeError exception.
28+
2629
- _strptime.py now has a behind-the-scenes caching mechanism for the most
2730
recent TimeRE instance used along with the last five unique directive
2831
patterns. The overall module was also made more thread-safe.

Modules/itertoolsmodule.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,12 +1510,6 @@ izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15101510
PyObject *result;
15111511
int tuplesize = PySequence_Length(args);
15121512

1513-
if (tuplesize < 1) {
1514-
PyErr_SetString(PyExc_TypeError,
1515-
"izip() requires at least one sequence");
1516-
return NULL;
1517-
}
1518-
15191513
/* args must be a tuple */
15201514
assert(PyTuple_Check(args));
15211515

@@ -1598,6 +1592,8 @@ izip_next(izipobject *lz)
15981592
PyObject *it;
15991593
PyObject *item;
16001594

1595+
if (tuplesize == 0)
1596+
return NULL;
16011597
if (result->ob_refcnt == 1) {
16021598
for (i=0 ; i < tuplesize ; i++) {
16031599
it = PyTuple_GET_ITEM(lz->ittuple, i);

0 commit comments

Comments
 (0)