[Python-Dev] Fixing _PyEval_SliceIndex so that integer-like objects can be used

Brett C. bac at OCF.Berkeley.EDU
Fri Feb 18 22:31:47 CET 2005

Travis Oliphant wrote:
> Hello again,
> There is a great discussion going on the numpy list regarding a proposed 
> PEP for multidimensional arrays that is in the works.
> During this discussion as resurfaced regarding slicing with objects that 
> are not IntegerType objects but that
> have a tp_as_number->nb_int method to convert to an int.
> Would it be possible to change
> _PyEval_SliceIndex  in ceval.c
> so that rather than throwing an error if the indexing object is not an 
> integer, the code first checks to see if the object has a
> tp_as_number->nb_int method and calls it instead.

You would also have to change apply_slice() since that also has a guard for 
checking the slice arguments are either NULL, int, or long objects.

But I am +1 with it since the guard is already there for ints and longs to 
handle those properly and thus the common case does not slow down in any way. 
As long as it also accepts Python objects that define __int__ and not just C 
types that have the nb_int slot defined I am okay with this idea.


More information about the Python-Dev mailing list