File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -344,7 +344,17 @@ class G:
344344if not exc :
345345 raise TestFailed , 'zip(a, b) - missing expected TypeError'
346346
347+ # Make sure zip doesn't try to allocate a billion elements for the
348+ # result list when one of its arguments doesn't say how long it is.
349+ # A MemoryError is the most likely failure mode.
350+ class SequenceWithoutALength :
351+ def __getitem__ (self , i ):
352+ if i == 5 :
353+ raise IndexError
354+ else :
355+ return i
356+ vereq (zip (SequenceWithoutALength (), xrange (2 ** 30 )),
357+ list (enumerate (range (5 ))))
347358
348359# Epilogue -- unlink the temp file
349-
350360unlink (TESTFN )
Original file line number Diff line number Diff line change @@ -1717,13 +1717,18 @@ builtin_zip(PyObject *self, PyObject *args)
17171717 /* args must be a tuple */
17181718 assert (PyTuple_Check (args ));
17191719
1720- /* Guess at result length: the shortest of the input lengths. */
1720+ /* Guess at result length: the shortest of the input lengths.
1721+ If some argument refuses to say, we refuse to guess too, lest
1722+ an argument like xrange(sys.maxint) lead us astray.*/
17211723 len = -1 ; /* unknown */
17221724 for (i = 0 ; i < itemsize ; ++ i ) {
17231725 PyObject * item = PyTuple_GET_ITEM (args , i );
17241726 int thislen = PySequence_Length (item );
1725- if (thislen < 0 )
1727+ if (thislen < 0 ) {
17261728 PyErr_Clear ();
1729+ len = -1 ;
1730+ break ;
1731+ }
17271732 else if (len < 0 || thislen < len )
17281733 len = thislen ;
17291734 }
You can’t perform that action at this time.
0 commit comments