PyListObject & C Modules
Martin v. Loewis
martin at v.loewis.de
Wed Sep 4 03:01:01 EDT 2002
Brian Quinlan <brian at sweetapp.com> writes:
> if(PyList_Size(list) == 0){
> PyErr_SetString(PyExc_ValueError, "empty lists not allowed");
> return NULL;
> }
>
> first = PyList_GetItem(list, 0);
> + if (first == NULL) return NULL;
That can't happen, really - GetItem can fail only if it is not a list,
or if the index is out of range. However, it is a good strategy to
test all results.
> + Py_ADDREF(first)
> - PyList_SetItem(result, 0, first);
> + if (PyList_SetItem(result, 0, first) != 0) {
> + Py_DECREF(first)
> + Py_DECREF(result);
> + result = NULL;
> + }
I see: yes, this is needed. I wasn't really aware that SetItem steals
a reference. This is counter-intuitive - it might be better to use
PySequence_SetItem instead.
Regards,
Martin
More information about the Python-list
mailing list