[Python-Dev] A cute new way to get an infinite loop

Tim Peters tim.peters at gmail.com
Sat Sep 25 06:41:37 CEST 2004


[George Yoshida]
>> It does not always go into an infinite loop. I was bitten by this:
>>
>>  >>> x = []
>>  >>> x.extend(-y for y in x)
>>  Segmentation fault

[Bob Ippolito]
> No algorithm that requires infinite memory will run for an infinite
> amount of time on a finite computer.  Of course it should raise an
> exception instead of segfaulting though.. could it be blowing the
> stack?

No, its stack use is bounded (and small) no matter how long it runs. 
On Windows it eventually raises MemoryError.  My guess is that George
is using Linux.  "It's a feature" that the Linux malloc() can lie (==
malloc(n) can return a non-NULL value p even if you're going to get a
segfault if you try to write to p+i for some i in range(n)).  Linus
likens this to airlines over-selling seats, based on the likelihood
that someone will miss their flight.  Argue with him <wink>.  When
malloc() claims to return memory that can't actually be used, there's
not much Python can do about that (other than blow up when trying to
use it).


More information about the Python-Dev mailing list