| 1 |
import pytest |
| 2 |
|
| 3 |
from hypothesis import given, assume, settings, HealthCheck |
| 4 |
from hypothesis import strategies as st |
| 5 |
|
| 6 |
from rpython.flowspace.model import Variable |
| 7 |
from rpython.flowspace.operation import op |
| 8 |
from rpython.translator.translator import TranslationContext |
| 9 |
from rpython.annotator.model import * |
| 10 |
from rpython.annotator.annrpython import BlockedInference |
| 11 |
from rpython.annotator.listdef import ListDef |
| 12 |
from rpython.annotator import unaryop, binaryop |
| 13 |
|
| 14 |
@pytest.fixture() |
| 15 |
def annotator(): |
| 16 |
t = TranslationContext() |
| 17 |
return t.buildannotator() |
| 18 |
|
| 19 |
|
| 20 |
listdef1 = ListDef(None, SomeTuple([SomeInteger(nonneg=True), SomeString()])) |
| 21 |
listdef2 = ListDef(None, SomeTuple([SomeInteger(nonneg=False), SomeString()])) |
| 22 |
|
| 23 |
s1 = SomeType() |
| 24 |
s2 = SomeInteger(nonneg=True) |
| 25 |
s3 = SomeInteger(nonneg=False) |
| 26 |
s4 = SomeList(listdef1) |
| 27 |
s5 = SomeList(listdef2) |
| 28 |
s6 = SomeImpossibleValue() |
| 29 |
slist = [s1, s2, s3, s4, s6] |
| 30 |
|
| 31 |
|
| 32 |
class C(object): |
| 33 |
pass |
| 34 |
|
| 35 |
def test_equality(): |
| 36 |
assert s1 != s2 != s3 != s4 != s5 != s6 |
| 37 |
assert s1 == SomeType() |
| 38 |
assert s2 == SomeInteger(nonneg=True) |
| 39 |
assert s3 == SomeInteger(nonneg=False) |
| 40 |
assert s4 == SomeList(listdef1) |
| 41 |
assert s5 == SomeList(listdef2) |
| 42 |
assert s6 == SomeImpossibleValue() |
| 43 |
|
| 44 |
def test_contains(): |
| 45 |
assert ([(s,t) for s in slist for t in slist if s.contains(t)] == |
| 46 |
[(s1, s1), (s1, s6), |
| 47 |
(s2, s2), (s2, s6), |
| 48 |
(s3, s2), (s3, s3), (s3, s6), |
| 49 |
(s4, s4), (s4, s6), |
| 50 |
(s6, s6)]) |
| 51 |
|
| 52 |
def test_signedness(): |
| 53 |
assert not SomeInteger(unsigned=True).contains(SomeInteger()) |
| 54 |
assert SomeInteger(unsigned=True).contains(SomeInteger(nonneg=True)) |
| 55 |
|
| 56 |
def test_commonbase_simple(): |
| 57 |
class A0: |
| 58 |
pass |
| 59 |
class A1(A0): |
| 60 |
pass |
| 61 |
class A2(A0): |
| 62 |
pass |
| 63 |
class B1(object): |
| 64 |
pass |
| 65 |
class B2(object): |
| 66 |
pass |
| 67 |
try: |
| 68 |
class B3(object, A0): |
| 69 |
pass |
| 70 |
except TypeError: |
| 71 |
class B3(A0, object): |
| 72 |
pass |
| 73 |
assert commonbase(A1, A2) is A0 |
| 74 |
assert commonbase(A1, A0) is A0 |
| 75 |
assert commonbase(A1, A1) is A1 |
| 76 |
assert commonbase(A2, B2) is object |
| 77 |
assert commonbase(A2, B3) is A0 |
| 78 |
|
| 79 |
def test_list_union(): |
| 80 |
listdef1 = ListDef('dummy', SomeInteger(nonneg=True)) |
| 81 |
listdef2 = ListDef('dummy', SomeInteger(nonneg=False)) |
| 82 |
s1 = SomeList(listdef1) |
| 83 |
s2 = SomeList(listdef2) |
| 84 |
assert s1 != s2 |
| 85 |
s3 = unionof(s1, s2) |
| 86 |
assert s1 == s2 == s3 |
| 87 |
|
| 88 |
def test_list_contains(): |
| 89 |
listdef1 = ListDef(None, SomeInteger(nonneg=True)) |
| 90 |
s1 = SomeList(listdef1) |
| 91 |
listdef2 = ListDef(None, SomeInteger(nonneg=False)) |
| 92 |
s2 = SomeList(listdef2) |
| 93 |
assert s1 != s2 |
| 94 |
assert not s2.contains(s1) |
| 95 |
assert s1 != s2 |
| 96 |
assert not s1.contains(s2) |
| 97 |
assert s1 != s2 |
| 98 |
|
| 99 |
def test_nan(): |
| 100 |
f1 = SomeFloat() |
| 101 |
f1.const = float("nan") |
| 102 |
f2 = SomeFloat() |
| 103 |
f2.const = float("nan") |
| 104 |
assert f1.contains(f1) |
| 105 |
assert f2.contains(f1) |
| 106 |
assert f1.contains(f2) |
| 107 |
|
| 108 |
def const_float(x): |
| 109 |
s = SomeFloat() |
| 110 |
s.const = x |
| 111 |
return s |
| 112 |
|
| 113 |
def const_int(n): |
| 114 |
s = SomeInteger(nonneg=(n >= 0)) |
| 115 |
s.const = n |
| 116 |
return s |
| 117 |
|
| 118 |
def const_str(x): |
| 119 |
no_nul = not '\x00' in x |
| 120 |
if len(x) == 1: |
| 121 |
result = SomeChar(no_nul=no_nul) |
| 122 |
else: |
| 123 |
result = SomeString(no_nul=no_nul) |
| 124 |
result.const = x |
| 125 |
return result |
| 126 |
|
| 127 |
def const_unicode(x): |
| 128 |
no_nul = not u'\x00' in x |
| 129 |
if len(x) == 1: |
| 130 |
result = SomeUnicodeCodePoint(no_nul=no_nul) |
| 131 |
else: |
| 132 |
result = SomeUnicodeString(no_nul=no_nul) |
| 133 |
result.const = x |
| 134 |
return result |
| 135 |
|
| 136 |
def compatible(s1, s2): |
| 137 |
try: |
| 138 |
union(s1, s2) |
| 139 |
except UnionError: |
| 140 |
return False |
| 141 |
return True |
| 142 |
|
| 143 |
def compatible_pair(pair_s): |
| 144 |
return compatible(*pair_s) |
| 145 |
|
| 146 |
st_const_float = st.builds(const_float, st.floats()) |
| 147 |
st_float = st.just(SomeFloat()) | st_const_float |
| 148 |
st_const_int = st.builds(const_int, st.integers()) |
| 149 |
st_int = st.builds(SomeInteger, st.booleans(), st.booleans()) | st_const_int |
| 150 |
st_const_bool = st.sampled_from([s_True, s_False]) |
| 151 |
st_bool = st.sampled_from([s_Bool, s_True, s_False]) |
| 152 |
st_numeric = st.one_of(st_float, st_int, st_bool) |
| 153 |
st_const_str = st.builds(const_str, st.binary()) |
| 154 |
st_str = st.builds(SomeString, st.booleans(), st.booleans()) | st_const_str |
| 155 |
st_const_unicode = st.builds(const_unicode, st.text()) |
| 156 |
st_unicode = (st.builds(SomeUnicodeString, st.booleans(), st.booleans()) |
| 157 |
| st_const_unicode) |
| 158 |
st_simple = st.one_of(st_numeric, st_str, st_unicode, st.just(s_ImpossibleValue), st.just(s_None)) |
| 159 |
st_const = st.one_of(st_const_float, st_const_int, st_const_bool, |
| 160 |
st_const_str, st_const_unicode, st.just(s_None)) |
| 161 |
|
| 162 |
def valid_unions(st_ann): |
| 163 |
"""From a strategy generating annotations, create a strategy returning |
| 164 |
unions of these annotations.""" |
| 165 |
pairs = st.tuples(st_ann, st_ann) |
| 166 |
return pairs.filter(compatible_pair).map(lambda t: union(*t)) |
| 167 |
|
| 168 |
|
| 169 |
st_annotation = st.recursive(st_simple, |
| 170 |
lambda st_ann: valid_unions(st_ann) | st.builds(SomeTuple, st.lists(st_ann)), |
| 171 |
max_leaves=3) |
| 172 |
|
| 173 |
@given(s=st_annotation) |
| 174 |
def test_union_unary(s): |
| 175 |
assert union(s, s) == s |
| 176 |
assert union(s_ImpossibleValue, s) == s |
| 177 |
|
| 178 |
@given(s1=st_annotation, s2=st_annotation) |
| 179 |
@settings(max_examples=500, suppress_health_check=[HealthCheck.filter_too_much]) |
| 180 |
def test_commutativity_of_union_compatibility(s1, s2): |
| 181 |
assert compatible(s1, s2) == compatible(s2, s1) |
| 182 |
|
| 183 |
@given(st_annotation, st_annotation) |
| 184 |
@settings(max_examples=500, suppress_health_check=[HealthCheck.filter_too_much]) |
| 185 |
def test_union_commutative(s1, s2): |
| 186 |
try: |
| 187 |
s_union = union(s1, s2) |
| 188 |
except UnionError: |
| 189 |
assume(False) |
| 190 |
assert union(s2, s1) == s_union |
| 191 |
assert s_union.contains(s1) |
| 192 |
assert s_union.contains(s2) |
| 193 |
|
| 194 |
@pytest.mark.xfail |
| 195 |
@settings(max_examples=500, suppress_health_check=[HealthCheck.filter_too_much]) |
| 196 |
@given(st_annotation, st_annotation, st_annotation) |
| 197 |
def test_union_associative(s1, s2, s3): |
| 198 |
assume(compatible(s1, s2) and compatible(union(s1, s2), s3)) |
| 199 |
assert union(union(s1, s2), s3) == union(s1, union(s2, s3)) |
| 200 |
|
| 201 |
@given(s_const=st_const, s_obj=st_annotation) |
| 202 |
@settings(max_examples=500, suppress_health_check=[HealthCheck.filter_too_much]) |
| 203 |
def test_constants_are_atoms(s_const, s_obj): |
| 204 |
assume(s_const.contains(s_obj)) |
| 205 |
assert s_const == s_obj or s_obj == s_ImpossibleValue |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.xfail |
| 209 |
@given(st_annotation, st_annotation) |
| 210 |
@settings(max_examples=500, suppress_health_check=[HealthCheck.filter_too_much]) |
| 211 |
def test_generalize_isinstance(annotator, s1, s2): |
| 212 |
try: |
| 213 |
s_12 = union(s1, s2) |
| 214 |
except UnionError: |
| 215 |
assume(False) |
| 216 |
assume(s1 != s_ImpossibleValue) |
| 217 |
from rpython.annotator.unaryop import s_isinstance |
| 218 |
s_int = annotator.bookkeeper.immutablevalue(int) |
| 219 |
s_res_12 = s_isinstance(annotator, s_12, s_int, []) |
| 220 |
s_res_1 = s_isinstance(annotator, s1, s_int, []) |
| 221 |
assert s_res_12.contains(s_res_1) |
| 222 |
|
| 223 |
def compile_function(function, annotation=[]): |
| 224 |
t = TranslationContext() |
| 225 |
t.buildannotator().build_types(function, annotation) |
| 226 |
|
| 227 |
class AAA(object): |
| 228 |
pass |
| 229 |
|
| 230 |
def test_blocked_inference1(annotator): |
| 231 |
def blocked_inference(): |
| 232 |
return AAA().m() |
| 233 |
|
| 234 |
with pytest.raises(AnnotatorError): |
| 235 |
annotator.build_types(blocked_inference, []) |
| 236 |
|
| 237 |
def test_blocked_inference2(annotator): |
| 238 |
def blocked_inference(): |
| 239 |
a = AAA() |
| 240 |
b = a.x |
| 241 |
return b |
| 242 |
|
| 243 |
with pytest.raises(AnnotatorError): |
| 244 |
annotator.build_types(blocked_inference, []) |
| 245 |
|
| 246 |
|
| 247 |
def test_not_const(): |
| 248 |
s_int = SomeInteger() |
| 249 |
s_int.const = 2 |
| 250 |
assert s_int != SomeInteger() |
| 251 |
assert not_const(s_int) == SomeInteger() |
| 252 |
assert not_const(s_None) == s_None |
| 253 |
|
| 254 |
|
| 255 |
def test_nonnulify(): |
| 256 |
s = SomeString(can_be_None=True).nonnulify() |
| 257 |
assert s.can_be_None is True |
| 258 |
assert s.no_nul is True |
| 259 |
s = SomeChar().nonnulify() |
| 260 |
assert s.no_nul is True |
| 261 |
|
| 262 |
def test_SomeException_union(annotator): |
| 263 |
bk = annotator.bookkeeper |
| 264 |
someinst = lambda cls, **kw: SomeInstance(bk.getuniqueclassdef(cls), **kw) |
| 265 |
s_inst = someinst(Exception) |
| 266 |
s_exc = bk.new_exception([ValueError, IndexError]) |
| 267 |
assert union(s_exc, s_inst) == s_inst |
| 268 |
assert union(s_inst, s_exc) == s_inst |
| 269 |
s_nullable = union(s_None, bk.new_exception([ValueError])) |
| 270 |
assert isinstance(s_nullable, SomeInstance) |
| 271 |
assert s_nullable.can_be_None |
| 272 |
s_exc1 = bk.new_exception([ValueError]) |
| 273 |
s_exc2 = bk.new_exception([IndexError]) |
| 274 |
union(s_exc1, s_exc2) == union(s_exc2, s_exc1) |
| 275 |
|
| 276 |
def contains_s(s_a, s_b): |
| 277 |
if s_b is None: |
| 278 |
return True |
| 279 |
elif s_a is None: |
| 280 |
return False |
| 281 |
else: |
| 282 |
return s_a.contains(s_b) |
| 283 |
|
| 284 |
def annotate_op(ann, hlop, args_s): |
| 285 |
for v_arg, s_arg in zip(hlop.args, args_s): |
| 286 |
ann.setbinding(v_arg, s_arg) |
| 287 |
with ann.bookkeeper.at_position(None): |
| 288 |
try: |
| 289 |
ann.consider_op(hlop) |
| 290 |
except BlockedInference: |
| 291 |
|
| 292 |
|
| 293 |
pass |
| 294 |
return hlop.result.annotation, ann.get_exception(hlop) |
| 295 |
|
| 296 |
def test_generalize_getitem_dict(annotator): |
| 297 |
bk = annotator.bookkeeper |
| 298 |
hlop = op.getitem(Variable(), Variable()) |
| 299 |
s_int = SomeInteger() |
| 300 |
with bk.at_position(None): |
| 301 |
s_empty_dict = bk.newdict() |
| 302 |
s_value, s_exc = annotate_op(annotator, hlop, [s_None, s_int]) |
| 303 |
s_value2, s_exc2 = annotate_op(annotator, hlop, [s_empty_dict, s_int]) |
| 304 |
assert contains_s(s_value2, s_value) |
| 305 |
assert contains_s(s_exc2, s_exc) |
| 306 |
|
| 307 |
def test_generalize_getitem_list(annotator): |
| 308 |
bk = annotator.bookkeeper |
| 309 |
hlop = op.getitem(Variable(), Variable()) |
| 310 |
s_int = SomeInteger() |
| 311 |
with bk.at_position(None): |
| 312 |
s_empty_list = bk.newlist() |
| 313 |
s_value, s_exc = annotate_op(annotator, hlop, [s_None, s_int]) |
| 314 |
s_value2, s_exc2 = annotate_op(annotator, hlop, [s_empty_list, s_int]) |
| 315 |
assert contains_s(s_value2, s_value) |
| 316 |
assert contains_s(s_exc2, s_exc) |
| 317 |
|
| 318 |
def test_generalize_getitem_string(annotator): |
| 319 |
hlop = op.getitem(Variable(), Variable()) |
| 320 |
s_int = SomeInteger() |
| 321 |
s_str = SomeString(can_be_None=True) |
| 322 |
s_value, s_exc = annotate_op(annotator, hlop, [s_None, s_int]) |
| 323 |
s_value2, s_exc2 = annotate_op(annotator, hlop, [s_str, s_int]) |
| 324 |
assert contains_s(s_value2, s_value) |
| 325 |
assert contains_s(s_exc2, s_exc) |
| 326 |
|
| 327 |
def test_generalize_string_concat(annotator): |
| 328 |
hlop = op.add(Variable(), Variable()) |
| 329 |
s_str = SomeString(can_be_None=True) |
| 330 |
s_value, s_exc = annotate_op(annotator, hlop, [s_None, s_str]) |
| 331 |
s_value2, s_exc2 = annotate_op(annotator, hlop, [s_str, s_str]) |
| 332 |
assert contains_s(s_value2, s_value) |
| 333 |
assert contains_s(s_exc2, s_exc) |
| 334 |
|
| 335 |
def test_getitem_dict(annotator): |
| 336 |
bk = annotator.bookkeeper |
| 337 |
hlop = op.getitem(Variable(), Variable()) |
| 338 |
with bk.at_position(None): |
| 339 |
s_dict = bk.newdict() |
| 340 |
s_dict.dictdef.generalize_key(SomeString()) |
| 341 |
s_dict.dictdef.generalize_value(SomeInteger()) |
| 342 |
s_result, _ = annotate_op(annotator, hlop, [s_dict, SomeString()]) |
| 343 |
assert s_result == SomeInteger() |