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

Ronald Oussoren ronaldoussoren at mac.com
Sat Sep 25 13:56:52 CEST 2004


On 25-sep-04, at 6:41, Tim Peters wrote:

> [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.

I get a bus error on OSX (although with a slightly out of date 
python2.4 from CVS).

Why should this loop at all? x is the empty list, and the generator 
comprehension should therefore end up with an empty sequence. It's not 
like your initial example where the list was non-empty to at the start.

It crashes because of an Py_INCREF(item) at line 2727 in listobject.c 
where item is NULL:

2722            assert(PyList_Check(seq));
2723
2724            if (it->it_index < PyList_GET_SIZE(seq)) {
2725                    item = PyList_GET_ITEM(seq, it->it_index);
2726                    ++it->it_index;
2727                    Py_INCREF(item);
2728                    return item;
2729            }
2730
2731            Py_DECREF(seq);

BWT. seq is null as well.



More information about the Python-Dev mailing list