]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
* Add news item for the previous bugfix
authorRaymond Hettinger <python@rcn.com>
Sat, 30 Aug 2003 22:16:59 +0000 (22:16 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 30 Aug 2003 22:16:59 +0000 (22:16 +0000)
* Backport itertoolsmodule.c 1.19 to re-sync Py2.3.1 with Py2.4.

Doc/lib/libitertools.tex
Lib/test/test_itertools.py
Misc/NEWS
Modules/itertoolsmodule.c

index 30fa60b685a0d39d3c992e751e8f08c3078885f5..aa9a02db4993ce13305d0c17bcad85cd7839ec8e 100644 (file)
@@ -226,10 +226,13 @@ by functions or loops that truncate the stream.
   \begin{verbatim}
      def izip(*iterables):
          iterables = map(iter, iterables)
-         while True:
+         while iterables:
              result = [i.next() for i in iterables]
              yield tuple(result)
   \end{verbatim}
+
+  \versionchanged[When no iterables are specified, returns a zero length
+                  iterator instead of raising a TypeError exception]{2.3.1}
 \end{funcdesc}
 
 \begin{funcdesc}{repeat}{object\optional{, times}}
index 7059650206357c4e3658664975e8cfacd56d61c4..057b576e49dec174caf5cd20c50e3dcdf6723193 100644 (file)
@@ -87,7 +87,7 @@ class TestBasicOps(unittest.TestCase):
         self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3)))
         self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3)))
         self.assertEqual(list(izip('abcdef')), zip('abcdef'))
-        self.assertRaises(TypeError, izip)
+        self.assertEqual(list(izip()), zip())
         self.assertRaises(TypeError, izip, 3)
         self.assertRaises(TypeError, izip, range(3), 3)
         # Check tuple re-use (implementation detail)
@@ -199,6 +199,8 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next)
 
     def test_StopIteration(self):
+        self.assertRaises(StopIteration, izip().next)
+
         for f in (chain, cycle, izip):
             self.assertRaises(StopIteration, f([]).next)
             self.assertRaises(StopIteration, f(StopNow()).next)
@@ -501,15 +503,19 @@ Samuele
 
 >>> def all(pred, seq):
 ...     "Returns True if pred(x) is True for every element in the iterable"
-...     return not nth(ifilterfalse(pred, seq), 0)
+...     return False not in imap(pred, seq)
 
 >>> def some(pred, seq):
 ...     "Returns True if pred(x) is True for at least one element in the iterable"
-...     return bool(nth(ifilter(pred, seq), 0))
+...     return True in imap(pred, seq)
 
 >>> def no(pred, seq):
 ...     "Returns True if pred(x) is False for every element in the iterable"
-...     return not nth(ifilter(pred, seq), 0)
+...     return True not in imap(pred, seq)
+
+>>> def quantify(pred, seq):
+...     "Count how many times the predicate is True in the sequence"
+...     return sum(imap(pred, seq))
 
 >>> def padnone(seq):
 ...     "Returns the sequence elements and then returns None indefinitely"
@@ -566,6 +572,9 @@ True
 >>> no(lambda x: x%2==0, [1, 2, 5, 9])
 False
 
+>>> quantify(lambda x: x%2==0, xrange(99))
+50
+
 >>> list(window('abc'))
 [('a', 'b'), ('b', 'c')]
 
index b682c27af227f5fbaeba73b4732ca4d0ed025cfb..fff30e6a150e5ff4af874bcac3b64f59812ce647 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,7 +21,9 @@ Extension modules
 -----------------
 
 - Bug #793826:  re-ordered the reference counting code in
-  itertools.izip() to prevent re-entrancy anomalies.
+  itertools.izip() to prevent re-entrancy anomalies.  Also,
+  if given zero arguments, it now returns an empty iterator
+  rather than raising a type error.
 
 - Bug #770485: cStringIO did not support the f.closed attribute.
 
index de7939d6fb9f9491fb68f3ef55d2d739b2b29e50..68e176f23d4e71c0078d6e6f76887dadcc87d80f 100644 (file)
@@ -1510,12 +1510,6 @@ izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        PyObject *result;
        int tuplesize = PySequence_Length(args);
 
-       if (tuplesize < 1) {
-               PyErr_SetString(PyExc_TypeError,
-                               "izip() requires at least one sequence");
-               return NULL;
-       }
-
        /* args must be a tuple */
        assert(PyTuple_Check(args));
 
@@ -1599,6 +1593,8 @@ izip_next(izipobject *lz)
        PyObject *item;
        PyObject *olditem;
 
+       if (tuplesize == 0)
+               return NULL;
        if (result->ob_refcnt == 1) {
                Py_INCREF(result);
                for (i=0 ; i < tuplesize ; i++) {