writing a C function to manipulate python lists

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Mar 1 06:09:17 EST 2001


Michael Vanier <mvanier at endor.bbb.caltech.edu> writes:

> If anyone has some code lying around that does something like this, could
> they pass it on to me?  I haven't seen this kind of thing in the official
> python docs.  I'd also prefer to use the C representation of python lists
> directly for speed, because using the C API would probably slow things down
> considerably.

I recommend to look at the "Embedding and Extending" tutorial, and the
Python/C API reference. The functions you will need in your code are

- PyArg_ParseTuple to extract the lists from the argument tuple
  (although PyTuple_GetItem will also do)
- PyList_New to create a result list (if necessary)
- PyList_GetItem and PyList_SetItem to manipulate lists
- potentially any other function defined in listobject.h, and
  perhaps other API, depending on you problem (most likely, Py_INCREF
  and Py_DECREF in the right places).

Hope this helps,
Martin




More information about the Python-list mailing list