pypy /pypy/rlib/test/test_rerased.py

Language Python Lines 326
MD5 Hash 02dbcaa61a9a2f9410ac33ca54462182 Estimated Cost $6,647 (why?)
Repository https://bitbucket.org/pypy/pypy/ View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import py
import sys
from pypy.rlib.rerased import *
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, llmemory

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

def make_annotator():
    a = RPythonAnnotator()
    a.translator.config.translation.taggedpointers = True
    return a



class X(object):
    pass

class Y(X):
    pass

class Z(X):
    pass

eraseX, uneraseX = new_erasing_pair("X")
erase_list_X, unerase_list_X = new_erasing_pair("list of X")


def test_simple():
    x1 = X()
    e = eraseX(x1)
    #assert is_integer(e) is False
    assert uneraseX(e) is x1

def test_simple_none():
    e = eraseX(None)
    assert uneraseX(e) is None

def test_simple_int():
    e = erase_int(15)
    #assert is_integer(e) is True
    assert unerase_int(e) == 15

def test_simple_int_overflow():
    erase_int(sys.maxint//2)
    py.test.raises(OverflowError, erase_int, sys.maxint//2 + 1)
    py.test.raises(OverflowError, erase_int, sys.maxint)
    py.test.raises(OverflowError, erase_int, sys.maxint-1)
    py.test.raises(OverflowError, erase_int, -sys.maxint)
    py.test.raises(OverflowError, erase_int, -sys.maxint-1)

def test_list():
    l = [X()]
    e = erase_list_X(l)
    #assert is_integer(e) is False
    assert unerase_list_X(e) is l

def test_annotate_1():
    def f():
        return eraseX(X())
    a = make_annotator()
    s = a.build_types(f, [])
    assert isinstance(s, SomeErased)

def test_annotate_2():
    def f():
        x1 = X()
        e = eraseX(x1)
        #assert not is_integer(e)
        x2 = uneraseX(e)
        return x2
    a = make_annotator()
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInstance)
    assert s.classdef == a.bookkeeper.getuniqueclassdef(X)

def test_annotate_3():
    def f():
        e = erase_int(16)
        #assert is_integer(e)
        x2 = unerase_int(e)
        return x2
    a = make_annotator()
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInteger)

def test_annotate_erasing_pair():
    erase, unerase = new_erasing_pair("test1")
    erase2, unerase2 = new_erasing_pair("test2")
    class Foo:
        pass
    #
    def make(n):
        if n > 5:
            return erase([5, 6, n-6])
        else:
            foo = Foo()
            foo.bar = n+1
            return erase2(foo)

    def check(x, n):
        if n > 5:
            return unerase(x)[2]
        else:
            return unerase2(x).bar

    def f(n):
        x = make(n)
        return check(x, n)
    #
    a = make_annotator()
    s = a.build_types(f, [int])
    assert isinstance(s, annmodel.SomeInteger)

def test_annotate_reflowing():
    erase, unerase = new_erasing_pair("test1")
    class A: pass
    class B(A): pass
    class C(B): pass
    class D(C): pass

    def f():
        x = erase(None)
        while True:
            inst = unerase(x)
            if inst is None:
                inst = D()
                x = erase(inst)
            elif isinstance(inst, D):
                inst = C()
                x = erase(inst)
            elif isinstance(inst, C):
                inst = B()
                x = erase(inst)
            elif isinstance(inst, B):
                inst = A()
                x = erase(inst)
            else:
                return inst
    #
    a = make_annotator()
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInstance)
    assert s.classdef == a.bookkeeper.getuniqueclassdef(A)

def test_annotate_prebuilt():
    erase, unerase = new_erasing_pair("test1")
    class X(object):
        pass
    x1 = X()
    e1 = erase(x1)
    e2 = erase(None)

    def f(i):
        if i:
            e = e1
        else:
            e = e2
        return unerase(e)
    #
    a = make_annotator()
    s = a.build_types(f, [int])
    assert isinstance(s, annmodel.SomeInstance)
    assert s.classdef == a.bookkeeper.getuniqueclassdef(X)
    assert s.can_be_none()

def test_annotate_prebuilt_int():
    e1 = erase_int(42)
    def f(i):
        return unerase_int(e1)
    a = make_annotator()
    s = a.build_types(f, [int])
    assert isinstance(s, annmodel.SomeInteger)

class BaseTestRErased(BaseRtypingTest):
    def interpret(self, *args, **kwargs):
        kwargs["taggedpointers"] = True
        return BaseRtypingTest.interpret(self, *args, **kwargs)
    def test_rtype_1(self):
        def f():
            return eraseX(X())
        x = self.interpret(f, [])
        assert lltype.typeOf(x) == self.ERASED_TYPE

    def test_rtype_2(self):
        def f():
            x1 = X()
            e = eraseX(x1)
            #assert not is_integer(e)
            x2 = uneraseX(e)
            return x2
        x = self.interpret(f, [])
        assert self.castable(self.UNERASED_TYPE, x)

    def test_rtype_3(self):
        def f():
            e = erase_int(16)
            #assert is_integer(e)
            x2 = unerase_int(e)
            return x2
        x = self.interpret(f, [])
        assert x == 16

    def test_prebuilt_erased(self):
        e1 = erase_int(16)
        x1 = X()
        x1.foobar = 42
        e2 = eraseX(x1)

        def f():
            #assert is_integer(e1)
            #assert not is_integer(e2)
            x1.foobar += 1
            x2 = unerase_int(e1) + uneraseX(e2).foobar
            return x2
        x = self.interpret(f, [])
        assert x == 16 + 42 + 1

    def test_prebuilt_erased_in_instance(self):
        erase_empty, unerase_empty = new_erasing_pair("empty")
        class FakeList(object):
            pass

        x1 = X()
        x1.foobar = 42
        l1 = FakeList()
        l1.storage = eraseX(x1)
        l2 = FakeList()
        l2.storage = erase_empty(None)

        def f():
            #assert is_integer(e1)
            #assert not is_integer(e2)
            x1.foobar += 1
            x2 = uneraseX(l1.storage).foobar + (unerase_empty(l2.storage) is None)
            return x2
        x = self.interpret(f, [])
        assert x == 43 + True

    def test_overflow(self):
        def f(i):
            try:
                e = erase_int(i)
            except OverflowError:
                return -1
            #assert is_integer(e)
            return unerase_int(e)
        x = self.interpret(f, [16])
        assert x == 16
        x = self.interpret(f, [sys.maxint])
        assert x == -1

    def test_none(self):
        def foo():
            return uneraseX(eraseX(None))
        assert foo() is None
        res = self.interpret(foo, [])
        assert not res
        #
        def foo():
            eraseX(X())
            return uneraseX(eraseX(None))
        assert foo() is None
        res = self.interpret(foo, [])
        assert not res

    def test_rtype_list(self):
        prebuilt_l = [X()]
        prebuilt_e = erase_list_X(prebuilt_l)
        def l(flag):
            if flag == 1:
                l = [X()]
                e = erase_list_X(l)
            elif flag == 2:
                l = prebuilt_l
                e = erase_list_X(l)
            else:
                l = prebuilt_l
                e = prebuilt_e
            #assert is_integer(e) is False
            assert unerase_list_X(e) is l
        self.interpret(l, [0])
        self.interpret(l, [1])
        self.interpret(l, [2])

class TestLLtype(BaseTestRErased, LLRtypeMixin):
    ERASED_TYPE = llmemory.GCREF
    UNERASED_TYPE = OBJECTPTR
    def castable(self, TO, var):
        return lltype.castable(TO, lltype.typeOf(var)) > 0

from pypy.rpython.ootypesystem.ootype import Object

class TestOOtype(BaseTestRErased, OORtypeMixin):
    ERASED_TYPE = Object
    UNERASED_TYPE = OBJECT
    def castable(self, TO, var):
        return ootype.isSubclass(lltype.typeOf(var), TO)
    @py.test.mark.xfail
    def test_prebuilt_erased(self):
        super(TestOOtype, self).test_prebuilt_erased()

def test_union():
    s_e1 = SomeErased()
    s_e1.const = 1
    s_e2 = SomeErased()
    s_e2.const = 3
    assert not annmodel.pair(s_e1, s_e2).union().is_constant()

# ____________________________________________________________

def test_erasing_pair():
    erase, unerase = new_erasing_pair("test1")
    class X:
        pass
    x = X()
    erased = erase(x)
    assert unerase(erased) is x
    #
    erase2, unerase2 = new_erasing_pair("test2")
    py.test.raises(AssertionError, unerase2, erased)
Back to Top