]> granicus.if.org Git - python/commitdiff
As discussed on python-dev, changed builtin.zip() to handle zero arguments
authorRaymond Hettinger <python@rcn.com>
Sat, 2 Aug 2003 07:42:57 +0000 (07:42 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 2 Aug 2003 07:42:57 +0000 (07:42 +0000)
by returning an empty list instead of raising a TypeError.

Doc/lib/libfuncs.tex
Lib/test/test_builtin.py
Lib/test/test_iter.py
Misc/NEWS
Python/bltinmodule.c

index b9dc4e1b57b8da6b167f537d75c09f80d67c4cf3..2f1ffff1c06eead47ef472948a6a7060019698dc 100644 (file)
@@ -1063,14 +1063,18 @@ It's a function
   when the loop is usually terminated with \keyword{break}).
 \end{funcdesc}
 
-\begin{funcdesc}{zip}{seq1, \moreargs}
+\begin{funcdesc}{zip}{\optional{seq1, \moreargs}}
   This function returns a list of tuples, where the \var{i}-th tuple contains
-  the \var{i}-th element from each of the argument sequences.  At
-  least one sequence is required, otherwise a \exception{TypeError} is
-  raised.  The returned list is truncated in length to the length of
+  the \var{i}-th element from each of the argument sequences.
+  The returned list is truncated in length to the length of
   the shortest argument sequence.  When there are multiple argument
   sequences which are all of the same length, \function{zip()} is
   similar to \function{map()} with an initial argument of \code{None}.
   With a single sequence argument, it returns a list of 1-tuples.
+  With no arguments, it returns an empty list.
   \versionadded{2.0}
+
+  \versionchanged[Formerly, \function{zip()} required at least one argument
+  and \code{zip()} raised a \exception{TypeError} instead of returning
+  \code{[]}]{2.4} 
 \end{funcdesc}
index 3dd2ffc7f312e5478f4e1b39f4e1bc77f8a2ac72..65dabb159aadde67323934d759ea00d03b587490 100644 (file)
@@ -1126,7 +1126,8 @@ class BuiltinTest(unittest.TestCase):
                 if i < 0 or i > 2: raise IndexError
                 return i + 4
         self.assertEqual(zip(a, I()), t)
-        self.assertRaises(TypeError, zip)
+        self.assertEqual(zip(), [])
+        self.assertEqual(zip(*[]), [])
         self.assertRaises(TypeError, zip, None)
         class G:
             pass
index 383fce344a4db8446e6a5a3a885299eb89e254c2..f83de78ad67955ebc24029b9681aaf19b65e73fd 100644 (file)
@@ -423,7 +423,10 @@ class TestCase(unittest.TestCase):
 
     # Test zip()'s use of iterators.
     def test_builtin_zip(self):
-        self.assertRaises(TypeError, zip)
+        self.assertEqual(zip(), [])
+        self.assertEqual(zip(*[]), [])
+        self.assertEqual(zip(*[(1, 2), 'ab']), [(1, 'a'), (2, 'b')])
+
         self.assertRaises(TypeError, zip, None)
         self.assertRaises(TypeError, zip, range(10), 42)
         self.assertRaises(TypeError, zip, range(10), zip)
index caf49b045713dbf9cc52569ad44d44a971208f78..435dd643c9f1b95b8e75ef426950eb15ff0213b2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1?
 Core and builtins
 -----------------
 
+- zip() with no arguments now returns an empty list instead of raising
+  a TypeError exception.
+
 Extension modules
 -----------------
 
index 49fcc09cfc04a1cfab40a31fabcbaf04b3616f02..fb92478ac25120f8f2d0c40651bebc195d17cd0b 100644 (file)
@@ -1916,11 +1916,9 @@ builtin_zip(PyObject *self, PyObject *args)
        PyObject *itlist;  /* tuple of iterators */
        int len;           /* guess at result length */
 
-       if (itemsize < 1) {
-               PyErr_SetString(PyExc_TypeError,
-                               "zip() requires at least one sequence");
-               return NULL;
-       }
+       if (itemsize == 0)
+               return PyList_New(0);
+
        /* args must be a tuple */
        assert(PyTuple_Check(args));