| 1 | import py, sys |
| 2 | |
| 3 | from rpython.rtyper import extregistry |
| 4 | from rpython.rtyper.extregistry import ExtRegistryEntry |
| 5 | from rpython.annotator import model as annmodel |
| 6 | from rpython.annotator.annrpython import RPythonAnnotator |
| 7 | from rpython.translator.translator import TranslationContext |
| 8 | from rpython.rtyper.lltypesystem import lltype |
| 9 | from rpython.rtyper.test.test_llinterp import interpret |
| 10 | from rpython.rtyper.rmodel import Repr |
| 11 | |
| 12 | def dummy(): |
| 13 | raiseNameError |
| 14 | |
| 15 | class Entry(ExtRegistryEntry): |
| 16 | _about_ = dummy |
| 17 | s_result_annotation = annmodel.SomeInteger() |
| 18 | |
| 19 | def test_call_dummy(): |
| 20 | def func(): |
| 21 | x = dummy() |
| 22 | return x |
| 23 | |
| 24 | a = RPythonAnnotator() |
| 25 | s = a.build_types(func, []) |
| 26 | assert isinstance(s, annmodel.SomeInteger) |
| 27 | |
| 28 | def test_callable_annotation(): |
| 29 | def dummy2(): |
| 30 | raiseNameError |
| 31 | |
| 32 | class Entry(ExtRegistryEntry): |
| 33 | _about_ = dummy2 |
| 34 | |
| 35 | def compute_result_annotation(self): |
| 36 | return annmodel.SomeInteger() |
| 37 | |
| 38 | def func(): |
| 39 | x = dummy2() |
| 40 | return x |
| 41 | |
| 42 | a = RPythonAnnotator() |
| 43 | s = a.build_types(func, []) |
| 44 | assert isinstance(s, annmodel.SomeInteger) |
| 45 | |
| 46 | def test_register_type_with_callable(): |
| 47 | class DummyType(object): |
| 48 | pass |
| 49 | |
| 50 | dummy_type = DummyType() |
| 51 | |
| 52 | def func(): |
| 53 | return dummy_type |
| 54 | |
| 55 | class Entry(ExtRegistryEntry): |
| 56 | _type_ = DummyType |
| 57 | def compute_annotation(self): |
| 58 | assert self.instance is dummy_type |
| 59 | return annmodel.SomeInteger() |
| 60 | |
| 61 | a = RPythonAnnotator() |
| 62 | s = a.build_types(func, []) |
| 63 | assert isinstance(s, annmodel.SomeInteger) |
| 64 | |
| 65 | def test_register_value_with_specialization(): |
| 66 | def dummy_func(): |
| 67 | raiseNameError |
| 68 | |
| 69 | class Entry(ExtRegistryEntry): |
| 70 | _about_ = dummy_func |
| 71 | s_result_annotation = annmodel.SomeInteger() |
| 72 | def specialize_call(self, hop): |
| 73 | hop.exception_cannot_occur() |
| 74 | return hop.inputconst(lltype.Signed, 42) |
| 75 | |
| 76 | def func(): |
| 77 | return dummy_func() |
| 78 | |
| 79 | res = interpret(func, []) |
| 80 | |
| 81 | assert res == 42 |
| 82 | |
| 83 | def test_register_type_with_get_repr(): |
| 84 | class DummyClass(object): |
| 85 | pass |
| 86 | |
| 87 | class SomeDummyObject(annmodel.SomeObject): |
| 88 | def rtyper_makerepr(self, rtyper): |
| 89 | entry = extregistry.lookup_type(self.knowntype) |
| 90 | return entry.get_repr(rtyper, self) |
| 91 | |
| 92 | def rtyper_makekey( self ): |
| 93 | return self.__class__, self.knowntype |
| 94 | |
| 95 | class DummyRepr(Repr): |
| 96 | lowleveltype = lltype.Signed |
| 97 | |
| 98 | def convert_const(self, value): |
| 99 | return 42 |
| 100 | |
| 101 | class Entry(ExtRegistryEntry): |
| 102 | _type_ = DummyClass |
| 103 | |
| 104 | def compute_annotation(self): |
| 105 | assert self.type is DummyClass |
| 106 | dummy_object = SomeDummyObject() |
| 107 | dummy_object.knowntype = DummyClass |
| 108 | return dummy_object |
| 109 | |
| 110 | def get_repr(self, rtyper, s_instance): |
| 111 | return DummyRepr() |
| 112 | |
| 113 | dummy_class = DummyClass() |
| 114 | |
| 115 | def func(): |
| 116 | return dummy_class |
| 117 | |
| 118 | res = interpret(func, []) |
| 119 | |
| 120 | assert res == 42 |
| 121 | |
| 122 | def test_register_unhashable(): |
| 123 | lst1 = [5, 6] |
| 124 | lst2 = [5, 6] |
| 125 | class Entry(ExtRegistryEntry): |
| 126 | _about_ = lst1 |
| 127 | assert isinstance(extregistry.lookup(lst1), Entry) |
| 128 | py.test.raises(KeyError, "extregistry.lookup(lst2)") |
| 129 | |
| 130 | def test_register_non_weakly_refable(): |
| 131 | n1 = sys.maxint // 2 |
| 132 | n2 = sys.maxint // 2 |
| 133 | class Entry(ExtRegistryEntry): |
| 134 | _about_ = n1 |
| 135 | assert isinstance(extregistry.lookup(n1), Entry) |
| 136 | assert isinstance(extregistry.lookup(n2), Entry) |