[Python-Dev] PySlice_GetIndicesEx annd stable ABI: bikeshedding

Serhiy Storchaka storchaka at gmail.com
Wed Dec 21 09:51:28 EST 2016

Three months ago we discussed about an issue with PySlice_GetIndicesEx().

The problem was that PySlice_GetIndicesEx() takes the size of the 
sequence, but the size of the sequence can be changed when call custom 
__index__() methods inside PySlice_GetIndicesEx().

The solution is to split PySlice_GetIndicesEx() into two functions: one 
function convert Python objects to C integers by calling __index__() 
methods, other function takes the size of the sequence and adjusts 
indices, it is atomic from Python view.

The code

     if (PySlice_GetIndicesEx(item, length,
             &start, &stop, &step, &slicelength) < 0)
         return -1;

should be replaced with

     if (foo(item, &start, &stop, &step) < 0)
         return -1;
     slicelength = bar(&start, &stop, step, length);

PySlice_GetIndicesEx() can be converted to a macro calling these two 
functions. It would be enough just recompile an extension to make it 
invulnerable to the original bug.

But there is a problem. New functions should be added to stable ABI. 
This means that we should design names and signatures of these functions 
even if don't make them a part of public API.

Let's start bikeshedding. What are your ideas about names and the order 
of arguments of two following functions?

1. Takes a slice object, returns it's start, stop and step as Py_ssize_t 
values. Can fail.

2. Takes slice's start, stop, step, and the length of the sequence (all 
are Py_ssize_t), returns adjusted start, stop, and the length of sliced 
subsequence (as Py_ssize_t). Always success.

More information about the Python-Dev mailing list