Issue 1328278: getslice taking priority over getitem (original) (raw)

When creating a class that uses getitem to implement slicing, if getattr is also implemented, slicing will fail. This is due to the (deprecated) getslice method being called before getitem.

The attached file demonstrates this. If getitem is implemented on its own, all is rosy. When we add getattr and do not raise an AttributeError when getslice is searched for, the slicing fails. If we raise this AttributeError, getitem is called next.

The only other reference I could find to this bug is on the jython mailing list, from 2003: http://sourceforge.net/mailarchive/forum.php? thread_id=2350972&forum_id=5586

My question is; why is getslice called before getitem? I assumed that because it is deprecated, it would be the last resort for a slicing.

Is this planned to be fixed, or is there existing behaviour that is reliant on it?

I would suggest that the list class should use the same form suggested on the documentation site, namely:

if sys.version_info < (2, 0):
    # They won't be defined if version is at least 2.0 final

    def __getslice__(self, i, j):
        return self[max(0, i):max(0, j):]
    def __setslice__(self, i, j, seq):
        self[max(0, i):max(0, j):] = seq
    def __delslice__(self, i, j):
        del self[max(0, i):max(0, j):]
...

in order to assure that the *slice methods are not defined unless needed for backward compatability in an older interpreter; then classes developed with the above suggested structure should work properly.