Problem slicing a list with the C API
Chris Angelico
rosuav at gmail.com
Sat Mar 12 16:41:12 EST 2022
On Sun, 13 Mar 2022 at 08:25, Jen Kris via Python-list
<python-list at python.org> wrote:
> PyObject* slice = PySlice_New(PyLong_FromLong(0), half_slice, 0);
> PyObject* subdata_a = PyObject_GetItem(pDictddata, slice);
>
> On the final line (subdata_a) I get a segfault. I know that the second parameter of PyObject_GetItem is a “key” and I suspect that’s where the problem comes from, but I don’t understand what a key is in this context.
>
The key is simply whatever would be in the square brackets in Python
code, so that part looks fine.
But dictionaries aren't usually subscripted with slices, so I'm a bit
confused as to what's going on here. What exactly is
dictdata/pDictdata?
Have you confirmed that pDictdata (a) isn't NULL, (b) is the object
you intend it to be, and (c) contains the objects you expect it to?
The segfault might not be from the slice object itself, it might be
from actually iterating over the thing being sliced and touching all
its elements. For instance, if dictdata is actually a list, that call
will be constructing a new list with references to the same elements,
so if one of them is broken (maybe NULL), it'll break badly.
ChrisA
More information about the Python-list
mailing list