| 1 | import py |
| 2 | from rpython.rlib.jit import virtual_ref, virtual_ref_finish |
| 3 | from rpython.rlib.jit import vref_None, non_virtual_ref, InvalidVirtualRef |
| 4 | from rpython.rlib._jit_vref import SomeVRef |
| 5 | from rpython.annotator import model as annmodel |
| 6 | from rpython.annotator.annrpython import RPythonAnnotator |
| 7 | from rpython.rtyper.rclass import OBJECTPTR |
| 8 | from rpython.rtyper.lltypesystem import lltype |
| 9 | |
| 10 | from rpython.rtyper.test.tool import BaseRtypingTest |
| 11 | |
| 12 | |
| 13 | class X(object): |
| 14 | pass |
| 15 | |
| 16 | class Y(X): |
| 17 | pass |
| 18 | |
| 19 | class Z(X): |
| 20 | pass |
| 21 | |
| 22 | |
| 23 | def test_direct_forced(): |
| 24 | x1 = X() |
| 25 | vref = virtual_ref(x1) |
| 26 | assert vref._state == 'non-forced' |
| 27 | assert vref.virtual is True |
| 28 | assert vref() is x1 |
| 29 | assert vref._state == 'forced' |
| 30 | assert vref.virtual is False |
| 31 | virtual_ref_finish(vref, x1) |
| 32 | assert vref._state == 'forced' |
| 33 | assert vref.virtual is False |
| 34 | assert vref() is x1 |
| 35 | |
| 36 | def test_direct_invalid(): |
| 37 | x1 = X() |
| 38 | vref = virtual_ref(x1) |
| 39 | assert vref._state == 'non-forced' |
| 40 | virtual_ref_finish(vref, x1) |
| 41 | assert vref._state == 'invalid' |
| 42 | py.test.raises(InvalidVirtualRef, "vref()") |
| 43 | |
| 44 | def test_annotate_1(): |
| 45 | def f(): |
| 46 | return virtual_ref(X()) |
| 47 | a = RPythonAnnotator() |
| 48 | s = a.build_types(f, []) |
| 49 | assert isinstance(s, SomeVRef) |
| 50 | assert isinstance(s.s_instance, annmodel.SomeInstance) |
| 51 | assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X) |
| 52 | |
| 53 | def test_annotate_2(): |
| 54 | def f(): |
| 55 | x1 = X() |
| 56 | vref = virtual_ref(x1) |
| 57 | x2 = vref() |
| 58 | virtual_ref_finish(vref, x1) |
| 59 | return x2 |
| 60 | a = RPythonAnnotator() |
| 61 | s = a.build_types(f, []) |
| 62 | assert isinstance(s, annmodel.SomeInstance) |
| 63 | assert s.classdef == a.bookkeeper.getuniqueclassdef(X) |
| 64 | |
| 65 | def test_annotate_3(): |
| 66 | def f(n): |
| 67 | if n > 0: |
| 68 | return virtual_ref(Y()) |
| 69 | else: |
| 70 | return non_virtual_ref(Z()) |
| 71 | a = RPythonAnnotator() |
| 72 | s = a.build_types(f, [int]) |
| 73 | assert isinstance(s, SomeVRef) |
| 74 | assert isinstance(s.s_instance, annmodel.SomeInstance) |
| 75 | assert not s.s_instance.can_be_None |
| 76 | assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X) |
| 77 | |
| 78 | def test_annotate_4(): |
| 79 | def f(n): |
| 80 | if n > 0: |
| 81 | return virtual_ref(X()) |
| 82 | else: |
| 83 | return vref_None |
| 84 | a = RPythonAnnotator() |
| 85 | s = a.build_types(f, [int]) |
| 86 | assert isinstance(s, SomeVRef) |
| 87 | assert isinstance(s.s_instance, annmodel.SomeInstance) |
| 88 | assert s.s_instance.can_be_None |
| 89 | assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X) |
| 90 | |
| 91 | class TestVRef(BaseRtypingTest): |
| 92 | OBJECTTYPE = OBJECTPTR |
| 93 | def castable(self, TO, var): |
| 94 | return lltype.castable(TO, lltype.typeOf(var)) > 0 |
| 95 | |
| 96 | def test_rtype_1(self): |
| 97 | def f(): |
| 98 | return virtual_ref(X()) |
| 99 | x = self.interpret(f, []) |
| 100 | assert lltype.typeOf(x) == self.OBJECTTYPE |
| 101 | |
| 102 | def test_rtype_2(self): |
| 103 | def f(): |
| 104 | x1 = X() |
| 105 | vref = virtual_ref(x1) |
| 106 | x2 = vref() |
| 107 | virtual_ref_finish(vref, x2) |
| 108 | return x2 |
| 109 | x = self.interpret(f, []) |
| 110 | assert self.castable(self.OBJECTTYPE, x) |
| 111 | |
| 112 | def test_rtype_3(self): |
| 113 | def f(n): |
| 114 | if n > 0: |
| 115 | return virtual_ref(Y()) |
| 116 | else: |
| 117 | return non_virtual_ref(Z()) |
| 118 | x = self.interpret(f, [-5]) |
| 119 | assert lltype.typeOf(x) == self.OBJECTTYPE |
| 120 | |
| 121 | def test_rtype_4(self): |
| 122 | def f(n): |
| 123 | if n > 0: |
| 124 | return virtual_ref(X()) |
| 125 | else: |
| 126 | return vref_None |
| 127 | x = self.interpret(f, [-5]) |
| 128 | assert lltype.typeOf(x) == self.OBJECTTYPE |
| 129 | assert not x |
| 130 | |
| 131 | def test_rtype_5(self): |
| 132 | def f(): |
| 133 | vref = virtual_ref(X()) |
| 134 | try: |
| 135 | vref() |
| 136 | return 42 |
| 137 | except InvalidVirtualRef: |
| 138 | return -1 |
| 139 | x = self.interpret(f, []) |
| 140 | assert x == 42 |
| 141 | |
| 142 | def test_rtype_virtualattr(self): |
| 143 | def f(): |
| 144 | vref = virtual_ref(X()) |
| 145 | return vref.virtual |
| 146 | x = self.interpret(f, []) |
| 147 | assert x is False |