[pypy-dev] Support for __getitem__ in rpython?

Hakan Ardo hakan at debian.org
Tue Dec 9 20:14:35 CET 2008

thanx for all help. For anyone else interested I've placed a few files
on http://hakan.ardoe.net/pypy/ namely:

getitem_support.py    - The suggested implementation
getsetitem_support.py - Generalisation to handle __setitem__ aswell
special_methods.py    - Generalisation to handle several __xxx__ methods
test_getitem.py       - Tests for __getitem__
test_matrix.py        - Tests using __getitem__, __setitem__ and __add__

>    do_getitem._annspecialcase_ = 'specialize:argtype(0)'
>    # ^^^ specialization; not sure I have done it right...
> I think the _annspecialcase_ should be able to sort out between multiple
> unrelated calls to the helper.

It seems to be doing it's job. But if I try to apply the same trick to
the __getitem__ method it does not seem to work, e.g. if I try to
compile the code below it only works if I either do a[i] or a[i,j]
calls not a mix of the two.

class arr2d:
    def __init__(self,w,h):
        self.data=[i for i in range(w*h)]
    def __getitem__(self,i):
        if isinstance(i,int):
            return self.data[i]
        elif len(i)==2:
            return self.data[i[1]*self.width + i[0]]
            raise TypeError
    __getitem__._annspecialcase_ = 'specialize:argtype(0)'

Håkan Ardö

More information about the Pypy-dev mailing list