using lists in C-extensions?

Martin v. Loewis martin at v.loewis.de
Sat Feb 16 16:44:23 EST 2002


stojek at part-gmbh.de (Marcus Stojek) writes:

> There is this strange "O" respectively "O&" which might
> be suitable, but I don't know how.

The appropriate format is inded O (or perhaps O!, passing PyList_Type),
passing the address of a PyObject* variable. Then, the PyObject*
will point to a PyListObject.

For this pointer, you can then use functions like PyList_Size,
PyList_GetItem, etc.

> So could anyone give an example or tell me where to look?

Just ask yourself what C functions of the standard library expect
lists. file.writelines comes to mind (although in 2.2, it uses the
METH_O optimization, which may complicate the understanding).

> EXAMPLE:
> 
> list=[]
> for i in range(100000):
>     list.append(i)
> 
> listnew=map((lambda x:x+1),list)  # this should be coded in C

This should look like

PyObject *
mymap(PyObject *self, PyObject *args)
{
   PyObject *l, *result, *item;
   int size, i;
   if (!PyArg_ParseTupe(args, "O!", &PyList_Type, &l))
     return NULL;
   size = PyList_Size(l);
   result = PyList_New(size);
   if (!result) return NULL;
   for (int i=0; i < size; i++) {
     item = PyList_GET_ITEM(l, i);
     if (!PyInt_Check(item)) {
        PyErr_SetString(PyExc_TypeError, "list contains non-integers");
        Py_DECREF(result);
        return NULL;
     }
     item = PyInt_FromLong(PyInt_AsLong(item) + 1);
     PyList_SET_ITEM(result, i, item);
   }
   return result;
}

Please refer to

http://www.python.org/doc/current/api/listObjects.html

for the exact meaning of the list API, in particular with regard to
reference counting.

If you want to support not only list, you need to use the abstract
sequence operations, notice that those have difference reference
counting semantics.

Regards,
Martin



More information about the Python-list mailing list