mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
As discussed on python-dev, changed builtin.zip() to handle zero arguments
by returning an empty list instead of raising a TypeError.
This commit is contained in:
parent
9463792c68
commit
eaef615116
5 changed files with 20 additions and 11 deletions
|
@ -1063,14 +1063,18 @@ It's a function
|
||||||
when the loop is usually terminated with \keyword{break}).
|
when the loop is usually terminated with \keyword{break}).
|
||||||
\end{funcdesc}
|
\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
|
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
|
the \var{i}-th element from each of the argument sequences.
|
||||||
least one sequence is required, otherwise a \exception{TypeError} is
|
The returned list is truncated in length to the length of
|
||||||
raised. The returned list is truncated in length to the length of
|
|
||||||
the shortest argument sequence. When there are multiple argument
|
the shortest argument sequence. When there are multiple argument
|
||||||
sequences which are all of the same length, \function{zip()} is
|
sequences which are all of the same length, \function{zip()} is
|
||||||
similar to \function{map()} with an initial argument of \code{None}.
|
similar to \function{map()} with an initial argument of \code{None}.
|
||||||
With a single sequence argument, it returns a list of 1-tuples.
|
With a single sequence argument, it returns a list of 1-tuples.
|
||||||
|
With no arguments, it returns an empty list.
|
||||||
\versionadded{2.0}
|
\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}
|
\end{funcdesc}
|
||||||
|
|
|
@ -1126,7 +1126,8 @@ class BuiltinTest(unittest.TestCase):
|
||||||
if i < 0 or i > 2: raise IndexError
|
if i < 0 or i > 2: raise IndexError
|
||||||
return i + 4
|
return i + 4
|
||||||
self.assertEqual(zip(a, I()), t)
|
self.assertEqual(zip(a, I()), t)
|
||||||
self.assertRaises(TypeError, zip)
|
self.assertEqual(zip(), [])
|
||||||
|
self.assertEqual(zip(*[]), [])
|
||||||
self.assertRaises(TypeError, zip, None)
|
self.assertRaises(TypeError, zip, None)
|
||||||
class G:
|
class G:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -423,7 +423,10 @@ class TestCase(unittest.TestCase):
|
||||||
|
|
||||||
# Test zip()'s use of iterators.
|
# Test zip()'s use of iterators.
|
||||||
def test_builtin_zip(self):
|
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, None)
|
||||||
self.assertRaises(TypeError, zip, range(10), 42)
|
self.assertRaises(TypeError, zip, range(10), 42)
|
||||||
self.assertRaises(TypeError, zip, range(10), zip)
|
self.assertRaises(TypeError, zip, range(10), zip)
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1?
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- zip() with no arguments now returns an empty list instead of raising
|
||||||
|
a TypeError exception.
|
||||||
|
|
||||||
Extension modules
|
Extension modules
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
|
|
@ -1916,11 +1916,9 @@ builtin_zip(PyObject *self, PyObject *args)
|
||||||
PyObject *itlist; /* tuple of iterators */
|
PyObject *itlist; /* tuple of iterators */
|
||||||
int len; /* guess at result length */
|
int len; /* guess at result length */
|
||||||
|
|
||||||
if (itemsize < 1) {
|
if (itemsize == 0)
|
||||||
PyErr_SetString(PyExc_TypeError,
|
return PyList_New(0);
|
||||||
"zip() requires at least one sequence");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
/* args must be a tuple */
|
/* args must be a tuple */
|
||||||
assert(PyTuple_Check(args));
|
assert(PyTuple_Check(args));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue