[pypy-dev] Support for __getitem__ in rpython?

Hakan Ardo hakan at debian.org
Fri Dec 12 16:49:17 CET 2008


On Sun, Dec 7, 2008 at 6:04 PM, Armin Rigo <arigo at tunes.org> wrote:
>
>    class __extend__(pairtype(SomeInstance, SomeObject)):
>        def getitem((s_array, s_index)):
>            # first generate a pseudo call to the helper
>            bk = getbookkeeper()
>            s_callable = bk.immutablevalue(do_getitem)
>            args_s = [s_array, s_index]
>            bk.emulate_pbc_call(('instance_getitem', s_array.knowntype),
>                                s_callable, args_s)
>            # then use your own trick to get the correct result
>            s=SomeString()
>            s.const="__getitem__"
>            p=s_array.getattr(s)
>            return p.simple_call(s_index)
>
> unrelated calls to the helper.  The code for the annotator is a bit
> bogus, btw, because it emulates a call to the function but also computes
> the result explicitly; but I couldn't figure out a better way.

How about instead doing:

    class __extend__(pairtype(SomeInstance, SomeObject)):
        def getitem((s_array, s_index)):
            return call_helper('do_getitem', (s_array, s_index))

    def call_helper(name,s_args):
        bk = getbookkeeper()
        s_callable = bk.immutablevalue(eval(name))
        s_ret=bk.emulate_pbc_call(('instance_'+name,)+tuple([s.knowntype
for s in s_args]),
                                  s_callable, s_args)
        for graph in bk.annotator.pendingblocks.values():
            if graph.name[0:len(name)]==name:
                bk.annotator.notify[graph.returnblock][bk.position_key]=1

        return s_ret;

Is there some way to get hold of the mangled function name of the
created graph? The above code might add too many notifies if there are
several graphs in pendingblocks with a name starting with the name of
the helper. Or is there some better way to get hold of the created
graph object if any was created?

> >     __getitem__._annspecialcase_ = 'specialize:argtype(0)'
>
> That's the wrong annotation.  For this case, it should be
> 'specialize:argtype(1)' in order to get two versions of __getitem__,

Right. Sorry about that.

-- 
Håkan Ardö



More information about the Pypy-dev mailing list