Problem slicing a list with the C API

Jen Kris jenkris at tutanota.com
Sat Mar 12 16:54:50 EST 2022


pDictData, despite the name, is a list of 2-tuples where each 2-tuple is a dictionary object and a string.  

Mar 12, 2022, 13:41 by rosuav at gmail.com:

> 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
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list