On 27 January 2014 22:29, Antoine Pitrou <solip...@pitrou.net> wrote:
> On Mon, 27 Jan 2014 20:22:53 +0800
> Vajrasky Kok <sky....@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   |   ncogh...@gmail.com   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to