2
from pypy.rpython.ootypesystem.ootype import *
3
from pypy.annotation import model as annmodel
4
from pypy.objspace.flow import FlowObjSpace
5
from pypy.annotation.annrpython import RPythonAnnotator
7
from pypy.rpython.ootypesystem import ooregistry # side effects
10
def test_simple_new():
11
C = Instance("test", ROOT, {'a': Signed})
18
a = RPythonAnnotator()
19
s = a.build_types(oof, [])
22
assert s.knowntype == int
24
def test_simple_instanceof():
25
C = Instance("test", ROOT, {'a': Signed})
29
return instanceof(c, C)
31
a = RPythonAnnotator()
32
s = a.build_types(oof, [])
35
assert s.knowntype == bool
37
def test_simple_null():
38
I = Instance("test", ROOT, {'a': Signed})
44
a = RPythonAnnotator()
45
s = a.build_types(oof, [])
48
assert s == annmodel.SomeOOInstance(I)
50
def test_simple_classof():
51
I = Instance("test", ROOT, {'a': Signed})
57
a = RPythonAnnotator()
58
s = a.build_types(oof, [])
61
assert s == annmodel.SomeOOClass(I)
63
def test_subclassof():
64
I = Instance("test", ROOT, {'a': Signed})
65
I1 = Instance("test1", I)
70
return subclassof(classof(i1), classof(i))
72
a = RPythonAnnotator()
73
s = a.build_types(oof, [])
76
assert s == annmodel.SomeBool()
78
def test_simple_runtimenew():
79
I = Instance("test", ROOT, {'a': Signed})
87
a = RPythonAnnotator()
88
s = a.build_types(oof, [])
91
assert s == annmodel.SomeOOInstance(I)
93
def test_complex_runtimenew():
94
I = Instance("test", ROOT, {'a': Signed})
95
J = Instance("test2", I, {'b': Signed})
96
K = Instance("test2", I, {'b': Signed})
108
a = RPythonAnnotator()
109
s = a.build_types(oof, [bool])
112
assert s == annmodel.SomeOOInstance(I)
115
C = Instance("test", ROOT, {"a": (Signed, 3)})
117
M = Meth([C], Signed)
119
return self.a + other.a
120
m = meth(M, _name="m", _callable=m_)
122
addMethods(C, {"m": m})
128
a = RPythonAnnotator()
129
s = a.build_types(oof, [])
130
# a.translator.view()
132
assert s.knowntype == int
135
C1 = Instance("C1", ROOT)
136
C2 = Instance("C2", C1)
137
C3 = Instance("C3", C1)
146
a = RPythonAnnotator()
147
s = a.build_types(oof, [bool])
150
assert s == annmodel.SomeOOInstance(C1)
152
def test_static_method():
153
F = StaticMethod([Signed, Signed], Signed)
156
f = static_meth(F, "f", _callable=f_)
161
a = RPythonAnnotator()
162
s = a.build_types(oof, [])
165
assert s.knowntype == int
167
def test_null_static_method():
168
F = StaticMethod([Signed, Signed], Signed)
173
a = RPythonAnnotator()
174
s = a.build_types(oof, [])
176
assert s == annmodel.SomeOOStaticMeth(F)
178
def test_truth_value():
179
C = Instance("C", ROOT)
187
a = RPythonAnnotator()
188
s = a.build_types(oof, [bool])
189
assert isinstance(s, annmodel.SomeBool)
190
assert not s.is_constant()
199
a = RPythonAnnotator()
200
s = a.build_types(oof, [])
203
assert s == annmodel.SomeOOInstance(L)
209
a = RPythonAnnotator()
210
s = a.build_types(oof, [])
211
assert s == annmodel.SomeOOInstance(String)
213
def test_nullstring():
220
a = RPythonAnnotator()
221
s = a.build_types(oof, [bool])
222
assert s == annmodel.SomeString(can_be_None=True)
228
a = RPythonAnnotator()
229
s = a.build_types(oof, [])
230
assert isinstance(s, annmodel.SomeBuiltin)
232
def test_ooparse_int():
234
return ooparse_int(oostring(n, b), b)
236
a = RPythonAnnotator()
237
s = a.build_types(oof, [int, int])
238
assert isinstance(s, annmodel.SomeInteger)
240
def test_overloaded_meth():
241
C = Instance("test", ROOT, {},
242
{'foo': overload(meth(Meth([Float], Void)),
243
meth(Meth([Signed], Signed)),
244
meth(Meth([], Float)))})
246
return new(C).foo(42.5)
248
return new(C).foo(42)
251
a = RPythonAnnotator()
252
assert a.build_types(fn1, []) is annmodel.s_None
253
assert isinstance(a.build_types(fn2, []), annmodel.SomeInteger)
254
assert isinstance(a.build_types(fn3, []), annmodel.SomeFloat)
256
def test_bad_overload():
258
C = Instance("test", ROOT, {},
259
{'foo': overload(meth(Meth([Signed], Void)),
260
meth(Meth([Signed], Signed)))})
261
py.test.raises(TypeError, fn)
264
def test_overload_reannotate():
265
C = Instance("test", ROOT, {},
266
{'foo': overload(meth(Meth([Signed], Signed)),
267
meth(Meth([Float], Float)))})
274
a = RPythonAnnotator()
275
assert isinstance(a.build_types(f, []), annmodel.SomeFloat)
277
def test_overload_reannotate_unrelated():
278
py.test.skip("Maybe we want this to work")
279
# this test fails because the result type of c.foo(mylist[0])
280
# changes completely after the list has been modified. We should
281
# handle this case, but it's far from trival.
282
C = Instance("test", ROOT, {},
283
{'foo': overload(meth(Meth([Signed], Void)),
284
meth(Meth([Float], Float)))})
291
a = RPythonAnnotator()
292
assert isinstance(a.build_types(f, []), annmodel.SomeFloat)
294
def test_overload_upcast():
295
C = Instance("base", ROOT, {}, {
296
'foo': overload(meth(Meth([], Void)),
297
meth(Meth([ROOT], Signed)))})
301
a = RPythonAnnotator()
302
assert isinstance(a.build_types(f, []), annmodel.SomeInteger)
304
def test_overload_upcast_fail():
305
C = Instance("base", ROOT, {}, {})
307
'foo': overload(meth(Meth([], Signed)),
308
meth(Meth([ROOT, C], Signed)),
309
meth(Meth([C, ROOT], Signed)))})
313
a = RPythonAnnotator()
314
py.test.raises(TypeError, a.build_types, f, [])