[pypy-dev] Support for __getitem__ in rpython?

Hakan Ardo hakan at debian.org
Mon Dec 29 23:50:37 CET 2008


On Mon, Dec 29, 2008 at 8:55 PM, Paolo Giarrusso <p.giarrusso at gmail.com> wrote:
>>
>> Atatched is a small patch for the annotator that makes it treat None
>> and NotImplemented alike. This is all that is needed for most cases as
>> all NotImplemented are typically removed by the optimisations
>> performed by the annotator.
> That can be made to work, but if such a method returns None you get
> completely different semantics (trying again with something else) from
> CPython (which will maybe return a failure, or return None for the
> result of such an operation), so you have to restrict the allowed

No, the patch do distinguish between None and NotImplemented. What I
mean is that NotImplemented is treated in a similar manner as to how
None is treated. The following crazy construction do compile and
generate the same result as in cpython ('OK', 'String', 'None',
'None'):

class mystr:
    def __init__(self,s):
        self.s=s
    def __str__(self):
        return self.s
    def __add__(self,other):
        if isinstance(other,mystr):
            return NotImplemented
        s=self.s+other
        if s=='None':
            return None
        else:
            return s
    __add__._annspecialcase_ = 'specialize:argtype(1)'
    def __radd__(self,other):
        return str(other)+self.s
    __radd__._annspecialcase_ = 'specialize:argtype(1)'

def dotst_nonestr():
    s1=mystr('No')+'ne'
    if s1 is None: s1='OK'
    s2=mystr('Str')+'ing'
    s3=mystr('No')+mystr('ne')
    s4='No'+mystr('ne')
    return (s1,s2,s3,s4)

-- 
Håkan Ardö



More information about the Pypy-dev mailing list