| 1 |
import py |
| 2 |
from rpython.rtyper.controllerentry import Controller, ControllerEntry |
| 3 |
from rpython.rtyper.controllerentry import ControllerEntryForPrebuilt |
| 4 |
|
| 5 |
from rpython.annotator.annrpython import RPythonAnnotator |
| 6 |
from rpython.rtyper.test.test_llinterp import interpret |
| 7 |
|
| 8 |
|
| 9 |
class C(object): |
| 10 |
"Imagine some magic here to have a foo attribute on instances" |
| 11 |
|
| 12 |
def fun(a): |
| 13 |
lst = [] |
| 14 |
c = C(a) |
| 15 |
c.foo = lst |
| 16 |
return c.foo, lst[0] |
| 17 |
|
| 18 |
class C_Controller(Controller): |
| 19 |
knowntype = C |
| 20 |
|
| 21 |
def new(self, a): |
| 22 |
return a + '_' |
| 23 |
|
| 24 |
def convert(self, c): |
| 25 |
return str(c._bar) |
| 26 |
|
| 27 |
def get_foo(self, obj): |
| 28 |
return obj + "2" |
| 29 |
|
| 30 |
def set_foo(self, obj, value): |
| 31 |
value.append(obj) |
| 32 |
|
| 33 |
def getitem(self, obj, key): |
| 34 |
return obj + key |
| 35 |
|
| 36 |
def setitem(self, obj, key, value): |
| 37 |
value.append(obj + key) |
| 38 |
|
| 39 |
def call(self, obj, arg): |
| 40 |
return obj + arg |
| 41 |
|
| 42 |
class Entry(ControllerEntry): |
| 43 |
_about_ = C |
| 44 |
_controller_ = C_Controller |
| 45 |
|
| 46 |
class Entry(ControllerEntryForPrebuilt): |
| 47 |
_type_ = C |
| 48 |
_controller_ = C_Controller |
| 49 |
|
| 50 |
|
| 51 |
def test_C_annotate(): |
| 52 |
a = RPythonAnnotator() |
| 53 |
s = a.build_types(fun, [a.bookkeeper.immutablevalue("4")]) |
| 54 |
assert s.const == ("4_2", "4_") |
| 55 |
|
| 56 |
def test_C_specialize(): |
| 57 |
res = interpret(fun, ["4"]) |
| 58 |
assert ''.join(res.item0.chars) == "4_2" |
| 59 |
assert ''.join(res.item1.chars) == "4_" |
| 60 |
|
| 61 |
|
| 62 |
c2 = C() |
| 63 |
c2._bar = 51 |
| 64 |
|
| 65 |
c3 = C() |
| 66 |
c3._bar = 7654 |
| 67 |
|
| 68 |
def fun1(): |
| 69 |
return c2.foo |
| 70 |
|
| 71 |
def test_C1_annotate(): |
| 72 |
a = RPythonAnnotator() |
| 73 |
s = a.build_types(fun1, []) |
| 74 |
assert s.const == "512" |
| 75 |
|
| 76 |
def test_C1_specialize(): |
| 77 |
res = interpret(fun1, []) |
| 78 |
assert ''.join(res.chars) == "512" |
| 79 |
|
| 80 |
def fun2(flag): |
| 81 |
if flag: |
| 82 |
c = c2 |
| 83 |
else: |
| 84 |
c = c3 |
| 85 |
return c.foo |
| 86 |
|
| 87 |
def test_C2_annotate(): |
| 88 |
a = RPythonAnnotator() |
| 89 |
s = a.build_types(fun2, [a.bookkeeper.immutablevalue(True)]) |
| 90 |
assert s.const == "512" |
| 91 |
|
| 92 |
def test_C2_specialize(): |
| 93 |
res = interpret(fun2, [True]) |
| 94 |
assert ''.join(res.chars) == "512" |
| 95 |
res = interpret(fun2, [False]) |
| 96 |
assert ''.join(res.chars) == "76542" |
| 97 |
|
| 98 |
def fun3(a): |
| 99 |
lst = [] |
| 100 |
c = C(a) |
| 101 |
c['foo'] = lst |
| 102 |
call_res = c("baz") |
| 103 |
return c['bar'], lst[0], call_res |
| 104 |
|
| 105 |
def test_getsetitem_annotate(): |
| 106 |
a = RPythonAnnotator() |
| 107 |
s = a.build_types(fun3, [a.bookkeeper.immutablevalue("4")]) |
| 108 |
assert s.const == ("4_bar", "4_foo", "4_baz") |
| 109 |
|
| 110 |
def test_getsetitem_specialize(): |
| 111 |
res = interpret(fun3, ["4"]) |
| 112 |
assert ''.join(res.item0.chars) == "4_bar" |
| 113 |
assert ''.join(res.item1.chars) == "4_foo" |
| 114 |
assert ''.join(res.item2.chars) == "4_baz" |