[Python-Dev] Negative times behaviour in itertools.repeat for Python maintenance releases (2.7, 3.3 and maybe 3.4)

Nick Coghlan ncoghlan at gmail.com
Mon Jan 27 13:43:39 CET 2014


On 27 January 2014 22:29, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Mon, 27 Jan 2014 20:22:53 +0800
> Vajrasky Kok <sky.kok at speaklikeaking.com> wrote:
>>
>> >>> from itertools import repeat
>> >>> list(repeat('a', 2**31))
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> MemoryError
>
> Sure, just adjust the number to fit the available memory (here, 2**29
> does the trick).

And for anyone interested in why a sufficiently large positive value
that won't fit in available RAM fails gracefully with MemoryError:

>>> repeat('a', 2**31).__length_hint__()
2147483648
>>> repeat('a', -1).__length_hint__()
0

list() uses __length_hint__() for preallocation, so a sufficiently
large length hint means the preallocation attempt fails with
MemoryError. As Antoine showed though, you still can't feed it
untrusted data, because a large enough value that just fits into RAM
can still cause you a lot of grief.

Everything points to "times=-1" behaving as it does being a bug, but
not a sufficiently critical one to risk breaking working code in a
maintenance release. That makes deprecating the current behaviour of
"times=-1" and accepting "times=None" in 3.5 the least controversial
course of action.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list