PySequence_SetItem

Jack Diederich jack at psynchronous.com
Wed Aug 16 18:02:22 EDT 2006


On Wed, Aug 16, 2006 at 02:39:24PM -0700, John Machin wrote:
> 
> Jack Diederich wrote:
> 
> > Changing the PySequence_SetItem to PyList_SetItem and dropping the
> > DECREF works for me too (PyList functions steal a reference).  I also
> > tried setting the list to length 1, still no dice.  The PySequence
> > version segs under 2.4 and 2.5.  It segs even when the Int is changed
> > to a String.
> >
> > Yikes, I'll poke around some more.
> 
> Yikes indeed.
> 
> Not the OP's problem, but a bug in the manual: example in the chapter
> that the OP was reading acts as though the 2nd arg to PyObject_SetItem
> is a C int (as it is for the List and Sequence varieties) -- it is in
> fact a (PyObject *), which explains the SystemError that I got.

The good news is that this is a documentation problem.
When working at the C level you use the concrete API when you can
(PyList_*) and at the abstract level (PySequence_*) only when someone
passes you something and you don't know what it is, you only know it
supports the Sequence API.

The exmample should use the PyList API to construct the list.  The
Sequence API requires valid objects to work.  Here the object is only half
constructed and list_ass_item tries to DECREF the zeroth member which
is NULL because the list isn't constructed yet.  PyList_SetItem does
an XDECREF (decrement only if the pointer isn't null) because it expects
to be used that way.

-Jack






More information about the Python-list mailing list