[Python-checkins] TRUNK IS UNFROZEN, available for 2.6 work if you are so inclined

Georg Brandl g.brandl at gmx.net
Thu Aug 17 21:50:40 CEST 2006


Georg Brandl wrote:
> Jack Diederich wrote:
> 
>> Looks good to me.  While you are on that page do you want to change
>> 
>> l = PyList_New(3);
>> x = PyInt_FromLong(1L);
>> PySequence_SetItem(l, 0, x); Py_DECREF(x);
>> x = PyInt_FromLong(2L);
>> PySequence_SetItem(l, 1, x); Py_DECREF(x);
>> x = PyString_FromString("three");
>> PySequence_SetItem(l, 2, x); Py_DECREF(x);
>> 
>> to
>> 
>> l = PyList_New(3);
>> x = PyInt_FromLong(1L);
>> PyList_SetItem(l, 0, x);
>> x = PyInt_FromLong(2L);
>> PyList_SetItem(l, 1, x);
>> x = PyString_FromString("three");
>> PyList_SetItem(l, 2, x);
>> 
>> The example code causes segfaults and probably always has (at last to 2.2)
> 
> Interesting! From a naive POV, the docs' example is quite right.
> 
> The segfault occurs because list_ass_item Py_DECREFs the old item (which
> segfaults because the old items are NULL in a newly created list).
> PyList_SetItem does a Py_XDECREF.
> 
> The docs to PyList_New, however, do not explicitly say that the new list
> must only be filled using PyList_SetItem.
> 
> So please, someone, decide what's broken here!

Okay, now that I stumbled over and read the c.l.py thread, I think that we should

* remove the faulty example (the correct one is already in there)
* add a note to PyList_New that the new list must be filled with items
   before handing it to Python code or using it with abstract APIs such as
   PySequence_SetItem.

Georg



More information about the Python-checkins mailing list