@@ -2150,6 +2150,61 @@ static char issubclass_doc[] =
21502150Return whether class C is a subclass (i.e., a derived class) of class B." ;
21512151
21522152
2153+ static PyObject *
2154+ builtin_zip (PyObject * self , PyObject * args )
2155+ {
2156+ PyObject * ret ;
2157+ int itemsize = PySequence_Length (args );
2158+ int i , j ;
2159+
2160+ if (itemsize < 1 ) {
2161+ PyErr_SetString (PyExc_TypeError ,
2162+ "at least one sequence is required" );
2163+ return NULL ;
2164+ }
2165+ /* args must be a tuple */
2166+ assert (PyTuple_Check (args ));
2167+
2168+ if ((ret = PyList_New (0 )) == NULL )
2169+ return NULL ;
2170+
2171+ for (i = 0 ;; i ++ ) {
2172+ PyObject * next = PyTuple_New (itemsize );
2173+ if (!next ) {
2174+ Py_DECREF (ret );
2175+ return NULL ;
2176+ }
2177+ for (j = 0 ; j < itemsize ; j ++ ) {
2178+ PyObject * seq = PyTuple_GET_ITEM (args , j );
2179+ PyObject * item = PySequence_GetItem (seq , i );
2180+
2181+ if (!item ) {
2182+ if (PyErr_ExceptionMatches (PyExc_IndexError )) {
2183+ PyErr_Clear ();
2184+ Py_DECREF (next );
2185+ return ret ;
2186+ }
2187+ Py_DECREF (next );
2188+ Py_DECREF (ret );
2189+ return NULL ;
2190+ }
2191+ PyTuple_SET_ITEM (next , j , item );
2192+ }
2193+ PyList_Append (ret , next );
2194+ Py_DECREF (next );
2195+ }
2196+ /* no return */
2197+ }
2198+
2199+
2200+ static char zip_doc [] =
2201+ "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
2202+ \n\
2203+ Return a list of tuples, where each tuple contains the i-th element\n\
2204+ from each of the argument sequences. The returned list is truncated\n\
2205+ in length to the length of the shortest argument sequence." ;
2206+
2207+
21532208static PyMethodDef builtin_methods [] = {
21542209 {"__import__" , builtin___import__ , 1 , import_doc },
21552210 {"abs" , builtin_abs , 1 , abs_doc },
@@ -2207,6 +2262,7 @@ static PyMethodDef builtin_methods[] = {
22072262 {"unichr" , builtin_unichr , 1 , unichr_doc },
22082263 {"vars" , builtin_vars , 1 , vars_doc },
22092264 {"xrange" , builtin_xrange , 1 , xrange_doc },
2265+ {"zip" , builtin_zip , 1 , zip_doc },
22102266 {NULL , NULL },
22112267};
22122268
0 commit comments