I have an 2d matrix class in c that I am using in python. I want to be able to call the matrix class by passing a tuple of slice objects, e.g., A[1:2,1:2]. So far what I have is the following. The relevant C code for the __getitem__
method looks like
int __getitem__(PyObject *slices){ // get tuple of slice objects
PyObject* slice1; // declare first slice object
PyObject* slice2; // declare second slice object
slice1 = PyTuple_GetItem(slices, 0); // get first slice from tuple
slice2 = PyTuple_GetItem(slices, 1); // get second slice from tuple
// get start and stop of first slice (currently the following three lines do not work)
Py_ssize_t start, stop, step, length;
PySlice_GetIndicesEx(slice1,length,&start,&stop,&step);
return PySlice_Check(slice2) ;
}
This code checks whether slice2 is indeed a slice object using the PySlice_Check
method. If I comment out PySlice_GetIndicesEx(slice1,length,&start,&stop,&step)
, I get a 1
as expected. However, if I leave this line in, I get an error:
cannot convert 'PyObject* {aka _object*}' to 'PySliceObject*' for argument '1' to 'int PySlice_GetIndicesEx(...
which means I cannot pass slice1 or slice2 into PySlice_GetIndicesEx
since it needs a PySliceObject
object.
How do I unravel the slice objects properly so I can pass it into PySlice_GetIndicesEx.
This function takes in PySliceObject
and not a PyObject
, which is what slice1 and slice2 is, even though PySlice_Check(slice1)
and PySlice_Check(slice2)
both return a 1.