tuple creation in C extensions

Louis M. Pecora pecora at anvil.nrl.navy.mil
Thu Jun 15 07:20:02 EDT 2000


In article <20000614184508.B26436 at xs4all.nl>, Thomas Wouters
<thomas at xs4all.net> wrote:

> The docs already have that:
> 
>    PyObject* PyList_GetItem (PyObject *list, int index)
> 
>    Return value: Borrowed reference.
> 
>           Returns the object at position pos in the list pointed to by p.
>           If pos is out of bounds, returns NULL and sets an IndexError
>           exception.
> 
>    PyObject* PyList_New (int len)
> 
>    Return value: New reference.
> 
>           Returns a new list of length len on success, or NULL on
>           failure.
> 
>    PyObject* PyList_GetSlice (PyObject *list, int low, int high)
> 
>    Return value: New reference.
> 
>           Returns a list of the objects in list containing the objects
>           between low and high. Returns NULL and sets an exception if
>           unsuccessful. Analogous to list[low:high].
> 
> And:
> 
>    int PyList_SetItem (PyObject *list, int index, PyObject *item)
>           Sets the item at index index in list to item. Note: This
>           function `steals'' a reference to item.

Thomas,  Nice list.  But dumb/newbie question, when do I INCREF and
DECREF the above?  I would guess I INCREF the borrowed ref that's
returned and DECREF any of the above (new or borrowed) at the end of
the C routine, unless those pointers are returned to Python caller to
be used there.  Right?  Wrong?  ...help.

You know, just adding INC and DEC rules to the above in the docs would
go a long way.



More information about the Python-list mailing list