PySequence_SetItem

Bill Pursell bill.pursell at gmail.com
Wed Aug 16 15:00:47 EDT 2006


The following code is pretty much straight out of
section 1.2.1.1 of the Python/C reference manual:

#include <Python.h>

int
main(void)
{
        PyObject *l, *x;

        Py_Initialize();

        l = PyList_New(3);
        x = PyInt_FromLong(1L);

        if (l == NULL || x == NULL) {
                PyErr_Print();
                exit (EXIT_FAILURE);
        }
        PySequence_SetItem(l, 0, x);
        Py_DECREF(x);

        Py_Finalize();
        return EXIT_SUCCESS;
}

Unforunately,  it doesn't work.  It segfaults, and
the error occurs in list_ass_item() when the
Py_DECREF macro is applied to old_value, which
was set  at line 699 of Objects/listobject.c
with old_value = a->ob_item[i];  (old_value is
being set to NULL, and Py_DECREF is
being applied to NULL...boom!)

I'm totally new to embedding/extending python,
so I'm not sure if I'm doing something incredibly
stupid, but it certainly looks like PySequence_SetItem()
was expecting that there should already be an
item at the desired index.

Am I doing something stupid?

--
Bill Pursell




More information about the Python-list mailing list