Source

pypy / pypy / rlib / test / test__jit_vref.py

Full commit
Jean-Philippe St… c775f9d 

Antonio Cuni da62de1 
Jean-Philippe St… c775f9d 




Daniel Roberts d3cf721 
Jean-Philippe St… c775f9d 

Daniel Roberts f2f146b 
Jean-Philippe St… c775f9d 
Daniel Roberts 00e4c92 

Jean-Philippe St… c775f9d 
Maciej Fijalkows… 86d4d71 
Jean-Philippe St… c775f9d 







Antonio Cuni da62de1 
Jean-Philippe St… c775f9d 

Antonio Cuni da62de1 
Armin Rigo d48eaa0 
Jean-Philippe St… c775f9d 
Antonio Cuni da62de1 
Armin Rigo d48eaa0 
Antonio Cuni da62de1 

Armin Rigo d48eaa0 
Jean-Philippe St… c775f9d 

Antonio Cuni da62de1 
Jean-Philippe St… c775f9d 

Antonio Cuni da62de1 



Jean-Philippe St… c775f9d 














Antonio Cuni 8d7f15c 
Jean-Philippe St… c775f9d 































Daniel Roberts f2f146b 




Daniel Roberts d3cf721 
Jean-Philippe St… c775f9d 
Daniel Roberts f2f146b 




Antonio Cuni 8d7f15c 
Daniel Roberts f2f146b 

Daniel Roberts d3cf721 
Jean-Philippe St… c775f9d 
Daniel Roberts f2f146b 






Daniel Roberts d3cf721 
Jean-Philippe St… c775f9d 
Daniel Roberts f2f146b 






Daniel Roberts d3cf721 
Daniel Roberts f2f146b 

Antonio Cuni 6853b1a 










Armin Rigo d48eaa0 






Maciej Fijalkows… cb06fc4 




Maciej Fijalkows… 86d4d71 
Maciej Fijalkows… cb06fc4 

Maciej Fijalkows… 86d4d71 
Maciej Fijalkows… cb06fc4 
Antonio Cuni 6853b1a 
Daniel Roberts f2f146b 
Daniel Roberts d3cf721 


Daniel Roberts f2f146b 

Daniel Roberts d3cf721 

Daniel Roberts 00e4c92 
Maciej Fijalkows… 86d4d71 
Maciej Fijalkows… cb06fc4 
Maciej Fijalkows… 86d4d71 
import py
from pypy.rlib.jit import virtual_ref, virtual_ref_finish
from pypy.rlib.jit import vref_None, non_virtual_ref, InvalidVirtualRef
from pypy.rlib._jit_vref import SomeVRef
from pypy.annotation import model as annmodel
from pypy.annotation.annrpython import RPythonAnnotator
from pypy.rpython.test.test_llinterp import interpret
from pypy.rpython.lltypesystem.rclass import OBJECTPTR
from pypy.rpython.ootypesystem.rclass import OBJECT
from pypy.rpython.lltypesystem import lltype

from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin

from pypy.rpython.ootypesystem import ootype

class X(object):
    x = 3

class Y(X):
    pass

class Z(X):
    pass


def test_direct_forced():
    x1 = X()
    vref = virtual_ref(x1)
    assert vref._state == 'non-forced'
    assert vref.virtual is True
    assert vref() is x1
    assert vref._state == 'forced'
    assert vref.virtual is False
    virtual_ref_finish(vref, x1)
    assert vref._state == 'forced'
    assert vref.virtual is False
    assert vref() is x1

def test_direct_invalid():
    x1 = X()
    vref = virtual_ref(x1)
    assert vref._state == 'non-forced'
    virtual_ref_finish(vref, x1)
    assert vref._state == 'invalid'
    py.test.raises(InvalidVirtualRef, "vref()")

def test_annotate_1():
    def f():
        return virtual_ref(X())
    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, SomeVRef)
    assert isinstance(s.s_instance, annmodel.SomeInstance)
    assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)

def test_annotate_2():
    def f():
        x1 = X()
        vref = virtual_ref(x1)
        x2 = vref()
        virtual_ref_finish(vref, x1)
        return x2
    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInstance)
    assert s.classdef == a.bookkeeper.getuniqueclassdef(X)

def test_annotate_3():
    def f(n):
        if n > 0:
            return virtual_ref(Y())
        else:
            return non_virtual_ref(Z())
    a = RPythonAnnotator()
    s = a.build_types(f, [int])
    assert isinstance(s, SomeVRef)
    assert isinstance(s.s_instance, annmodel.SomeInstance)
    assert not s.s_instance.can_be_None
    assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)

def test_annotate_4():
    def f(n):
        if n > 0:
            return virtual_ref(X())
        else:
            return vref_None
    a = RPythonAnnotator()
    s = a.build_types(f, [int])
    assert isinstance(s, SomeVRef)
    assert isinstance(s.s_instance, annmodel.SomeInstance)
    assert s.s_instance.can_be_None
    assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)

class BaseTestVRef(BaseRtypingTest):
    def test_rtype_1(self):
        def f():
            return virtual_ref(X())
        x = self.interpret(f, [])
        assert lltype.typeOf(x) == self.OBJECTTYPE

    def test_rtype_2(self):
        def f():
            x1 = X()
            vref = virtual_ref(x1)
            x2 = vref()
            virtual_ref_finish(vref, x2)
            return x2
        x = self.interpret(f, [])
        assert self.castable(self.OBJECTTYPE, x)

    def test_rtype_3(self):
        def f(n):
            if n > 0:
                return virtual_ref(Y())
            else:
                return non_virtual_ref(Z())
        x = self.interpret(f, [-5])
        assert lltype.typeOf(x) == self.OBJECTTYPE

    def test_rtype_4(self):
        def f(n):
            if n > 0:
                return virtual_ref(X())
            else:
                return vref_None
        x = self.interpret(f, [-5])
        assert lltype.typeOf(x) == self.OBJECTTYPE
        assert not x

    def test_rtype_5(self):
        def f():
            vref = virtual_ref(X())
            try:
                vref()
                return 42
            except InvalidVirtualRef:
                return -1
        x = self.interpret(f, [])
        assert x == 42

    def test_rtype_virtualattr(self):
        def f():
            vref = virtual_ref(X())
            return vref.virtual
        x = self.interpret(f, [])
        assert x is False

    def test_rtype_getfield(self):
        class X(object):
            def __init__(self, x):
                self.x = x
        
        def f():
            vref = virtual_ref(X(1))
            return vref.getfield('x')
        x = self.interpret(f, [])
        assert x == 1

class TestLLtype(BaseTestVRef, LLRtypeMixin):
    OBJECTTYPE = OBJECTPTR
    def castable(self, TO, var):
        return lltype.castable(TO, lltype.typeOf(var)) > 0

class TestOOtype(BaseTestVRef, OORtypeMixin):
    OBJECTTYPE = OBJECT 
    def castable(self, TO, var):
        return ootype.isSubclass(lltype.typeOf(var), TO)

    def test_rtype_getfield(self):
        py.test.skip("not supported")