[Python-Dev] Bad interaction of __index__ and sequence repeat
Nick Coghlan
ncoghlan at gmail.com
Fri Jul 28 18:33:01 CEST 2006
Tim Peters wrote:
> OTOH, in the long discussion about PEP 357, I'm not sure anyone except
> Travis was clear on whether nb_index was meant to apply only to
> sequence /slicing/ or was meant to apply "everywhere an object gets
> used in an index-like context". Clipping makes sense only for the
> former, but it looks like the implementation treats it more like the
> latter. This was probably exacerbated by:
>
> http://mail.python.org/pipermail/python-dev/2006-February/060663.html
>
> [Travis]
> There are other places in Python that check specifically for int objects
> and long integer objects and fail with anything else. Perhaps all of
> these should aslo call the __index__ slot.
>
> [Guido]
> Right, absolutely.
>
> This is a mess :-)
I've been trawling through the code a bit, and I don't think it's as bad as
all that.
All I believe is really needed is to:
- remove the PyErr_Occurred() check and its body from long_index in longobject.c
- add a PyErr_Occurred() check to force a -1 return from PyNumber_Index in
abstract.c
- add a PyErr_Occurred() and PyErr_ExceptionMatches(PyOverflowError) check
to invoke PyErr_Clear() in _PyEval_SliceIndex in ceval.c.
Add test cases to test_index.py to check that:
(2**100).__index__() == 2**100
(-2**100).__index__() == -2**100
slice(-2**100, 2**100).indices(sys.maxint) == (0, sys.maxint, 1)
"a" * 2**100 raises OverflowError
Add test cases to test_operator.py to check that:
operator.index(2**100) == 2**100
operator.index(-2**100) == -2**100
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list