| 1 |
from __future__ import with_statement, print_function |
| 2 |
import py.test |
| 3 |
import sys |
| 4 |
from collections import OrderedDict |
| 5 |
|
| 6 |
from rpython.conftest import option |
| 7 |
|
| 8 |
from rpython.annotator import model as annmodel |
| 9 |
from rpython.annotator.model import AnnotatorError, UnionError |
| 10 |
from rpython.annotator.annrpython import RPythonAnnotator as _RPythonAnnotator |
| 11 |
from rpython.annotator.classdesc import NoSuchAttrError |
| 12 |
from rpython.translator.translator import graphof as tgraphof |
| 13 |
from rpython.annotator.policy import AnnotatorPolicy |
| 14 |
from rpython.annotator.signature import Sig, SignatureError |
| 15 |
from rpython.annotator.listdef import ListDef, ListChangeUnallowed |
| 16 |
from rpython.annotator.dictdef import DictDef |
| 17 |
from rpython.flowspace.model import * |
| 18 |
from rpython.rlib.rarithmetic import r_uint, base_int, r_longlong, r_ulonglong |
| 19 |
from rpython.rlib.rarithmetic import r_singlefloat |
| 20 |
from rpython.rlib import objectmodel |
| 21 |
from rpython.flowspace.flowcontext import FlowingError |
| 22 |
from rpython.flowspace.operation import op |
| 23 |
|
| 24 |
from rpython.translator.test import snippet |
| 25 |
|
| 26 |
def graphof(a, func): |
| 27 |
return tgraphof(a.translator, func) |
| 28 |
|
| 29 |
def listitem(s_list): |
| 30 |
assert isinstance(s_list, annmodel.SomeList) |
| 31 |
return s_list.listdef.listitem.s_value |
| 32 |
|
| 33 |
def somelist(s_type): |
| 34 |
return annmodel.SomeList(ListDef(None, s_type)) |
| 35 |
|
| 36 |
def dictkey(s_dict): |
| 37 |
assert isinstance(s_dict, annmodel.SomeDict) |
| 38 |
return s_dict.dictdef.dictkey.s_value |
| 39 |
|
| 40 |
def dictvalue(s_dict): |
| 41 |
assert isinstance(s_dict, annmodel.SomeDict) |
| 42 |
return s_dict.dictdef.dictvalue.s_value |
| 43 |
|
| 44 |
def somedict(annotator, s_key, s_value): |
| 45 |
return annmodel.SomeDict(DictDef(annotator.bookkeeper, s_key, s_value)) |
| 46 |
|
| 47 |
|
| 48 |
class TestAnnotateTestCase: |
| 49 |
def teardown_method(self, meth): |
| 50 |
assert annmodel.s_Bool == annmodel.SomeBool() |
| 51 |
|
| 52 |
class RPythonAnnotator(_RPythonAnnotator): |
| 53 |
def build_types(self, *args): |
| 54 |
s = _RPythonAnnotator.build_types(self, *args) |
| 55 |
self.validate() |
| 56 |
if option.view: |
| 57 |
self.translator.view() |
| 58 |
return s |
| 59 |
|
| 60 |
def test_simple_func(self): |
| 61 |
""" |
| 62 |
one test source: |
| 63 |
def f(x): |
| 64 |
return x+1 |
| 65 |
""" |
| 66 |
x = Variable("x") |
| 67 |
oper = op.add(x, Constant(1)) |
| 68 |
block = Block([x]) |
| 69 |
fun = FunctionGraph("f", block) |
| 70 |
block.operations.append(oper) |
| 71 |
block.closeblock(Link([oper.result], fun.returnblock)) |
| 72 |
a = self.RPythonAnnotator() |
| 73 |
a.addpendingblock(fun, fun.startblock, [annmodel.SomeInteger()]) |
| 74 |
a.complete() |
| 75 |
assert a.gettype(fun.getreturnvar()) == int |
| 76 |
|
| 77 |
def test_while(self): |
| 78 |
""" |
| 79 |
one test source: |
| 80 |
def f(i): |
| 81 |
while i > 0: |
| 82 |
i = i - 1 |
| 83 |
return i |
| 84 |
""" |
| 85 |
i1 = Variable("i1") |
| 86 |
i2 = Variable("i2") |
| 87 |
conditionop = op.gt(i1, Constant(0)) |
| 88 |
decop = op.add(i2, Constant(-1)) |
| 89 |
headerblock = Block([i1]) |
| 90 |
whileblock = Block([i2]) |
| 91 |
|
| 92 |
fun = FunctionGraph("f", headerblock) |
| 93 |
headerblock.operations.append(conditionop) |
| 94 |
headerblock.exitswitch = conditionop.result |
| 95 |
headerblock.closeblock(Link([i1], fun.returnblock, False), |
| 96 |
Link([i1], whileblock, True)) |
| 97 |
whileblock.operations.append(decop) |
| 98 |
whileblock.closeblock(Link([decop.result], headerblock)) |
| 99 |
|
| 100 |
a = self.RPythonAnnotator() |
| 101 |
a.addpendingblock(fun, fun.startblock, [annmodel.SomeInteger()]) |
| 102 |
a.complete() |
| 103 |
assert a.gettype(fun.getreturnvar()) == int |
| 104 |
|
| 105 |
def test_while_sum(self): |
| 106 |
""" |
| 107 |
one test source: |
| 108 |
def f(i): |
| 109 |
sum = 0 |
| 110 |
while i > 0: |
| 111 |
sum = sum + i |
| 112 |
i = i - 1 |
| 113 |
return sum |
| 114 |
""" |
| 115 |
i1 = Variable("i1") |
| 116 |
i2 = Variable("i2") |
| 117 |
i3 = Variable("i3") |
| 118 |
sum2 = Variable("sum2") |
| 119 |
sum3 = Variable("sum3") |
| 120 |
|
| 121 |
conditionop = op.gt(i2, Constant(0)) |
| 122 |
decop = op.add(i3, Constant(-1)) |
| 123 |
addop = op.add(i3, sum3) |
| 124 |
startblock = Block([i1]) |
| 125 |
headerblock = Block([i2, sum2]) |
| 126 |
whileblock = Block([i3, sum3]) |
| 127 |
|
| 128 |
fun = FunctionGraph("f", startblock) |
| 129 |
startblock.closeblock(Link([i1, Constant(0)], headerblock)) |
| 130 |
headerblock.operations.append(conditionop) |
| 131 |
headerblock.exitswitch = conditionop.result |
| 132 |
headerblock.closeblock(Link([sum2], fun.returnblock, False), |
| 133 |
Link([i2, sum2], whileblock, True)) |
| 134 |
whileblock.operations.append(addop) |
| 135 |
whileblock.operations.append(decop) |
| 136 |
whileblock.closeblock(Link([decop.result, addop.result], headerblock)) |
| 137 |
|
| 138 |
a = self.RPythonAnnotator() |
| 139 |
a.addpendingblock(fun, fun.startblock, [annmodel.SomeInteger()]) |
| 140 |
a.complete() |
| 141 |
assert a.gettype(fun.getreturnvar()) == int |
| 142 |
|
| 143 |
def test_f_calls_g(self): |
| 144 |
a = self.RPythonAnnotator() |
| 145 |
s = a.build_types(f_calls_g, [int]) |
| 146 |
|
| 147 |
assert s.knowntype == int |
| 148 |
|
| 149 |
def test_not_rpython(self): |
| 150 |
def g(x): |
| 151 |
""" NOT_RPYTHON """ |
| 152 |
return eval(x) |
| 153 |
def f(x): |
| 154 |
return g(str(x)) |
| 155 |
a = self.RPythonAnnotator() |
| 156 |
with py.test.raises(ValueError): |
| 157 |
a.build_types(f, [int]) |
| 158 |
|
| 159 |
def test_not_rpython_decorator(self): |
| 160 |
from rpython.rlib.objectmodel import not_rpython |
| 161 |
@not_rpython |
| 162 |
def g(x): |
| 163 |
return eval(x) |
| 164 |
def f(x): |
| 165 |
return g(str(x)) |
| 166 |
a = self.RPythonAnnotator() |
| 167 |
with py.test.raises(ValueError): |
| 168 |
a.build_types(f, [int]) |
| 169 |
|
| 170 |
def test_lists(self): |
| 171 |
a = self.RPythonAnnotator() |
| 172 |
end_cell = a.build_types(snippet.poor_man_rev_range, [int]) |
| 173 |
|
| 174 |
assert listitem(end_cell).knowntype == int |
| 175 |
|
| 176 |
def test_factorial(self): |
| 177 |
a = self.RPythonAnnotator() |
| 178 |
s = a.build_types(snippet.factorial, [int]) |
| 179 |
|
| 180 |
assert s.knowntype == int |
| 181 |
|
| 182 |
def test_factorial2(self): |
| 183 |
a = self.RPythonAnnotator() |
| 184 |
s = a.build_types(snippet.factorial2, [int]) |
| 185 |
|
| 186 |
assert s.knowntype == int |
| 187 |
|
| 188 |
def test_build_instance(self): |
| 189 |
a = self.RPythonAnnotator() |
| 190 |
s = a.build_types(snippet.build_instance, []) |
| 191 |
|
| 192 |
assert isinstance(s, annmodel.SomeInstance) |
| 193 |
assert s.classdef == a.bookkeeper.getuniqueclassdef(snippet.C) |
| 194 |
|
| 195 |
def test_set_attr(self): |
| 196 |
a = self.RPythonAnnotator() |
| 197 |
s = a.build_types(snippet.set_attr, []) |
| 198 |
|
| 199 |
assert s.knowntype == int |
| 200 |
|
| 201 |
def test_merge_setattr(self): |
| 202 |
a = self.RPythonAnnotator() |
| 203 |
s = a.build_types(snippet.merge_setattr, [int]) |
| 204 |
|
| 205 |
assert s.knowntype == int |
| 206 |
|
| 207 |
def test_inheritance1(self): |
| 208 |
a = self.RPythonAnnotator() |
| 209 |
s = a.build_types(snippet.inheritance1, []) |
| 210 |
|
| 211 |
assert s == annmodel.SomeTuple([ |
| 212 |
a.bookkeeper.immutablevalue(()), |
| 213 |
annmodel.SomeInteger() |
| 214 |
]) |
| 215 |
|
| 216 |
def test_poor_man_range(self): |
| 217 |
a = self.RPythonAnnotator() |
| 218 |
s = a.build_types(snippet.poor_man_range, [int]) |
| 219 |
|
| 220 |
assert listitem(s).knowntype == int |
| 221 |
|
| 222 |
def test_staticmethod(self): |
| 223 |
class X(object): |
| 224 |
@staticmethod |
| 225 |
def stat(value): |
| 226 |
return value + 4 |
| 227 |
def f(v): |
| 228 |
return X().stat(v) |
| 229 |
a = self.RPythonAnnotator() |
| 230 |
s = a.build_types(f, [int]) |
| 231 |
assert isinstance(s, annmodel.SomeInteger) |
| 232 |
|
| 233 |
def test_classmethod(self): |
| 234 |
class X(object): |
| 235 |
@classmethod |
| 236 |
def meth(cls): |
| 237 |
return None |
| 238 |
def f(): |
| 239 |
return X().meth() |
| 240 |
a = self.RPythonAnnotator() |
| 241 |
py.test.raises(AnnotatorError, a.build_types, f, []) |
| 242 |
|
| 243 |
def test_methodcall1(self): |
| 244 |
a = self.RPythonAnnotator() |
| 245 |
s = a.build_types(snippet._methodcall1, [int]) |
| 246 |
|
| 247 |
assert s.knowntype == tuple |
| 248 |
assert len(s.items) == 2 |
| 249 |
s0 = s.items[0] |
| 250 |
assert isinstance(s0, annmodel.SomeInstance) |
| 251 |
assert s0.classdef == a.bookkeeper.getuniqueclassdef(snippet.C) |
| 252 |
assert s.items[1].knowntype == int |
| 253 |
assert s.items[1].nonneg == True |
| 254 |
|
| 255 |
def test_classes_methodcall1(self): |
| 256 |
a = self.RPythonAnnotator() |
| 257 |
a.build_types(snippet._methodcall1, [int]) |
| 258 |
|
| 259 |
getcdef = a.bookkeeper.getuniqueclassdef |
| 260 |
assert getcdef(snippet.F).attrs.keys() == ['m'] |
| 261 |
assert getcdef(snippet.G).attrs.keys() == ['m2'] |
| 262 |
assert getcdef(snippet.H).attrs.keys() == ['attr'] |
| 263 |
assert getcdef(snippet.H).about_attribute('attr') == ( |
| 264 |
a.bookkeeper.immutablevalue(1)) |
| 265 |
|
| 266 |
def test_generaldict(self): |
| 267 |
a = self.RPythonAnnotator() |
| 268 |
s = a.build_types(snippet.generaldict, [str, int, str, int]) |
| 269 |
|
| 270 |
assert s.knowntype == int |
| 271 |
|
| 272 |
def test_somebug1(self): |
| 273 |
a = self.RPythonAnnotator() |
| 274 |
s = a.build_types(snippet._somebug1, [int]) |
| 275 |
|
| 276 |
assert isinstance(s, annmodel.SomeBuiltin) |
| 277 |
|
| 278 |
def test_with_init(self): |
| 279 |
a = self.RPythonAnnotator() |
| 280 |
s = a.build_types(snippet.with_init, [int]) |
| 281 |
|
| 282 |
assert s.knowntype == int |
| 283 |
|
| 284 |
def test_with_more_init(self): |
| 285 |
a = self.RPythonAnnotator() |
| 286 |
s = a.build_types(snippet.with_more_init, [int, bool]) |
| 287 |
|
| 288 |
getcdef = a.bookkeeper.getuniqueclassdef |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
assert getcdef(snippet.WithMoreInit).about_attribute('a') == ( |
| 293 |
annmodel.SomeInteger()) |
| 294 |
assert getcdef(snippet.WithMoreInit).about_attribute('b') == ( |
| 295 |
annmodel.SomeBool()) |
| 296 |
|
| 297 |
def test_global_instance(self): |
| 298 |
a = self.RPythonAnnotator() |
| 299 |
s = a.build_types(snippet.global_instance, []) |
| 300 |
|
| 301 |
|
| 302 |
assert s == a.bookkeeper.immutablevalue(42) |
| 303 |
|
| 304 |
def test_call_five(self): |
| 305 |
a = self.RPythonAnnotator() |
| 306 |
s = a.build_types(snippet.call_five, []) |
| 307 |
|
| 308 |
assert listitem(s) == a.bookkeeper.immutablevalue(5) |
| 309 |
|
| 310 |
def test_call_five_six(self): |
| 311 |
a = self.RPythonAnnotator() |
| 312 |
s = a.build_types(snippet.call_five_six, []) |
| 313 |
|
| 314 |
assert listitem(s) == annmodel.SomeInteger(nonneg=True) |
| 315 |
|
| 316 |
def test_constant_result(self): |
| 317 |
a = self.RPythonAnnotator() |
| 318 |
s = a.build_types(snippet.constant_result, []) |
| 319 |
|
| 320 |
|
| 321 |
assert s == a.bookkeeper.immutablevalue("yadda") |
| 322 |
graphs = a.translator.graphs |
| 323 |
assert len(graphs) == 2 |
| 324 |
assert graphs[0].func is snippet.constant_result |
| 325 |
assert graphs[1].func is snippet.forty_two |
| 326 |
a.simplify() |
| 327 |
|
| 328 |
|
| 329 |
def test_flow_type_info(self): |
| 330 |
a = self.RPythonAnnotator() |
| 331 |
s = a.build_types(snippet.flow_type_info, [int]) |
| 332 |
a.simplify() |
| 333 |
assert s.knowntype == int |
| 334 |
|
| 335 |
a = self.RPythonAnnotator() |
| 336 |
s = a.build_types(snippet.flow_type_info, [str]) |
| 337 |
a.simplify() |
| 338 |
assert s.knowntype == int |
| 339 |
|
| 340 |
def test_flow_type_info_2(self): |
| 341 |
a = self.RPythonAnnotator() |
| 342 |
s = a.build_types(snippet.flow_type_info, |
| 343 |
[annmodel.SomeInteger(nonneg=True)]) |
| 344 |
|
| 345 |
|
| 346 |
assert s == annmodel.SomeInteger(nonneg=True) |
| 347 |
|
| 348 |
def test_flow_usertype_info(self): |
| 349 |
a = self.RPythonAnnotator() |
| 350 |
s = a.build_types(snippet.flow_usertype_info, [snippet.WithInit]) |
| 351 |
|
| 352 |
assert isinstance(s, annmodel.SomeInstance) |
| 353 |
assert s.classdef == a.bookkeeper.getuniqueclassdef(snippet.WithInit) |
| 354 |
|
| 355 |
def test_flow_usertype_info2(self): |
| 356 |
a = self.RPythonAnnotator() |
| 357 |
s = a.build_types(snippet.flow_usertype_info, [snippet.WithMoreInit]) |
| 358 |
|
| 359 |
assert isinstance(s, annmodel.SomeInstance) |
| 360 |
assert s.classdef == a.bookkeeper.getuniqueclassdef(snippet.WithMoreInit) |
| 361 |
|
| 362 |
def test_mergefunctions(self): |
| 363 |
a = self.RPythonAnnotator() |
| 364 |
s = a.build_types(snippet.mergefunctions, [int]) |
| 365 |
|
| 366 |
|
| 367 |
assert isinstance(s, annmodel.SomePBC) |
| 368 |
|
| 369 |
def test_func_calls_func_which_just_raises(self): |
| 370 |
a = self.RPythonAnnotator() |
| 371 |
s = a.build_types(snippet.funccallsex, []) |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
def test_tuple_unpack_from_const_tuple_with_different_types(self): |
| 377 |
a = self.RPythonAnnotator() |
| 378 |
s = a.build_types(snippet.func_arg_unpack, []) |
| 379 |
assert isinstance(s, annmodel.SomeInteger) |
| 380 |
assert s.const == 3 |
| 381 |
|
| 382 |
def test_star_unpack_list(self): |
| 383 |
def g(): |
| 384 |
pass |
| 385 |
def f(l): |
| 386 |
return g(*l) |
| 387 |
a = self.RPythonAnnotator() |
| 388 |
with py.test.raises(AnnotatorError): |
| 389 |
a.build_types(f, [[int]]) |
| 390 |
|
| 391 |
def test_star_unpack_and_keywords(self): |
| 392 |
def g(a, b, c=0, d=0): |
| 393 |
return a + b + c + d |
| 394 |
|
| 395 |
def f(a, b): |
| 396 |
return g(a, *(b,), d=5) |
| 397 |
a = self.RPythonAnnotator() |
| 398 |
s_result = a.build_types(f, [int, int]) |
| 399 |
assert isinstance(s_result, annmodel.SomeInteger) |
| 400 |
|
| 401 |
def test_pbc_attr_preserved_on_instance(self): |
| 402 |
a = self.RPythonAnnotator() |
| 403 |
s = a.build_types(snippet.preserve_pbc_attr_on_instance, [bool]) |
| 404 |
|
| 405 |
|
| 406 |
assert s == annmodel.SomeInteger(nonneg=True) |
| 407 |
|
| 408 |
|
| 409 |
def test_pbc_attr_preserved_on_instance_with_slots(self): |
| 410 |
a = self.RPythonAnnotator() |
| 411 |
s = a.build_types(snippet.preserve_pbc_attr_on_instance_with_slots, |
| 412 |
[bool]) |
| 413 |
assert s == annmodel.SomeInteger(nonneg=True) |
| 414 |
|
| 415 |
def test_is_and_knowntype_data(self): |
| 416 |
a = self.RPythonAnnotator() |
| 417 |
s = a.build_types(snippet.is_and_knowntype, [str]) |
| 418 |
|
| 419 |
|
| 420 |
assert s == a.bookkeeper.immutablevalue(None) |
| 421 |
|
| 422 |
def test_isinstance_and_knowntype_data(self): |
| 423 |
a = self.RPythonAnnotator() |
| 424 |
x = a.bookkeeper.immutablevalue(snippet.apbc) |
| 425 |
s = a.build_types(snippet.isinstance_and_knowntype, [x]) |
| 426 |
|
| 427 |
|
| 428 |
assert s == x |
| 429 |
|
| 430 |
def test_somepbc_simplify(self): |
| 431 |
a = self.RPythonAnnotator() |
| 432 |
|
| 433 |
a.build_types(snippet.somepbc_simplify, []) |
| 434 |
|
| 435 |
def test_builtin_methods(self): |
| 436 |
a = self.RPythonAnnotator() |
| 437 |
iv = a.bookkeeper.immutablevalue |
| 438 |
|
| 439 |
|
| 440 |
for example, methname, s_example in [ |
| 441 |
('', 'join', annmodel.SomeString()), |
| 442 |
([], 'append', somelist(annmodel.s_Int)), |
| 443 |
([], 'extend', somelist(annmodel.s_Int)), |
| 444 |
([], 'reverse', somelist(annmodel.s_Int)), |
| 445 |
([], 'insert', somelist(annmodel.s_Int)), |
| 446 |
([], 'pop', somelist(annmodel.s_Int)), |
| 447 |
]: |
| 448 |
constmeth = getattr(example, methname) |
| 449 |
s_constmeth = iv(constmeth) |
| 450 |
assert isinstance(s_constmeth, annmodel.SomeBuiltin) |
| 451 |
s_meth = s_example.getattr(iv(methname)) |
| 452 |
assert isinstance(s_constmeth, annmodel.SomeBuiltin) |
| 453 |
|
| 454 |
def test_str_join(self): |
| 455 |
a = self.RPythonAnnotator() |
| 456 |
def g(n): |
| 457 |
if n: |
| 458 |
return ["foo", "bar"] |
| 459 |
def f(n): |
| 460 |
g(0) |
| 461 |
return ''.join(g(n)) |
| 462 |
s = a.build_types(f, [int]) |
| 463 |
assert s.knowntype == str |
| 464 |
assert s.no_nul |
| 465 |
|
| 466 |
def test_unicode_join(self): |
| 467 |
a = self.RPythonAnnotator() |
| 468 |
def g(n): |
| 469 |
if n: |
| 470 |
return [u"foo", u"bar"] |
| 471 |
def f(n): |
| 472 |
g(0) |
| 473 |
return u''.join(g(n)) |
| 474 |
s = a.build_types(f, [int]) |
| 475 |
assert s.knowntype == unicode |
| 476 |
assert s.no_nul |
| 477 |
|
| 478 |
def test_str_split(self): |
| 479 |
a = self.RPythonAnnotator() |
| 480 |
def g(n): |
| 481 |
if n: |
| 482 |
return "test string" |
| 483 |
def f(n): |
| 484 |
if n: |
| 485 |
return g(n).split(' ') |
| 486 |
s = a.build_types(f, [int]) |
| 487 |
assert isinstance(s, annmodel.SomeList) |
| 488 |
s_item = s.listdef.listitem.s_value |
| 489 |
assert s_item.no_nul |
| 490 |
|
| 491 |
def test_unicode_split(self): |
| 492 |
a = self.RPythonAnnotator() |
| 493 |
def g(n): |
| 494 |
if n: |
| 495 |
return u"test string" |
| 496 |
def f(n): |
| 497 |
if n: |
| 498 |
return g(n).split(u' ') |
| 499 |
s = a.build_types(f, [int]) |
| 500 |
assert isinstance(s, annmodel.SomeList) |
| 501 |
s_item = s.listdef.listitem.s_value |
| 502 |
assert s_item.no_nul |
| 503 |
|
| 504 |
def test_str_split_nul(self): |
| 505 |
def f(n): |
| 506 |
return n.split('\0')[0] |
| 507 |
a = self.RPythonAnnotator() |
| 508 |
a.translator.config.translation.check_str_without_nul = True |
| 509 |
s = a.build_types(f, [annmodel.SomeString(no_nul=False, can_be_None=False)]) |
| 510 |
assert isinstance(s, annmodel.SomeString) |
| 511 |
assert not s.can_be_None |
| 512 |
assert s.no_nul |
| 513 |
|
| 514 |
def g(n): |
| 515 |
return n.split('\0', 1)[0] |
| 516 |
a = self.RPythonAnnotator() |
| 517 |
a.translator.config.translation.check_str_without_nul = True |
| 518 |
s = a.build_types(g, [annmodel.SomeString(no_nul=False, can_be_None=False)]) |
| 519 |
assert isinstance(s, annmodel.SomeString) |
| 520 |
assert not s.can_be_None |
| 521 |
assert not s.no_nul |
| 522 |
|
| 523 |
def test_unicode_split_nul(self): |
| 524 |
def f(n): |
| 525 |
return n.split(u'\0')[0] |
| 526 |
a = self.RPythonAnnotator() |
| 527 |
a.translator.config.translation.check_str_without_nul = True |
| 528 |
s = a.build_types(f, [annmodel.SomeUnicodeString( |
| 529 |
no_nul=False, can_be_None=False)]) |
| 530 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 531 |
assert not s.can_be_None |
| 532 |
assert s.no_nul |
| 533 |
|
| 534 |
def g(n): |
| 535 |
return n.split(u'\0', 1)[0] |
| 536 |
a = self.RPythonAnnotator() |
| 537 |
a.translator.config.translation.check_str_without_nul = True |
| 538 |
s = a.build_types(g, [annmodel.SomeUnicodeString( |
| 539 |
no_nul=False, can_be_None=False)]) |
| 540 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 541 |
assert not s.can_be_None |
| 542 |
assert not s.no_nul |
| 543 |
|
| 544 |
def test_str_splitlines(self): |
| 545 |
a = self.RPythonAnnotator() |
| 546 |
def f(a_str): |
| 547 |
return a_str.splitlines() |
| 548 |
s = a.build_types(f, [str]) |
| 549 |
assert isinstance(s, annmodel.SomeList) |
| 550 |
assert s.listdef.listitem.resized |
| 551 |
|
| 552 |
def test_str_strip(self): |
| 553 |
a = self.RPythonAnnotator() |
| 554 |
def f(n, a_str): |
| 555 |
if n == 0: |
| 556 |
return a_str.strip(' ') |
| 557 |
elif n == 1: |
| 558 |
return a_str.rstrip(' ') |
| 559 |
else: |
| 560 |
return a_str.lstrip(' ') |
| 561 |
s = a.build_types(f, [int, annmodel.SomeString(no_nul=True)]) |
| 562 |
assert s.no_nul |
| 563 |
|
| 564 |
def test_unicode_strip(self): |
| 565 |
a = self.RPythonAnnotator() |
| 566 |
def f(n, a_str): |
| 567 |
if n == 0: |
| 568 |
return a_str.strip(u' ') |
| 569 |
elif n == 1: |
| 570 |
return a_str.rstrip(u' ') |
| 571 |
else: |
| 572 |
return a_str.lstrip(u' ') |
| 573 |
s = a.build_types(f, [int, annmodel.SomeUnicodeString(no_nul=True)]) |
| 574 |
assert s.no_nul |
| 575 |
|
| 576 |
def test_str_mul(self): |
| 577 |
a = self.RPythonAnnotator() |
| 578 |
def f(a_str): |
| 579 |
return a_str * 3 |
| 580 |
s = a.build_types(f, [str]) |
| 581 |
assert isinstance(s, annmodel.SomeString) |
| 582 |
|
| 583 |
def test_str_isalpha(self): |
| 584 |
def f(s): |
| 585 |
return s.isalpha() |
| 586 |
a = self.RPythonAnnotator() |
| 587 |
s = a.build_types(f, [str]) |
| 588 |
assert isinstance(s, annmodel.SomeBool) |
| 589 |
|
| 590 |
def test_simple_slicing(self): |
| 591 |
a = self.RPythonAnnotator() |
| 592 |
s = a.build_types(snippet.simple_slice, [somelist(annmodel.s_Int)]) |
| 593 |
assert isinstance(s, annmodel.SomeList) |
| 594 |
|
| 595 |
def test_simple_iter_list(self): |
| 596 |
a = self.RPythonAnnotator() |
| 597 |
s = a.build_types(snippet.simple_iter, [somelist(annmodel.s_Int)]) |
| 598 |
assert isinstance(s, annmodel.SomeIterator) |
| 599 |
|
| 600 |
def test_simple_iter_next(self): |
| 601 |
def f(x): |
| 602 |
i = iter(range(x)) |
| 603 |
return i.next() |
| 604 |
a = self.RPythonAnnotator() |
| 605 |
s = a.build_types(f, [int]) |
| 606 |
assert isinstance(s, annmodel.SomeInteger) |
| 607 |
|
| 608 |
def test_simple_iter_dict(self): |
| 609 |
a = self.RPythonAnnotator() |
| 610 |
t = somedict(a, annmodel.SomeInteger(), annmodel.SomeInteger()) |
| 611 |
s = a.build_types(snippet.simple_iter, [t]) |
| 612 |
assert isinstance(s, annmodel.SomeIterator) |
| 613 |
|
| 614 |
def test_simple_zip(self): |
| 615 |
a = self.RPythonAnnotator() |
| 616 |
x = somelist(annmodel.SomeInteger()) |
| 617 |
y = somelist(annmodel.SomeString()) |
| 618 |
s = a.build_types(snippet.simple_zip, [x,y]) |
| 619 |
assert s.knowntype == list |
| 620 |
assert listitem(s).knowntype == tuple |
| 621 |
assert listitem(s).items[0].knowntype == int |
| 622 |
assert listitem(s).items[1].knowntype == str |
| 623 |
|
| 624 |
def test_dict_copy(self): |
| 625 |
a = self.RPythonAnnotator() |
| 626 |
t = somedict(a, annmodel.SomeInteger(), annmodel.SomeInteger()) |
| 627 |
s = a.build_types(snippet.dict_copy, [t]) |
| 628 |
assert isinstance(dictkey(s), annmodel.SomeInteger) |
| 629 |
assert isinstance(dictvalue(s), annmodel.SomeInteger) |
| 630 |
|
| 631 |
def test_dict_update(self): |
| 632 |
a = self.RPythonAnnotator() |
| 633 |
s = a.build_types(snippet.dict_update, [int]) |
| 634 |
assert isinstance(dictkey(s), annmodel.SomeInteger) |
| 635 |
assert isinstance(dictvalue(s), annmodel.SomeInteger) |
| 636 |
|
| 637 |
def test_dict_update_2(self): |
| 638 |
a = self.RPythonAnnotator() |
| 639 |
def g(n): |
| 640 |
if n: |
| 641 |
return {3: 4} |
| 642 |
def f(n): |
| 643 |
g(0) |
| 644 |
d = {} |
| 645 |
d.update(g(n)) |
| 646 |
return d |
| 647 |
s = a.build_types(f, [int]) |
| 648 |
assert dictkey(s).knowntype == int |
| 649 |
|
| 650 |
def test_dict_keys(self): |
| 651 |
a = self.RPythonAnnotator() |
| 652 |
s = a.build_types(snippet.dict_keys, []) |
| 653 |
assert isinstance(listitem(s), annmodel.SomeString) |
| 654 |
|
| 655 |
def test_dict_keys2(self): |
| 656 |
a = self.RPythonAnnotator() |
| 657 |
s = a.build_types(snippet.dict_keys2, []) |
| 658 |
assert type(listitem(s)) is annmodel.SomeString |
| 659 |
|
| 660 |
def test_dict_values(self): |
| 661 |
a = self.RPythonAnnotator() |
| 662 |
s = a.build_types(snippet.dict_values, []) |
| 663 |
assert isinstance(listitem(s), annmodel.SomeString) |
| 664 |
|
| 665 |
def test_dict_values2(self): |
| 666 |
a = self.RPythonAnnotator() |
| 667 |
s = a.build_types(snippet.dict_values2, []) |
| 668 |
assert type(listitem(s)) is annmodel.SomeString |
| 669 |
|
| 670 |
def test_dict_items(self): |
| 671 |
a = self.RPythonAnnotator() |
| 672 |
s = a.build_types(snippet.dict_items, []) |
| 673 |
assert isinstance(listitem(s), annmodel.SomeTuple) |
| 674 |
s_key, s_value = listitem(s).items |
| 675 |
assert isinstance(s_key, annmodel.SomeString) |
| 676 |
assert isinstance(s_value, annmodel.SomeInteger) |
| 677 |
|
| 678 |
def test_dict_setdefault(self): |
| 679 |
a = self.RPythonAnnotator() |
| 680 |
def f(): |
| 681 |
d = {} |
| 682 |
d.setdefault('a', 2) |
| 683 |
d.setdefault('a', -3) |
| 684 |
return d |
| 685 |
s = a.build_types(f, []) |
| 686 |
assert isinstance(s, annmodel.SomeDict) |
| 687 |
assert isinstance(dictkey(s), annmodel.SomeString) |
| 688 |
assert isinstance(dictvalue(s), annmodel.SomeInteger) |
| 689 |
assert not dictvalue(s).nonneg |
| 690 |
|
| 691 |
def test_dict_get(self): |
| 692 |
def f1(i, j): |
| 693 |
d = {i: ''} |
| 694 |
return d.get(j) |
| 695 |
a = self.RPythonAnnotator() |
| 696 |
s = a.build_types(f1, [int, int]) |
| 697 |
assert isinstance(s, annmodel.SomeString) |
| 698 |
assert s.can_be_None |
| 699 |
|
| 700 |
|
| 701 |
def test_exception_deduction(self): |
| 702 |
a = self.RPythonAnnotator() |
| 703 |
s = a.build_types(snippet.exception_deduction, []) |
| 704 |
assert isinstance(s, annmodel.SomeInstance) |
| 705 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc) |
| 706 |
|
| 707 |
def test_exception_deduction_we_are_dumb(self): |
| 708 |
a = self.RPythonAnnotator() |
| 709 |
s = a.build_types(snippet.exception_deduction_we_are_dumb, []) |
| 710 |
assert isinstance(s, annmodel.SomeInstance) |
| 711 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc) |
| 712 |
|
| 713 |
def test_nested_exception_deduction(self): |
| 714 |
a = self.RPythonAnnotator() |
| 715 |
s = a.build_types(snippet.nested_exception_deduction, []) |
| 716 |
assert isinstance(s, annmodel.SomeTuple) |
| 717 |
assert isinstance(s.items[0], annmodel.SomeInstance) |
| 718 |
assert isinstance(s.items[1], annmodel.SomeInstance) |
| 719 |
assert s.items[0].classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc) |
| 720 |
assert s.items[1].classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc2) |
| 721 |
|
| 722 |
def test_exc_deduction_our_exc_plus_others(self): |
| 723 |
a = self.RPythonAnnotator() |
| 724 |
s = a.build_types(snippet.exc_deduction_our_exc_plus_others, []) |
| 725 |
assert isinstance(s, annmodel.SomeInteger) |
| 726 |
|
| 727 |
def test_exc_deduction_our_excs_plus_others(self): |
| 728 |
a = self.RPythonAnnotator() |
| 729 |
s = a.build_types(snippet.exc_deduction_our_excs_plus_others, []) |
| 730 |
assert isinstance(s, annmodel.SomeInteger) |
| 731 |
|
| 732 |
def test_complex_exception_deduction(self): |
| 733 |
class InternalError(Exception): |
| 734 |
def __init__(self, msg): |
| 735 |
self.msg = msg |
| 736 |
|
| 737 |
class AppError(Exception): |
| 738 |
def __init__(self, msg): |
| 739 |
self.msg = msg |
| 740 |
def apperror(msg): |
| 741 |
return AppError(msg) |
| 742 |
|
| 743 |
def f(string): |
| 744 |
if not string: |
| 745 |
raise InternalError('Empty string') |
| 746 |
return string, None |
| 747 |
def cleanup(): |
| 748 |
pass |
| 749 |
|
| 750 |
def g(string): |
| 751 |
try: |
| 752 |
try: |
| 753 |
string, _ = f(string) |
| 754 |
except ZeroDivisionError: |
| 755 |
raise apperror('ZeroDivisionError') |
| 756 |
try: |
| 757 |
result, _ = f(string) |
| 758 |
finally: |
| 759 |
cleanup() |
| 760 |
except InternalError as e: |
| 761 |
raise apperror(e.msg) |
| 762 |
return result |
| 763 |
|
| 764 |
a = self.RPythonAnnotator() |
| 765 |
s_result = a.build_types(g, [str]) |
| 766 |
assert isinstance(s_result, annmodel.SomeString) |
| 767 |
|
| 768 |
def test_method_exception_specialization(self): |
| 769 |
def f(l): |
| 770 |
try: |
| 771 |
return l.pop() |
| 772 |
except Exception: |
| 773 |
raise |
| 774 |
a = self.RPythonAnnotator() |
| 775 |
s = a.build_types(f, [[int]]) |
| 776 |
graph = graphof(a, f) |
| 777 |
etype, evalue = graph.exceptblock.inputargs |
| 778 |
assert evalue.annotation.classdefs == { |
| 779 |
a.bookkeeper.getuniqueclassdef(IndexError)} |
| 780 |
assert etype.annotation.const == IndexError |
| 781 |
|
| 782 |
def test_operation_always_raising(self): |
| 783 |
def operation_always_raising(n): |
| 784 |
lst = [] |
| 785 |
try: |
| 786 |
return lst[n] |
| 787 |
except IndexError: |
| 788 |
return 24 |
| 789 |
a = self.RPythonAnnotator() |
| 790 |
s = a.build_types(operation_always_raising, [int]) |
| 791 |
assert s == a.bookkeeper.immutablevalue(24) |
| 792 |
|
| 793 |
def test_propagation_of_fresh_instances_through_attrs(self): |
| 794 |
a = self.RPythonAnnotator() |
| 795 |
s = a.build_types(snippet.propagation_of_fresh_instances_through_attrs, [int]) |
| 796 |
assert s is not None |
| 797 |
|
| 798 |
def test_propagation_of_fresh_instances_through_attrs_rec_0(self): |
| 799 |
a = self.RPythonAnnotator() |
| 800 |
s = a.build_types(snippet.make_r, [int]) |
| 801 |
Rdef = a.bookkeeper.getuniqueclassdef(snippet.R) |
| 802 |
assert s.classdef == Rdef |
| 803 |
assert Rdef.attrs['r'].s_value.classdef == Rdef |
| 804 |
assert Rdef.attrs['n'].s_value.knowntype == int |
| 805 |
assert Rdef.attrs['m'].s_value.knowntype == int |
| 806 |
|
| 807 |
|
| 808 |
def test_propagation_of_fresh_instances_through_attrs_rec_eo(self): |
| 809 |
a = self.RPythonAnnotator() |
| 810 |
s = a.build_types(snippet.make_eo, [int]) |
| 811 |
assert s.classdef == a.bookkeeper.getuniqueclassdef(snippet.B) |
| 812 |
Even_def = a.bookkeeper.getuniqueclassdef(snippet.Even) |
| 813 |
Odd_def = a.bookkeeper.getuniqueclassdef(snippet.Odd) |
| 814 |
assert listitem(Even_def.attrs['x'].s_value).classdef == Odd_def |
| 815 |
assert listitem(Even_def.attrs['y'].s_value).classdef == Even_def |
| 816 |
assert listitem(Odd_def.attrs['x'].s_value).classdef == Even_def |
| 817 |
assert listitem(Odd_def.attrs['y'].s_value).classdef == Odd_def |
| 818 |
|
| 819 |
def test_flow_rev_numbers(self): |
| 820 |
a = self.RPythonAnnotator() |
| 821 |
s = a.build_types(snippet.flow_rev_numbers, [int]) |
| 822 |
assert s.knowntype == int |
| 823 |
assert not s.is_constant() |
| 824 |
|
| 825 |
def test_methodcall_is_precise(self): |
| 826 |
a = self.RPythonAnnotator() |
| 827 |
s = a.build_types(snippet.methodcall_is_precise, [bool]) |
| 828 |
getcdef = a.bookkeeper.getuniqueclassdef |
| 829 |
assert 'x' not in getcdef(snippet.CBase).attrs |
| 830 |
assert (getcdef(snippet.CSub1).attrs['x'].s_value == |
| 831 |
a.bookkeeper.immutablevalue(42)) |
| 832 |
assert (getcdef(snippet.CSub2).attrs['x'].s_value == |
| 833 |
a.bookkeeper.immutablevalue('world')) |
| 834 |
assert s == a.bookkeeper.immutablevalue(42) |
| 835 |
|
| 836 |
def test_call_star_args(self): |
| 837 |
a = self.RPythonAnnotator(policy=AnnotatorPolicy()) |
| 838 |
s = a.build_types(snippet.call_star_args, [int]) |
| 839 |
assert s.knowntype == int |
| 840 |
|
| 841 |
def test_call_star_args_multiple(self): |
| 842 |
a = self.RPythonAnnotator(policy=AnnotatorPolicy()) |
| 843 |
s = a.build_types(snippet.call_star_args_multiple, [int]) |
| 844 |
assert s.knowntype == int |
| 845 |
|
| 846 |
def test_exception_deduction_with_raise1(self): |
| 847 |
a = self.RPythonAnnotator() |
| 848 |
s = a.build_types(snippet.exception_deduction_with_raise1, [bool]) |
| 849 |
assert isinstance(s, annmodel.SomeInstance) |
| 850 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc) |
| 851 |
|
| 852 |
|
| 853 |
def test_exception_deduction_with_raise2(self): |
| 854 |
a = self.RPythonAnnotator() |
| 855 |
s = a.build_types(snippet.exception_deduction_with_raise2, [bool]) |
| 856 |
assert isinstance(s, annmodel.SomeInstance) |
| 857 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc) |
| 858 |
|
| 859 |
def test_exception_deduction_with_raise3(self): |
| 860 |
a = self.RPythonAnnotator() |
| 861 |
s = a.build_types(snippet.exception_deduction_with_raise3, [bool]) |
| 862 |
assert isinstance(s, annmodel.SomeInstance) |
| 863 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(snippet.Exc) |
| 864 |
|
| 865 |
def test_type_is(self): |
| 866 |
class B(object): |
| 867 |
pass |
| 868 |
class C(B): |
| 869 |
pass |
| 870 |
def f(x): |
| 871 |
assert type(x) is C |
| 872 |
return x |
| 873 |
a = self.RPythonAnnotator() |
| 874 |
s = a.build_types(f, [B]) |
| 875 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(C) |
| 876 |
|
| 877 |
@py.test.mark.xfail |
| 878 |
def test_union_type_some_pbc(self): |
| 879 |
class A(object): |
| 880 |
name = "A" |
| 881 |
|
| 882 |
def f(self): |
| 883 |
return type(self) |
| 884 |
|
| 885 |
class B(A): |
| 886 |
name = "B" |
| 887 |
|
| 888 |
def f(tp): |
| 889 |
return tp |
| 890 |
|
| 891 |
def main(n): |
| 892 |
if n: |
| 893 |
if n == 1: |
| 894 |
inst = A() |
| 895 |
else: |
| 896 |
inst = B() |
| 897 |
arg = inst.f() |
| 898 |
else: |
| 899 |
arg = B |
| 900 |
return f(arg).name |
| 901 |
|
| 902 |
a = self.RPythonAnnotator() |
| 903 |
s = a.build_types(main, [int]) |
| 904 |
assert isinstance(s, annmodel.SomeString) |
| 905 |
|
| 906 |
def test_ann_assert(self): |
| 907 |
def assert_(x): |
| 908 |
assert x,"XXX" |
| 909 |
a = self.RPythonAnnotator() |
| 910 |
s = a.build_types(assert_, [int]) |
| 911 |
assert s.const is None |
| 912 |
|
| 913 |
def test_string_and_none(self): |
| 914 |
def f(n): |
| 915 |
if n: |
| 916 |
return 'y' |
| 917 |
else: |
| 918 |
return 'n' |
| 919 |
def g(n): |
| 920 |
if n: |
| 921 |
return 'y' |
| 922 |
else: |
| 923 |
return None |
| 924 |
a = self.RPythonAnnotator() |
| 925 |
s = a.build_types(f, [bool]) |
| 926 |
assert s.knowntype == str |
| 927 |
assert not s.can_be_None |
| 928 |
s = a.build_types(g, [bool]) |
| 929 |
assert s.knowntype == str |
| 930 |
assert s.can_be_None |
| 931 |
|
| 932 |
def test_implicit_exc(self): |
| 933 |
def f(l): |
| 934 |
try: |
| 935 |
l[0] |
| 936 |
except (KeyError, IndexError) as e: |
| 937 |
return e |
| 938 |
return None |
| 939 |
|
| 940 |
a = self.RPythonAnnotator() |
| 941 |
s = a.build_types(f, [somelist(annmodel.s_Int)]) |
| 942 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(IndexError) |
| 943 |
|
| 944 |
def test_freeze_protocol(self): |
| 945 |
class Stuff: |
| 946 |
def __init__(self): |
| 947 |
self.called = False |
| 948 |
def _freeze_(self): |
| 949 |
self.called = True |
| 950 |
return True |
| 951 |
myobj = Stuff() |
| 952 |
a = self.RPythonAnnotator() |
| 953 |
s = a.build_types(lambda: myobj, []) |
| 954 |
assert myobj.called |
| 955 |
assert isinstance(s, annmodel.SomePBC) |
| 956 |
assert s.const == myobj |
| 957 |
|
| 958 |
def test_cleanup_protocol(self): |
| 959 |
class Stuff: |
| 960 |
def __init__(self): |
| 961 |
self.called = False |
| 962 |
def _cleanup_(self): |
| 963 |
self.called = True |
| 964 |
myobj = Stuff() |
| 965 |
a = self.RPythonAnnotator() |
| 966 |
s = a.build_types(lambda: myobj, []) |
| 967 |
assert myobj.called |
| 968 |
assert isinstance(s, annmodel.SomeInstance) |
| 969 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(Stuff) |
| 970 |
|
| 971 |
def test_circular_mutable_getattr(self): |
| 972 |
class C: |
| 973 |
pass |
| 974 |
c = C() |
| 975 |
c.x = c |
| 976 |
def f(): |
| 977 |
return c.x |
| 978 |
a = self.RPythonAnnotator() |
| 979 |
s = a.build_types(f, []) |
| 980 |
assert isinstance(s, annmodel.SomeInstance) |
| 981 |
assert s.classdef == a.bookkeeper.getuniqueclassdef(C) |
| 982 |
|
| 983 |
def test_circular_list_type(self): |
| 984 |
def f(n): |
| 985 |
lst = [] |
| 986 |
for i in range(n): |
| 987 |
lst = [lst] |
| 988 |
return lst |
| 989 |
a = self.RPythonAnnotator() |
| 990 |
s = a.build_types(f, [int]) |
| 991 |
assert listitem(s) == s |
| 992 |
|
| 993 |
def test_harmonic(self): |
| 994 |
a = self.RPythonAnnotator() |
| 995 |
s = a.build_types(snippet.harmonic, [int]) |
| 996 |
assert s.knowntype == float |
| 997 |
|
| 998 |
graph = graphof(a, snippet.harmonic) |
| 999 |
all_vars = set().union(*[block.getvariables() for block in graph.iterblocks()]) |
| 1000 |
print(all_vars) |
| 1001 |
for var in all_vars: |
| 1002 |
s_value = var.annotation |
| 1003 |
if isinstance(s_value, annmodel.SomeList): |
| 1004 |
assert not s_value.listdef.listitem.resized |
| 1005 |
assert not s_value.listdef.listitem.mutated |
| 1006 |
assert s_value.listdef.listitem.range_step |
| 1007 |
|
| 1008 |
def test_bool(self): |
| 1009 |
def f(a,b): |
| 1010 |
return bool(a) or bool(b) |
| 1011 |
a = self.RPythonAnnotator() |
| 1012 |
s = a.build_types(f, [int, somelist(annmodel.s_Int)]) |
| 1013 |
assert s.knowntype == bool |
| 1014 |
|
| 1015 |
def test_float(self): |
| 1016 |
def f(n): |
| 1017 |
return float(n) |
| 1018 |
a = self.RPythonAnnotator() |
| 1019 |
s = a.build_types(f, [int]) |
| 1020 |
assert s.knowntype == float |
| 1021 |
|
| 1022 |
def test_r_uint(self): |
| 1023 |
def f(n): |
| 1024 |
return n + constant_unsigned_five |
| 1025 |
a = self.RPythonAnnotator() |
| 1026 |
s = a.build_types(f, [r_uint]) |
| 1027 |
assert s == annmodel.SomeInteger(nonneg = True, unsigned = True) |
| 1028 |
|
| 1029 |
def test_large_unsigned(self): |
| 1030 |
large_constant = sys.maxint * 2 + 1 |
| 1031 |
def f(): |
| 1032 |
return large_constant |
| 1033 |
a = self.RPythonAnnotator() |
| 1034 |
with py.test.raises(ValueError): |
| 1035 |
a.build_types(f, []) |
| 1036 |
|
| 1037 |
|
| 1038 |
def test_add_different_ints(self): |
| 1039 |
def f(a, b): |
| 1040 |
return a + b |
| 1041 |
a = self.RPythonAnnotator() |
| 1042 |
with py.test.raises(UnionError): |
| 1043 |
a.build_types(f, [r_uint, int]) |
| 1044 |
|
| 1045 |
def test_merge_different_ints(self): |
| 1046 |
def f(a, b): |
| 1047 |
if a: |
| 1048 |
c = a |
| 1049 |
else: |
| 1050 |
c = b |
| 1051 |
return c |
| 1052 |
a = self.RPythonAnnotator() |
| 1053 |
with py.test.raises(UnionError): |
| 1054 |
a.build_types(f, [r_uint, int]) |
| 1055 |
|
| 1056 |
def test_merge_ruint_zero(self): |
| 1057 |
def f(a): |
| 1058 |
if a: |
| 1059 |
c = a |
| 1060 |
else: |
| 1061 |
c = 0 |
| 1062 |
return c |
| 1063 |
a = self.RPythonAnnotator() |
| 1064 |
s = a.build_types(f, [r_uint]) |
| 1065 |
assert s == annmodel.SomeInteger(nonneg = True, unsigned = True) |
| 1066 |
|
| 1067 |
def test_merge_ruint_nonneg_signed(self): |
| 1068 |
def f(a, b): |
| 1069 |
if a: |
| 1070 |
c = a |
| 1071 |
else: |
| 1072 |
assert b >= 0 |
| 1073 |
c = b |
| 1074 |
return c |
| 1075 |
a = self.RPythonAnnotator() |
| 1076 |
s = a.build_types(f, [r_uint, int]) |
| 1077 |
assert s == annmodel.SomeInteger(nonneg = True, unsigned = True) |
| 1078 |
|
| 1079 |
|
| 1080 |
def test_prebuilt_int_that_is_not_too_long(self): |
| 1081 |
small_constant = 12 |
| 1082 |
def f(): |
| 1083 |
return small_constant |
| 1084 |
a = self.RPythonAnnotator() |
| 1085 |
s = a.build_types(f, []) |
| 1086 |
assert s.const == 12 |
| 1087 |
assert s.nonneg |
| 1088 |
assert not s.unsigned |
| 1089 |
|
| 1090 |
small_constant = -23 |
| 1091 |
def f(): |
| 1092 |
return small_constant |
| 1093 |
a = self.RPythonAnnotator() |
| 1094 |
s = a.build_types(f, []) |
| 1095 |
assert s.const == -23 |
| 1096 |
assert not s.nonneg |
| 1097 |
assert not s.unsigned |
| 1098 |
|
| 1099 |
def test_pbc_getattr(self): |
| 1100 |
class C: |
| 1101 |
def __init__(self, v1, v2): |
| 1102 |
self.v2 = v2 |
| 1103 |
self.v1 = v1 |
| 1104 |
|
| 1105 |
def _freeze_(self): |
| 1106 |
return True |
| 1107 |
|
| 1108 |
c1 = C(1,'a') |
| 1109 |
c2 = C(2,'b') |
| 1110 |
c3 = C(3,'c') |
| 1111 |
|
| 1112 |
def f1(l, c): |
| 1113 |
l.append(c.v1) |
| 1114 |
def f2(l, c): |
| 1115 |
l.append(c.v2) |
| 1116 |
|
| 1117 |
def g(): |
| 1118 |
l1 = [] |
| 1119 |
l2 = [] |
| 1120 |
f1(l1, c1) |
| 1121 |
f1(l1, c2) |
| 1122 |
f2(l2, c2) |
| 1123 |
f2(l2, c3) |
| 1124 |
return l1,l2 |
| 1125 |
|
| 1126 |
a = self.RPythonAnnotator() |
| 1127 |
s = a.build_types(g,[]) |
| 1128 |
l1, l2 = s.items |
| 1129 |
assert listitem(l1).knowntype == int |
| 1130 |
assert listitem(l2).knowntype == str |
| 1131 |
|
| 1132 |
|
| 1133 |
acc1 = a.bookkeeper.getdesc(c1).getattrfamily() |
| 1134 |
acc2 = a.bookkeeper.getdesc(c2).getattrfamily() |
| 1135 |
acc3 = a.bookkeeper.getdesc(c3).getattrfamily() |
| 1136 |
|
| 1137 |
assert acc1 is acc2 is acc3 |
| 1138 |
|
| 1139 |
assert len(acc1.descs) == 3 |
| 1140 |
assert dict.fromkeys(acc1.attrs) == {'v1': None, 'v2': None} |
| 1141 |
|
| 1142 |
def test_single_pbc_getattr(self): |
| 1143 |
class C: |
| 1144 |
def __init__(self, v1, v2): |
| 1145 |
self.v1 = v1 |
| 1146 |
self.v2 = v2 |
| 1147 |
def _freeze_(self): |
| 1148 |
return True |
| 1149 |
c1 = C(11, "hello") |
| 1150 |
c2 = C(22, 623) |
| 1151 |
def f1(l, c): |
| 1152 |
l.append(c.v1) |
| 1153 |
def f2(c): |
| 1154 |
return c.v2 |
| 1155 |
def f3(c): |
| 1156 |
return c.v2 |
| 1157 |
def g(): |
| 1158 |
l = [] |
| 1159 |
f1(l, c1) |
| 1160 |
f1(l, c2) |
| 1161 |
return l, f2(c1), f3(c2) |
| 1162 |
|
| 1163 |
a = self.RPythonAnnotator() |
| 1164 |
s = a.build_types(g,[]) |
| 1165 |
s_l, s_c1v2, s_c2v2 = s.items |
| 1166 |
assert listitem(s_l).knowntype == int |
| 1167 |
assert s_c1v2.const == "hello" |
| 1168 |
assert s_c2v2.const == 623 |
| 1169 |
|
| 1170 |
acc1 = a.bookkeeper.getdesc(c1).getattrfamily() |
| 1171 |
acc2 = a.bookkeeper.getdesc(c2).getattrfamily() |
| 1172 |
assert acc1 is acc2 |
| 1173 |
assert acc1.attrs.keys() == ['v1'] |
| 1174 |
|
| 1175 |
def test_isinstance_unsigned_1(self): |
| 1176 |
def f(x): |
| 1177 |
return isinstance(x, r_uint) |
| 1178 |
def g(): |
| 1179 |
v = r_uint(1) |
| 1180 |
return f(v) |
| 1181 |
a = self.RPythonAnnotator() |
| 1182 |
s = a.build_types(g, []) |
| 1183 |
assert s.const == True |
| 1184 |
|
| 1185 |
def test_isinstance_unsigned_2(self): |
| 1186 |
class Foo: |
| 1187 |
pass |
| 1188 |
def f(x): |
| 1189 |
return isinstance(x, r_uint) |
| 1190 |
def g(): |
| 1191 |
v = Foo() |
| 1192 |
return f(v) |
| 1193 |
a = self.RPythonAnnotator() |
| 1194 |
s = a.build_types(g, []) |
| 1195 |
assert s.const == False |
| 1196 |
|
| 1197 |
def test_isinstance_base_int(self): |
| 1198 |
def f(x): |
| 1199 |
return isinstance(x, base_int) |
| 1200 |
def g(n): |
| 1201 |
v = r_uint(n) |
| 1202 |
return f(v) |
| 1203 |
a = self.RPythonAnnotator() |
| 1204 |
s = a.build_types(g, [int]) |
| 1205 |
assert s.const == True |
| 1206 |
|
| 1207 |
def test_isinstance_basic(self): |
| 1208 |
def f(): |
| 1209 |
return isinstance(IndexError(), type) |
| 1210 |
a = self.RPythonAnnotator() |
| 1211 |
s = a.build_types(f, []) |
| 1212 |
assert s.const == False |
| 1213 |
|
| 1214 |
def test_alloc_like(self): |
| 1215 |
class Base(object): |
| 1216 |
pass |
| 1217 |
class C1(Base): |
| 1218 |
pass |
| 1219 |
class C2(Base): |
| 1220 |
pass |
| 1221 |
|
| 1222 |
def inst(cls): |
| 1223 |
return cls() |
| 1224 |
|
| 1225 |
def alloc(cls): |
| 1226 |
i = inst(cls) |
| 1227 |
assert isinstance(i, cls) |
| 1228 |
return i |
| 1229 |
alloc._annspecialcase_ = "specialize:arg(0)" |
| 1230 |
|
| 1231 |
def f(): |
| 1232 |
c1 = alloc(C1) |
| 1233 |
c2 = alloc(C2) |
| 1234 |
return c1,c2 |
| 1235 |
|
| 1236 |
a = self.RPythonAnnotator() |
| 1237 |
s = a.build_types(f, []) |
| 1238 |
C1df = a.bookkeeper.getuniqueclassdef(C1) |
| 1239 |
C2df = a.bookkeeper.getuniqueclassdef(C2) |
| 1240 |
|
| 1241 |
assert s.items[0].classdef == C1df |
| 1242 |
assert s.items[1].classdef == C2df |
| 1243 |
|
| 1244 |
allocdesc = a.bookkeeper.getdesc(alloc) |
| 1245 |
s_C1 = a.bookkeeper.immutablevalue(C1) |
| 1246 |
s_C2 = a.bookkeeper.immutablevalue(C2) |
| 1247 |
graph1 = allocdesc.specialize([s_C1], None) |
| 1248 |
graph2 = allocdesc.specialize([s_C2], None) |
| 1249 |
assert a.binding(graph1.getreturnvar()).classdef == C1df |
| 1250 |
assert a.binding(graph2.getreturnvar()).classdef == C2df |
| 1251 |
assert graph1 in a.translator.graphs |
| 1252 |
assert graph2 in a.translator.graphs |
| 1253 |
|
| 1254 |
def test_specialcase_args(self): |
| 1255 |
class C1(object): |
| 1256 |
pass |
| 1257 |
|
| 1258 |
class C2(object): |
| 1259 |
pass |
| 1260 |
|
| 1261 |
def alloc(cls, cls2): |
| 1262 |
i = cls() |
| 1263 |
assert isinstance(i, cls) |
| 1264 |
j = cls2() |
| 1265 |
assert isinstance(j, cls2) |
| 1266 |
return i |
| 1267 |
|
| 1268 |
def f(): |
| 1269 |
alloc(C1, C1) |
| 1270 |
alloc(C1, C2) |
| 1271 |
alloc(C2, C1) |
| 1272 |
alloc(C2, C2) |
| 1273 |
|
| 1274 |
alloc._annspecialcase_ = "specialize:arg(0,1)" |
| 1275 |
|
| 1276 |
a = self.RPythonAnnotator() |
| 1277 |
C1df = a.bookkeeper.getuniqueclassdef(C1) |
| 1278 |
C2df = a.bookkeeper.getuniqueclassdef(C2) |
| 1279 |
s = a.build_types(f, []) |
| 1280 |
allocdesc = a.bookkeeper.getdesc(alloc) |
| 1281 |
s_C1 = a.bookkeeper.immutablevalue(C1) |
| 1282 |
s_C2 = a.bookkeeper.immutablevalue(C2) |
| 1283 |
graph1 = allocdesc.specialize([s_C1, s_C2], None) |
| 1284 |
graph2 = allocdesc.specialize([s_C2, s_C2], None) |
| 1285 |
assert a.binding(graph1.getreturnvar()).classdef == C1df |
| 1286 |
assert a.binding(graph2.getreturnvar()).classdef == C2df |
| 1287 |
assert graph1 in a.translator.graphs |
| 1288 |
assert graph2 in a.translator.graphs |
| 1289 |
|
| 1290 |
def test_specialize_arg_bound_method(self): |
| 1291 |
class GC(object): |
| 1292 |
def trace(self, callback, *args): |
| 1293 |
return callback(*args) |
| 1294 |
trace._annspecialcase_ = "specialize:arg(1)" |
| 1295 |
|
| 1296 |
def callback1(self, arg1): |
| 1297 |
self.x = arg1 |
| 1298 |
return "hello" |
| 1299 |
|
| 1300 |
def callback2(self, arg2, arg3): |
| 1301 |
self.y = arg2 |
| 1302 |
self.z = arg3 |
| 1303 |
return 6 |
| 1304 |
|
| 1305 |
def f(): |
| 1306 |
gc = GC() |
| 1307 |
s1 = gc.trace(gc.callback1, "foo") |
| 1308 |
n2 = gc.trace(gc.callback2, 7, 2) |
| 1309 |
return (s1, n2, gc.x, gc.y, gc.z) |
| 1310 |
a = self.RPythonAnnotator() |
| 1311 |
s = a.build_types(f, []) |
| 1312 |
assert s.items[0].const == "hello" |
| 1313 |
assert s.items[1].const == 6 |
| 1314 |
assert s.items[2].const == "foo" |
| 1315 |
assert s.items[3].const == 7 |
| 1316 |
assert s.items[4].const == 2 |
| 1317 |
|
| 1318 |
def test_specialize_and_star_args(self): |
| 1319 |
class I(object): |
| 1320 |
def execute(self, op, *args): |
| 1321 |
if op == 0: |
| 1322 |
return args[0]+args[1] |
| 1323 |
if op == 1: |
| 1324 |
return args[0] * args[1] + args[2] |
| 1325 |
execute._annspecialcase_ = "specialize:arg(1)" |
| 1326 |
|
| 1327 |
def f(x, y): |
| 1328 |
i = I() |
| 1329 |
a = i.execute(0, x, y) |
| 1330 |
b = i.execute(1, y, y, 5) |
| 1331 |
return a+b |
| 1332 |
|
| 1333 |
a = self.RPythonAnnotator() |
| 1334 |
s = a.build_types(f, [int, int]) |
| 1335 |
|
| 1336 |
executedesc = a.bookkeeper.getdesc(I.execute.im_func) |
| 1337 |
assert len(executedesc._cache) == 2 |
| 1338 |
|
| 1339 |
assert len(executedesc._cache[(0, 'star', 2)].startblock.inputargs) == 4 |
| 1340 |
assert len(executedesc._cache[(1, 'star', 3)].startblock.inputargs) == 5 |
| 1341 |
|
| 1342 |
def test_specialize_arg_or_var(self): |
| 1343 |
def f(a): |
| 1344 |
return 1 |
| 1345 |
f._annspecialcase_ = 'specialize:arg_or_var(0)' |
| 1346 |
|
| 1347 |
def fn(a): |
| 1348 |
return f(3) + f(a) |
| 1349 |
|
| 1350 |
a = self.RPythonAnnotator() |
| 1351 |
a.build_types(fn, [int]) |
| 1352 |
executedesc = a.bookkeeper.getdesc(f) |
| 1353 |
assert sorted(executedesc._cache.keys()) == [None, (3,)] |
| 1354 |
|
| 1355 |
|
| 1356 |
def test_specialize_call_location(self): |
| 1357 |
def g(a): |
| 1358 |
return a |
| 1359 |
g._annspecialcase_ = "specialize:call_location" |
| 1360 |
def f(x): |
| 1361 |
return g(x) |
| 1362 |
f._annspecialcase_ = "specialize:argtype(0)" |
| 1363 |
def h(y): |
| 1364 |
w = f(y) |
| 1365 |
return int(f(str(y))) + w |
| 1366 |
a = self.RPythonAnnotator() |
| 1367 |
assert a.build_types(h, [int]) == annmodel.SomeInteger() |
| 1368 |
|
| 1369 |
def test_assert_list_doesnt_lose_info(self): |
| 1370 |
class T(object): |
| 1371 |
pass |
| 1372 |
def g(l): |
| 1373 |
assert isinstance(l, list) |
| 1374 |
return l |
| 1375 |
def f(): |
| 1376 |
l = [T()] |
| 1377 |
return g(l) |
| 1378 |
a = self.RPythonAnnotator() |
| 1379 |
s = a.build_types(f, []) |
| 1380 |
s_item = listitem(s) |
| 1381 |
assert isinstance(s_item, annmodel.SomeInstance) |
| 1382 |
assert s_item.classdef is a.bookkeeper.getuniqueclassdef(T) |
| 1383 |
|
| 1384 |
def test_int_str_mul(self): |
| 1385 |
def f(x,a,b): |
| 1386 |
return a*x+x*b |
| 1387 |
a = self.RPythonAnnotator() |
| 1388 |
s = a.build_types(f, [str,int,int]) |
| 1389 |
assert s.knowntype == str |
| 1390 |
|
| 1391 |
def test_list_tuple(self): |
| 1392 |
def g0(x): |
| 1393 |
return list(x) |
| 1394 |
def g1(x): |
| 1395 |
return list(x) |
| 1396 |
def f(n): |
| 1397 |
l1 = g0(()) |
| 1398 |
l2 = g1((1,)) |
| 1399 |
if n: |
| 1400 |
t = (1,) |
| 1401 |
else: |
| 1402 |
t = (2,) |
| 1403 |
l3 = g1(t) |
| 1404 |
return l1, l2, l3 |
| 1405 |
a = self.RPythonAnnotator() |
| 1406 |
s = a.build_types(f, [bool]) |
| 1407 |
assert listitem(s.items[0]) == annmodel.SomeImpossibleValue() |
| 1408 |
assert listitem(s.items[1]).knowntype == int |
| 1409 |
assert listitem(s.items[2]).knowntype == int |
| 1410 |
|
| 1411 |
def test_empty_list(self): |
| 1412 |
def f(): |
| 1413 |
l = [] |
| 1414 |
return bool(l) |
| 1415 |
def g(): |
| 1416 |
l = [] |
| 1417 |
x = bool(l) |
| 1418 |
l.append(1) |
| 1419 |
return x, bool(l) |
| 1420 |
|
| 1421 |
a = self.RPythonAnnotator() |
| 1422 |
s = a.build_types(f, []) |
| 1423 |
assert s.const == False |
| 1424 |
|
| 1425 |
a = self.RPythonAnnotator() |
| 1426 |
s = a.build_types(g, []) |
| 1427 |
|
| 1428 |
assert s.items[0].knowntype == bool and not s.items[0].is_constant() |
| 1429 |
assert s.items[1].knowntype == bool and not s.items[1].is_constant() |
| 1430 |
|
| 1431 |
def test_empty_dict(self): |
| 1432 |
def f(): |
| 1433 |
d = {} |
| 1434 |
return bool(d) |
| 1435 |
def g(): |
| 1436 |
d = {} |
| 1437 |
x = bool(d) |
| 1438 |
d['a'] = 1 |
| 1439 |
return x, bool(d) |
| 1440 |
|
| 1441 |
a = self.RPythonAnnotator() |
| 1442 |
s = a.build_types(f, []) |
| 1443 |
assert s.const == False |
| 1444 |
|
| 1445 |
a = self.RPythonAnnotator() |
| 1446 |
s = a.build_types(g, []) |
| 1447 |
|
| 1448 |
assert s.items[0].knowntype == bool and not s.items[0].is_constant() |
| 1449 |
assert s.items[1].knowntype == bool and not s.items[1].is_constant() |
| 1450 |
|
| 1451 |
def test_call_two_funcs_but_one_can_only_raise(self): |
| 1452 |
a = self.RPythonAnnotator() |
| 1453 |
s = a.build_types(snippet.call_two_funcs_but_one_can_only_raise, |
| 1454 |
[int]) |
| 1455 |
assert s == a.bookkeeper.immutablevalue(None) |
| 1456 |
|
| 1457 |
def test_reraiseKeyError(self): |
| 1458 |
def f(dic): |
| 1459 |
try: |
| 1460 |
dic[5] |
| 1461 |
except KeyError: |
| 1462 |
raise |
| 1463 |
a = self.RPythonAnnotator() |
| 1464 |
a.build_types(f, [somedict(a, annmodel.s_Int, annmodel.s_Int)]) |
| 1465 |
fg = graphof(a, f) |
| 1466 |
et, ev = fg.exceptblock.inputargs |
| 1467 |
t = annmodel.SomeTypeOf([ev]) |
| 1468 |
t.const = KeyError |
| 1469 |
assert et.annotation == t |
| 1470 |
s_ev = ev.annotation |
| 1471 |
assert s_ev == a.bookkeeper.new_exception([KeyError]) |
| 1472 |
|
| 1473 |
def test_reraiseAnything(self): |
| 1474 |
def f(dic): |
| 1475 |
try: |
| 1476 |
dic[5] |
| 1477 |
except: |
| 1478 |
raise |
| 1479 |
a = self.RPythonAnnotator() |
| 1480 |
a.build_types(f, [somedict(a, annmodel.s_Int, annmodel.s_Int)]) |
| 1481 |
fg = graphof(a, f) |
| 1482 |
et, ev = fg.exceptblock.inputargs |
| 1483 |
t = annmodel.SomeTypeOf([ev]) |
| 1484 |
t.const = KeyError |
| 1485 |
assert et.annotation == t |
| 1486 |
s_ev = ev.annotation |
| 1487 |
assert s_ev == a.bookkeeper.new_exception([KeyError]) |
| 1488 |
|
| 1489 |
def test_exception_mixing(self): |
| 1490 |
def h(): |
| 1491 |
pass |
| 1492 |
|
| 1493 |
def g(): |
| 1494 |
pass |
| 1495 |
|
| 1496 |
class X(Exception): |
| 1497 |
def __init__(self, x=0): |
| 1498 |
self.x = x |
| 1499 |
|
| 1500 |
def f(a, l): |
| 1501 |
if a==1: |
| 1502 |
raise X |
| 1503 |
elif a==2: |
| 1504 |
raise X(1) |
| 1505 |
elif a==3: |
| 1506 |
raise X(4) |
| 1507 |
else: |
| 1508 |
try: |
| 1509 |
l[0] |
| 1510 |
x,y = l |
| 1511 |
g() |
| 1512 |
finally: |
| 1513 |
h() |
| 1514 |
a = self.RPythonAnnotator() |
| 1515 |
a.build_types(f, [int, somelist(annmodel.s_Int)]) |
| 1516 |
fg = graphof(a, f) |
| 1517 |
et, ev = fg.exceptblock.inputargs |
| 1518 |
t = annmodel.SomeTypeOf([ev]) |
| 1519 |
assert et.annotation == t |
| 1520 |
s_ev = ev.annotation |
| 1521 |
assert (isinstance(s_ev, annmodel.SomeInstance) and |
| 1522 |
s_ev.classdef == a.bookkeeper.getuniqueclassdef(Exception)) |
| 1523 |
|
| 1524 |
def test_try_except_raise_finally1(self): |
| 1525 |
def h(): pass |
| 1526 |
def g(): pass |
| 1527 |
class X(Exception): pass |
| 1528 |
def f(): |
| 1529 |
try: |
| 1530 |
try: |
| 1531 |
g() |
| 1532 |
except X: |
| 1533 |
h() |
| 1534 |
raise |
| 1535 |
finally: |
| 1536 |
h() |
| 1537 |
a = self.RPythonAnnotator() |
| 1538 |
a.build_types(f, []) |
| 1539 |
fg = graphof(a, f) |
| 1540 |
et, ev = fg.exceptblock.inputargs |
| 1541 |
t = annmodel.SomeTypeOf([ev]) |
| 1542 |
assert et.annotation == t |
| 1543 |
s_ev = ev.annotation |
| 1544 |
assert (isinstance(s_ev, annmodel.SomeInstance) and |
| 1545 |
s_ev.classdef == a.bookkeeper.getuniqueclassdef(Exception)) |
| 1546 |
|
| 1547 |
def test_inplace_div(self): |
| 1548 |
def f(n): |
| 1549 |
n /= 2 |
| 1550 |
return n / 2 |
| 1551 |
a = self.RPythonAnnotator() |
| 1552 |
s = a.build_types(f, [int]) |
| 1553 |
assert s.knowntype == int |
| 1554 |
|
| 1555 |
def test_prime(self): |
| 1556 |
a = self.RPythonAnnotator() |
| 1557 |
s = a.build_types(snippet.prime, [int]) |
| 1558 |
assert s.knowntype == bool |
| 1559 |
|
| 1560 |
def test_and_bool_coalesce(self): |
| 1561 |
def f(a,b,c,d,e): |
| 1562 |
x = a and b |
| 1563 |
if x: |
| 1564 |
return d,c |
| 1565 |
return e,c |
| 1566 |
a = self.RPythonAnnotator() |
| 1567 |
s = a.build_types(f, [int, str, a.bookkeeper.immutablevalue(1.0), a.bookkeeper.immutablevalue('d'), a.bookkeeper.immutablevalue('e')]) |
| 1568 |
assert s == annmodel.SomeTuple([annmodel.SomeChar(), a.bookkeeper.immutablevalue(1.0)]) |
| 1569 |
|
| 1570 |
def test_bool_coalesce2(self): |
| 1571 |
def f(a,b,a1,b1,c,d,e): |
| 1572 |
x = (a or b) and (a1 or b1) |
| 1573 |
if x: |
| 1574 |
return d,c |
| 1575 |
return e,c |
| 1576 |
a = self.RPythonAnnotator() |
| 1577 |
s = a.build_types(f, [int, str, float, somelist(annmodel.s_Int), |
| 1578 |
a.bookkeeper.immutablevalue(1.0), |
| 1579 |
a.bookkeeper.immutablevalue('d'), |
| 1580 |
a.bookkeeper.immutablevalue('e')]) |
| 1581 |
assert s == annmodel.SomeTuple([annmodel.SomeChar(), |
| 1582 |
a.bookkeeper.immutablevalue(1.0)]) |
| 1583 |
|
| 1584 |
def test_bool_coalesce_sanity(self): |
| 1585 |
def f(a): |
| 1586 |
while a: |
| 1587 |
pass |
| 1588 |
a = self.RPythonAnnotator() |
| 1589 |
s = a.build_types(f, [int]) |
| 1590 |
assert s == a.bookkeeper.immutablevalue(None) |
| 1591 |
|
| 1592 |
def test_non_None_path(self): |
| 1593 |
class C: |
| 1594 |
pass |
| 1595 |
def g(c): |
| 1596 |
if c is None: |
| 1597 |
return C() |
| 1598 |
return c |
| 1599 |
def f(x): |
| 1600 |
if x: |
| 1601 |
c = None |
| 1602 |
else: |
| 1603 |
c = C() |
| 1604 |
return g(c) |
| 1605 |
a = self.RPythonAnnotator() |
| 1606 |
s = a.build_types(f, [bool]) |
| 1607 |
assert s.can_be_none() == False |
| 1608 |
|
| 1609 |
def test_can_be_None_path(self): |
| 1610 |
class C: |
| 1611 |
pass |
| 1612 |
def f(x): |
| 1613 |
if x: |
| 1614 |
c = None |
| 1615 |
else: |
| 1616 |
c = C() |
| 1617 |
return isinstance(c, C) |
| 1618 |
a = self.RPythonAnnotator() |
| 1619 |
s = a.build_types(f, [bool]) |
| 1620 |
assert not s.is_constant() |
| 1621 |
|
| 1622 |
def test_nonneg_cleverness(self): |
| 1623 |
def f(a, b, c, d, e, f, g, h): |
| 1624 |
if a < 0: a = 0 |
| 1625 |
if b <= 0: b = 0 |
| 1626 |
if c >= 0: |
| 1627 |
pass |
| 1628 |
else: |
| 1629 |
c = 0 |
| 1630 |
if d < a: d = a |
| 1631 |
if e <= b: e = 1 |
| 1632 |
if c > f: f = 2 |
| 1633 |
if d >= g: g = 3 |
| 1634 |
if h != a: h = 0 |
| 1635 |
return a, b, c, d, e, f, g, h |
| 1636 |
a = self.RPythonAnnotator() |
| 1637 |
s = a.build_types(f, [int]*8) |
| 1638 |
assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True)] * 8) |
| 1639 |
|
| 1640 |
def test_general_nonneg_cleverness(self): |
| 1641 |
def f(a, b, c, d, e, f, g, h): |
| 1642 |
if a < 0: a = 0 |
| 1643 |
if b <= 0: b = 0 |
| 1644 |
if c >= 0: |
| 1645 |
pass |
| 1646 |
else: |
| 1647 |
c = 0 |
| 1648 |
if d < a: d = a |
| 1649 |
if e <= b: e = 1 |
| 1650 |
if c > f: f = 2 |
| 1651 |
if d >= g: g = 3 |
| 1652 |
if h != a: h = 0 |
| 1653 |
return a, b, c, d, e, f, g, h |
| 1654 |
a = self.RPythonAnnotator() |
| 1655 |
s = a.build_types(f, [r_longlong]*8) |
| 1656 |
assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True, knowntype=r_longlong)] * 8) |
| 1657 |
|
| 1658 |
|
| 1659 |
def test_more_nonneg_cleverness(self): |
| 1660 |
def f(start, stop): |
| 1661 |
assert 0 <= start <= stop |
| 1662 |
return start, stop |
| 1663 |
a = self.RPythonAnnotator() |
| 1664 |
s = a.build_types(f, [int, int]) |
| 1665 |
assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True)] * 2) |
| 1666 |
|
| 1667 |
def test_more_general_nonneg_cleverness(self): |
| 1668 |
def f(start, stop): |
| 1669 |
assert 0 <= start <= stop |
| 1670 |
return start, stop |
| 1671 |
a = self.RPythonAnnotator() |
| 1672 |
s = a.build_types(f, [r_longlong, r_longlong]) |
| 1673 |
assert s == annmodel.SomeTuple([annmodel.SomeInteger(nonneg=True, knowntype=r_longlong)] * 2) |
| 1674 |
|
| 1675 |
def test_nonneg_cleverness_is_gentle_with_unsigned(self): |
| 1676 |
def witness1(x): |
| 1677 |
pass |
| 1678 |
def witness2(x): |
| 1679 |
pass |
| 1680 |
def f(x): |
| 1681 |
if 0 < x: |
| 1682 |
witness1(x) |
| 1683 |
if x > 0: |
| 1684 |
witness2(x) |
| 1685 |
a = self.RPythonAnnotator() |
| 1686 |
s = a.build_types(f, [annmodel.SomeInteger(unsigned=True)]) |
| 1687 |
wg1 = graphof(a, witness1) |
| 1688 |
wg2 = graphof(a, witness2) |
| 1689 |
assert a.binding(wg1.getargs()[0]).unsigned is True |
| 1690 |
assert a.binding(wg2.getargs()[0]).unsigned is True |
| 1691 |
|
| 1692 |
def test_general_nonneg_cleverness_is_gentle_with_unsigned(self): |
| 1693 |
def witness1(x): |
| 1694 |
pass |
| 1695 |
def witness2(x): |
| 1696 |
pass |
| 1697 |
def f(x): |
| 1698 |
if 0 < x: |
| 1699 |
witness1(x) |
| 1700 |
if x > 0: |
| 1701 |
witness2(x) |
| 1702 |
a = self.RPythonAnnotator() |
| 1703 |
s = a.build_types(f, [annmodel.SomeInteger(knowntype=r_ulonglong)]) |
| 1704 |
wg1 = graphof(a, witness1) |
| 1705 |
wg2 = graphof(a, witness2) |
| 1706 |
assert a.binding(wg1.getargs()[0]).knowntype is r_ulonglong |
| 1707 |
assert a.binding(wg2.getargs()[0]).knowntype is r_ulonglong |
| 1708 |
|
| 1709 |
def test_nonneg_cleverness_in_max(self): |
| 1710 |
def f(x): |
| 1711 |
return max(x, 0) + max(0, x) |
| 1712 |
a = self.RPythonAnnotator() |
| 1713 |
s = a.build_types(f, [int]) |
| 1714 |
assert s.nonneg |
| 1715 |
|
| 1716 |
def test_attr_moving_into_parent(self): |
| 1717 |
class A: pass |
| 1718 |
class B(A): pass |
| 1719 |
a1 = A() |
| 1720 |
b1 = B() |
| 1721 |
b1.stuff = a1 |
| 1722 |
a1.stuff = None |
| 1723 |
def f(): |
| 1724 |
return b1.stuff |
| 1725 |
a = self.RPythonAnnotator() |
| 1726 |
s = a.build_types(f, []) |
| 1727 |
assert isinstance(s, annmodel.SomeInstance) |
| 1728 |
assert not s.can_be_None |
| 1729 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(A) |
| 1730 |
|
| 1731 |
def test_class_attribute(self): |
| 1732 |
class A: |
| 1733 |
stuff = 42 |
| 1734 |
class B(A): |
| 1735 |
pass |
| 1736 |
def f(): |
| 1737 |
b = B() |
| 1738 |
return b.stuff |
| 1739 |
a = self.RPythonAnnotator() |
| 1740 |
s = a.build_types(f, []) |
| 1741 |
assert s == a.bookkeeper.immutablevalue(42) |
| 1742 |
|
| 1743 |
def test_attr_recursive_getvalue(self): |
| 1744 |
class A: pass |
| 1745 |
a2 = A() |
| 1746 |
a2.stuff = None |
| 1747 |
a1 = A() |
| 1748 |
a1.stuff = a2 |
| 1749 |
def f(): |
| 1750 |
return a1.stuff |
| 1751 |
a = self.RPythonAnnotator() |
| 1752 |
s = a.build_types(f, []) |
| 1753 |
assert isinstance(s, annmodel.SomeInstance) |
| 1754 |
assert s.can_be_None |
| 1755 |
assert s.classdef is a.bookkeeper.getuniqueclassdef(A) |
| 1756 |
|
| 1757 |
def test_long_list_recursive_getvalue(self): |
| 1758 |
class A: pass |
| 1759 |
lst = [] |
| 1760 |
for i in range(500): |
| 1761 |
a1 = A() |
| 1762 |
a1.stuff = lst |
| 1763 |
lst.append(a1) |
| 1764 |
def f(): |
| 1765 |
A().stuff = None |
| 1766 |
return (A().stuff, lst)[1] |
| 1767 |
a = self.RPythonAnnotator() |
| 1768 |
s = a.build_types(f, []) |
| 1769 |
assert isinstance(s, annmodel.SomeList) |
| 1770 |
s_item = s.listdef.listitem.s_value |
| 1771 |
assert isinstance(s_item, annmodel.SomeInstance) |
| 1772 |
|
| 1773 |
def test_immutable_dict(self): |
| 1774 |
d = {4: "hello", |
| 1775 |
5: "world"} |
| 1776 |
def f(n): |
| 1777 |
return d[n] |
| 1778 |
a = self.RPythonAnnotator() |
| 1779 |
s = a.build_types(f, [int]) |
| 1780 |
assert isinstance(s, annmodel.SomeString) |
| 1781 |
|
| 1782 |
def test_immutable_recursive_list(self): |
| 1783 |
l = [] |
| 1784 |
l.append(l) |
| 1785 |
def f(): |
| 1786 |
return l |
| 1787 |
a = self.RPythonAnnotator() |
| 1788 |
s = a.build_types(f, []) |
| 1789 |
assert isinstance(s, annmodel.SomeList) |
| 1790 |
s_item = s.listdef.listitem.s_value |
| 1791 |
assert isinstance(s_item, annmodel.SomeList) |
| 1792 |
assert s_item.listdef.same_as(s.listdef) |
| 1793 |
|
| 1794 |
def test_defaults_with_list_or_dict(self): |
| 1795 |
def fn1(a=[]): |
| 1796 |
return a |
| 1797 |
def fn2(a={}): |
| 1798 |
return a |
| 1799 |
def f(): |
| 1800 |
fn1() |
| 1801 |
fn2() |
| 1802 |
return fn1([6, 7]), fn2({2: 3, 4: 5}) |
| 1803 |
a = self.RPythonAnnotator() |
| 1804 |
s = a.build_types(f, []) |
| 1805 |
assert isinstance(s, annmodel.SomeTuple) |
| 1806 |
s1, s2 = s.items |
| 1807 |
assert not s1.is_constant() |
| 1808 |
assert not s2.is_constant() |
| 1809 |
assert isinstance(s1.listdef.listitem. s_value, annmodel.SomeInteger) |
| 1810 |
assert isinstance(s2.dictdef.dictkey. s_value, annmodel.SomeInteger) |
| 1811 |
assert isinstance(s2.dictdef.dictvalue.s_value, annmodel.SomeInteger) |
| 1812 |
|
| 1813 |
def test_pbc_union(self): |
| 1814 |
class A: |
| 1815 |
def meth(self): |
| 1816 |
return 12 |
| 1817 |
class B(A): |
| 1818 |
pass |
| 1819 |
class C(B): |
| 1820 |
pass |
| 1821 |
def f(i): |
| 1822 |
if i: |
| 1823 |
f(0) |
| 1824 |
x = B() |
| 1825 |
else: |
| 1826 |
x = C() |
| 1827 |
return x.meth() |
| 1828 |
a = self.RPythonAnnotator() |
| 1829 |
s = a.build_types(f, [int]) |
| 1830 |
assert s == a.bookkeeper.immutablevalue(12) |
| 1831 |
|
| 1832 |
def test_int(self): |
| 1833 |
def f(x, s): |
| 1834 |
return int(x) + int(s) + int(s, 16) |
| 1835 |
a = self.RPythonAnnotator() |
| 1836 |
s = a.build_types(f, [int, str]) |
| 1837 |
assert s.knowntype == int |
| 1838 |
|
| 1839 |
def test_int_nonneg(self): |
| 1840 |
def f(x, y): |
| 1841 |
assert x >= 0 |
| 1842 |
return int(x) + int(y == 3) |
| 1843 |
a = self.RPythonAnnotator() |
| 1844 |
s = a.build_types(f, [int, int]) |
| 1845 |
assert isinstance(s, annmodel.SomeInteger) |
| 1846 |
assert s.nonneg |
| 1847 |
|
| 1848 |
def test_listitem_merge_asymmetry_bug(self): |
| 1849 |
class K: |
| 1850 |
pass |
| 1851 |
def mutr(k, x, i): |
| 1852 |
k.l2 = [x] + k.l2 |
| 1853 |
|
| 1854 |
k.l2[i] = x |
| 1855 |
def witness(i): |
| 1856 |
pass |
| 1857 |
def trouble(k): |
| 1858 |
l = k.l1 + k.l2 |
| 1859 |
for i in range(len(l)): |
| 1860 |
witness(l[i]) |
| 1861 |
def f(flag, k, x, i): |
| 1862 |
if flag: |
| 1863 |
k = K() |
| 1864 |
k.l1 = [] |
| 1865 |
k.l2 = [] |
| 1866 |
trouble(k) |
| 1867 |
mutr(k, x, i) |
| 1868 |
a = self.RPythonAnnotator() |
| 1869 |
a.build_types(f, [bool, K, int, int]) |
| 1870 |
g = graphof(a, witness) |
| 1871 |
assert a.binding(g.getargs()[0]).knowntype == int |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
def test_isinstance_int_bool(self): |
| 1876 |
def f(x): |
| 1877 |
if isinstance(x, int): |
| 1878 |
if isinstance(x, bool): |
| 1879 |
return "bool" |
| 1880 |
return "int" |
| 1881 |
return "dontknow" |
| 1882 |
a = self.RPythonAnnotator() |
| 1883 |
s = a.build_types(f, [bool]) |
| 1884 |
assert s.const == "bool" |
| 1885 |
a = self.RPythonAnnotator() |
| 1886 |
s = a.build_types(f, [int]) |
| 1887 |
assert s.const == "int" |
| 1888 |
a = self.RPythonAnnotator() |
| 1889 |
s = a.build_types(f, [float]) |
| 1890 |
assert s.const == "dontknow" |
| 1891 |
|
| 1892 |
def test_hidden_method(self): |
| 1893 |
class Base: |
| 1894 |
def method(self): |
| 1895 |
return ["should be hidden"] |
| 1896 |
def indirect(self): |
| 1897 |
return self.method() |
| 1898 |
class A(Base): |
| 1899 |
def method(self): |
| 1900 |
return "visible" |
| 1901 |
class B(A): |
| 1902 |
def method(self): |
| 1903 |
return None |
| 1904 |
def f(flag): |
| 1905 |
if flag: |
| 1906 |
obj = A() |
| 1907 |
else: |
| 1908 |
obj = B() |
| 1909 |
return obj.indirect() |
| 1910 |
a = self.RPythonAnnotator() |
| 1911 |
s = a.build_types(f, [bool]) |
| 1912 |
assert annmodel.SomeString(can_be_None=True).contains(s) |
| 1913 |
|
| 1914 |
def test_dont_see_AttributeError_clause(self): |
| 1915 |
class Stuff: |
| 1916 |
def _freeze_(self): |
| 1917 |
return True |
| 1918 |
def createcompiler(self): |
| 1919 |
try: |
| 1920 |
return self.default_compiler |
| 1921 |
except AttributeError: |
| 1922 |
compiler = "yadda" |
| 1923 |
self.default_compiler = compiler |
| 1924 |
return compiler |
| 1925 |
stuff = Stuff() |
| 1926 |
stuff.default_compiler = 123 |
| 1927 |
def f(): |
| 1928 |
return stuff.createcompiler() |
| 1929 |
a = self.RPythonAnnotator() |
| 1930 |
s = a.build_types(f, []) |
| 1931 |
assert s == a.bookkeeper.immutablevalue(123) |
| 1932 |
|
| 1933 |
def test_class_attribute_is_an_instance_of_itself(self): |
| 1934 |
class Base: |
| 1935 |
hello = None |
| 1936 |
class A(Base): |
| 1937 |
pass |
| 1938 |
A.hello = globalA = A() |
| 1939 |
def f(): |
| 1940 |
return (Base().hello, globalA) |
| 1941 |
a = self.RPythonAnnotator() |
| 1942 |
s = a.build_types(f, []) |
| 1943 |
assert isinstance(s, annmodel.SomeTuple) |
| 1944 |
assert isinstance(s.items[0], annmodel.SomeInstance) |
| 1945 |
assert s.items[0].classdef is a.bookkeeper.getuniqueclassdef(A) |
| 1946 |
assert s.items[0].can_be_None |
| 1947 |
assert s.items[1] == a.bookkeeper.immutablevalue(A.hello) |
| 1948 |
|
| 1949 |
def test_dict_and_none(self): |
| 1950 |
def f(i): |
| 1951 |
if i: |
| 1952 |
return {} |
| 1953 |
else: |
| 1954 |
return None |
| 1955 |
a = self.RPythonAnnotator() |
| 1956 |
s = a.build_types(f, [int]) |
| 1957 |
assert s.knowntype == annmodel.SomeOrderedDict.knowntype |
| 1958 |
|
| 1959 |
def test_const_list_and_none(self): |
| 1960 |
def g(l=None): |
| 1961 |
return l is None |
| 1962 |
L = [1,2] |
| 1963 |
def f(): |
| 1964 |
g() |
| 1965 |
return g(L) |
| 1966 |
a = self.RPythonAnnotator() |
| 1967 |
s = a.build_types(f, []) |
| 1968 |
assert s.knowntype == bool |
| 1969 |
assert not s.is_constant() |
| 1970 |
|
| 1971 |
def test_const_dict_and_none(self): |
| 1972 |
def g(d=None): |
| 1973 |
return d is None |
| 1974 |
D = {1:2} |
| 1975 |
def f(): |
| 1976 |
g(D) |
| 1977 |
return g() |
| 1978 |
a = self.RPythonAnnotator() |
| 1979 |
s = a.build_types(f, []) |
| 1980 |
assert s.knowntype == bool |
| 1981 |
assert not s.is_constant() |
| 1982 |
|
| 1983 |
def test_issubtype_and_const(self): |
| 1984 |
class A(object): |
| 1985 |
pass |
| 1986 |
class B(object): |
| 1987 |
pass |
| 1988 |
class C(A): |
| 1989 |
pass |
| 1990 |
b = B() |
| 1991 |
c = C() |
| 1992 |
def g(f): |
| 1993 |
if f == 1: |
| 1994 |
x = b |
| 1995 |
elif f == 2: |
| 1996 |
x = c |
| 1997 |
else: |
| 1998 |
x = C() |
| 1999 |
t = type(x) |
| 2000 |
return issubclass(t, A) |
| 2001 |
|
| 2002 |
a = self.RPythonAnnotator() |
| 2003 |
x = annmodel.SomeInteger() |
| 2004 |
x.const = 1 |
| 2005 |
s = a.build_types(g, [x]) |
| 2006 |
assert s.const == False |
| 2007 |
a = self.RPythonAnnotator() |
| 2008 |
x = annmodel.SomeInteger() |
| 2009 |
x.const = 2 |
| 2010 |
s = a.build_types(g, [x]) |
| 2011 |
assert s.const == True |
| 2012 |
|
| 2013 |
def test_reading_also_generalizes(self): |
| 2014 |
def f1(i): |
| 2015 |
d = {'c': i} |
| 2016 |
return d['not-a-char'], d |
| 2017 |
a = self.RPythonAnnotator() |
| 2018 |
s = a.build_types(f1, [int]) |
| 2019 |
assert dictkey(s.items[1]).__class__ == annmodel.SomeString |
| 2020 |
def f2(i): |
| 2021 |
d = {'c': i} |
| 2022 |
return d.get('not-a-char', i+1), d |
| 2023 |
a = self.RPythonAnnotator() |
| 2024 |
s = a.build_types(f2, [int]) |
| 2025 |
assert dictkey(s.items[1]).__class__ == annmodel.SomeString |
| 2026 |
def f3(i): |
| 2027 |
d = {'c': i} |
| 2028 |
return 'not-a-char' in d, d |
| 2029 |
a = self.RPythonAnnotator() |
| 2030 |
s = a.build_types(f3, [int]) |
| 2031 |
assert dictkey(s.items[1]).__class__ == annmodel.SomeString |
| 2032 |
def f4(): |
| 2033 |
lst = ['a', 'b', 'c'] |
| 2034 |
return 'not-a-char' in lst, lst |
| 2035 |
a = self.RPythonAnnotator() |
| 2036 |
s = a.build_types(f4, []) |
| 2037 |
assert listitem(s.items[1]).__class__ == annmodel.SomeString |
| 2038 |
def f5(): |
| 2039 |
lst = ['a', 'b', 'c'] |
| 2040 |
return lst.index('not-a-char'), lst |
| 2041 |
a = self.RPythonAnnotator() |
| 2042 |
s = a.build_types(f5, []) |
| 2043 |
assert listitem(s.items[1]).__class__ == annmodel.SomeString |
| 2044 |
|
| 2045 |
def test_true_str_is_not_none(self): |
| 2046 |
def f(s): |
| 2047 |
if s: |
| 2048 |
return s |
| 2049 |
else: |
| 2050 |
return '' |
| 2051 |
def g(i): |
| 2052 |
if i: |
| 2053 |
return f(None) |
| 2054 |
else: |
| 2055 |
return f('') |
| 2056 |
a = self.RPythonAnnotator() |
| 2057 |
s = a.build_types(g, [int]) |
| 2058 |
assert s.knowntype == str |
| 2059 |
assert not s.can_be_None |
| 2060 |
|
| 2061 |
def test_true_func_is_not_none(self): |
| 2062 |
def a1(): |
| 2063 |
pass |
| 2064 |
def a2(): |
| 2065 |
pass |
| 2066 |
def f(a): |
| 2067 |
if a: |
| 2068 |
return a |
| 2069 |
else: |
| 2070 |
return a2 |
| 2071 |
def g(i): |
| 2072 |
if i: |
| 2073 |
return f(None) |
| 2074 |
else: |
| 2075 |
return f(a1) |
| 2076 |
a = self.RPythonAnnotator() |
| 2077 |
s = a.build_types(g, [int]) |
| 2078 |
assert not s.can_be_None |
| 2079 |
|
| 2080 |
def test_string_noNUL_canbeNone(self): |
| 2081 |
def f(a): |
| 2082 |
if a: |
| 2083 |
return "abc" |
| 2084 |
else: |
| 2085 |
return None |
| 2086 |
a = self.RPythonAnnotator() |
| 2087 |
s = a.build_types(f, [int]) |
| 2088 |
assert s.can_be_None |
| 2089 |
assert s.no_nul |
| 2090 |
|
| 2091 |
def test_unicode_noNUL_canbeNone(self): |
| 2092 |
def f(a): |
| 2093 |
if a: |
| 2094 |
return u"abc" |
| 2095 |
else: |
| 2096 |
return None |
| 2097 |
a = self.RPythonAnnotator() |
| 2098 |
s = a.build_types(f, [int]) |
| 2099 |
assert s.can_be_None |
| 2100 |
assert s.no_nul |
| 2101 |
|
| 2102 |
def test_str_or_None(self): |
| 2103 |
def f(a): |
| 2104 |
if a: |
| 2105 |
return "abc" |
| 2106 |
else: |
| 2107 |
return None |
| 2108 |
def g(a): |
| 2109 |
x = f(a) |
| 2110 |
if x is None: |
| 2111 |
return "abcd" |
| 2112 |
return x |
| 2113 |
a = self.RPythonAnnotator() |
| 2114 |
s = a.build_types(f, [int]) |
| 2115 |
assert s.can_be_None |
| 2116 |
assert s.no_nul |
| 2117 |
|
| 2118 |
def test_unicode_or_None(self): |
| 2119 |
def f(a): |
| 2120 |
if a: |
| 2121 |
return u"abc" |
| 2122 |
else: |
| 2123 |
return None |
| 2124 |
def g(a): |
| 2125 |
x = f(a) |
| 2126 |
if x is None: |
| 2127 |
return u"abcd" |
| 2128 |
return x |
| 2129 |
a = self.RPythonAnnotator() |
| 2130 |
s = a.build_types(f, [int]) |
| 2131 |
assert s.can_be_None |
| 2132 |
assert s.no_nul |
| 2133 |
|
| 2134 |
def test_emulated_pbc_call_simple(self): |
| 2135 |
def f(a,b): |
| 2136 |
return a + b |
| 2137 |
from rpython.annotator import annrpython |
| 2138 |
a = annrpython.RPythonAnnotator() |
| 2139 |
from rpython.annotator import model as annmodel |
| 2140 |
|
| 2141 |
s_f = a.bookkeeper.immutablevalue(f) |
| 2142 |
a.bookkeeper.emulate_pbc_call('f', s_f, [annmodel.SomeInteger(), annmodel.SomeInteger()]) |
| 2143 |
a.complete() |
| 2144 |
a.simplify() |
| 2145 |
|
| 2146 |
assert a.binding(graphof(a, f).getreturnvar()).knowntype == int |
| 2147 |
fdesc = a.bookkeeper.getdesc(f) |
| 2148 |
|
| 2149 |
someint = annmodel.SomeInteger() |
| 2150 |
|
| 2151 |
assert (fdesc.get_s_signatures((2, (), False)) |
| 2152 |
== [([someint,someint],someint)]) |
| 2153 |
|
| 2154 |
def test_iterator_union(self): |
| 2155 |
def it(d): |
| 2156 |
return d.iteritems() |
| 2157 |
d0 = {1:2} |
| 2158 |
def f(): |
| 2159 |
it(d0) |
| 2160 |
return it({1:2}) |
| 2161 |
a = self.RPythonAnnotator() |
| 2162 |
s = a.build_types(f, []) |
| 2163 |
assert isinstance(s, annmodel.SomeIterator) |
| 2164 |
assert s.variant == ('items',) |
| 2165 |
|
| 2166 |
def test_iteritems_str0(self): |
| 2167 |
def it(d): |
| 2168 |
return d.iteritems() |
| 2169 |
def f(): |
| 2170 |
d0 = {'1a': '2a', '3': '4'} |
| 2171 |
for item in it(d0): |
| 2172 |
return "%s=%s" % item |
| 2173 |
raise ValueError |
| 2174 |
a = self.RPythonAnnotator() |
| 2175 |
s = a.build_types(f, []) |
| 2176 |
assert isinstance(s, annmodel.SomeString) |
| 2177 |
assert s.no_nul |
| 2178 |
|
| 2179 |
def test_iteritems_unicode0(self): |
| 2180 |
def it(d): |
| 2181 |
return d.iteritems() |
| 2182 |
def f(): |
| 2183 |
d0 = {u'1a': u'2a', u'3': u'4'} |
| 2184 |
for item in it(d0): |
| 2185 |
return u"%s=%s" % item |
| 2186 |
raise ValueError |
| 2187 |
a = self.RPythonAnnotator() |
| 2188 |
s = a.build_types(f, []) |
| 2189 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 2190 |
assert s.no_nul |
| 2191 |
|
| 2192 |
def test_no_nul_mod(self): |
| 2193 |
def f(x): |
| 2194 |
s = "%d" % x |
| 2195 |
return s |
| 2196 |
a = self.RPythonAnnotator() |
| 2197 |
s = a.build_types(f, [int]) |
| 2198 |
assert isinstance(s, annmodel.SomeString) |
| 2199 |
assert s.no_nul |
| 2200 |
|
| 2201 |
def test_no_nul_mod_unicode(self): |
| 2202 |
def f(x): |
| 2203 |
s = u"%d" % x |
| 2204 |
return s |
| 2205 |
a = self.RPythonAnnotator() |
| 2206 |
s = a.build_types(f, [int]) |
| 2207 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 2208 |
assert s.no_nul |
| 2209 |
|
| 2210 |
def test_mul_str0(self): |
| 2211 |
def f(s): |
| 2212 |
return s*10 |
| 2213 |
a = self.RPythonAnnotator() |
| 2214 |
s = a.build_types(f, [annmodel.SomeString(no_nul=True)]) |
| 2215 |
assert isinstance(s, annmodel.SomeString) |
| 2216 |
assert s.no_nul |
| 2217 |
|
| 2218 |
a = self.RPythonAnnotator() |
| 2219 |
s = a.build_types(f, [annmodel.SomeUnicodeString(no_nul=True)]) |
| 2220 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 2221 |
assert s.no_nul |
| 2222 |
|
| 2223 |
def test_reverse_mul_str0(self): |
| 2224 |
def f(s): |
| 2225 |
return 10*s |
| 2226 |
a = self.RPythonAnnotator() |
| 2227 |
s = a.build_types(f, [annmodel.SomeString(no_nul=True)]) |
| 2228 |
assert isinstance(s, annmodel.SomeString) |
| 2229 |
assert s.no_nul |
| 2230 |
|
| 2231 |
a = self.RPythonAnnotator() |
| 2232 |
s = a.build_types(f, [annmodel.SomeUnicodeString(no_nul=True)]) |
| 2233 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 2234 |
assert s.no_nul |
| 2235 |
|
| 2236 |
def test_getitem_str0(self): |
| 2237 |
def f(s, n): |
| 2238 |
if n == 1: |
| 2239 |
return s[0] |
| 2240 |
elif n == 2: |
| 2241 |
return s[1] |
| 2242 |
elif n == 3: |
| 2243 |
return s[1:] |
| 2244 |
return s |
| 2245 |
a = self.RPythonAnnotator() |
| 2246 |
a.translator.config.translation.check_str_without_nul = True |
| 2247 |
s = a.build_types(f, [annmodel.SomeString(no_nul=True), |
| 2248 |
annmodel.SomeInteger()]) |
| 2249 |
assert isinstance(s, annmodel.SomeString) |
| 2250 |
assert s.no_nul |
| 2251 |
|
| 2252 |
a = self.RPythonAnnotator() |
| 2253 |
a.translator.config.translation.check_str_without_nul = True |
| 2254 |
s = a.build_types(f, [annmodel.SomeUnicodeString(no_nul=True), |
| 2255 |
annmodel.SomeInteger()]) |
| 2256 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 2257 |
assert s.no_nul |
| 2258 |
|
| 2259 |
def test_non_none_and_none_with_isinstance(self): |
| 2260 |
class A(object): |
| 2261 |
pass |
| 2262 |
class B(A): |
| 2263 |
pass |
| 2264 |
def g(x): |
| 2265 |
if isinstance(x, A): |
| 2266 |
return x |
| 2267 |
return None |
| 2268 |
def f(): |
| 2269 |
g(B()) |
| 2270 |
return g(None) |
| 2271 |
a = self.RPythonAnnotator() |
| 2272 |
s = a.build_types(f, []) |
| 2273 |
assert isinstance(s, annmodel.SomeInstance) |
| 2274 |
assert s.classdef == a.bookkeeper.getuniqueclassdef(B) |
| 2275 |
|
| 2276 |
def test_type_is_no_improvement(self): |
| 2277 |
class B(object): |
| 2278 |
pass |
| 2279 |
class C(B): |
| 2280 |
pass |
| 2281 |
class D(B): |
| 2282 |
pass |
| 2283 |
def f(x): |
| 2284 |
if type(x) is C: |
| 2285 |
return x |
| 2286 |
raise Exception |
| 2287 |
a = self.RPythonAnnotator() |
| 2288 |
s = a.build_types(f, [D]) |
| 2289 |
assert s == annmodel.SomeImpossibleValue() |
| 2290 |
|
| 2291 |
def test_is_constant_instance(self): |
| 2292 |
class A(object): |
| 2293 |
pass |
| 2294 |
prebuilt_instance = A() |
| 2295 |
def f(x): |
| 2296 |
if x is prebuilt_instance: |
| 2297 |
return x |
| 2298 |
raise Exception |
| 2299 |
a = self.RPythonAnnotator() |
| 2300 |
s = a.build_types(f, [A]) |
| 2301 |
assert s.is_constant() |
| 2302 |
assert s.const is prebuilt_instance |
| 2303 |
|
| 2304 |
def test_call_memoized_function(self): |
| 2305 |
fr1 = Freezing() |
| 2306 |
fr2 = Freezing() |
| 2307 |
def getorbuild(key): |
| 2308 |
a = 1 |
| 2309 |
if key is fr1: |
| 2310 |
result = eval("a+2") |
| 2311 |
else: |
| 2312 |
result = eval("a+6") |
| 2313 |
return result |
| 2314 |
getorbuild._annspecialcase_ = "specialize:memo" |
| 2315 |
|
| 2316 |
def f1(i): |
| 2317 |
if i > 0: |
| 2318 |
fr = fr1 |
| 2319 |
else: |
| 2320 |
fr = fr2 |
| 2321 |
return getorbuild(fr) |
| 2322 |
|
| 2323 |
a = self.RPythonAnnotator() |
| 2324 |
s = a.build_types(f1, [int]) |
| 2325 |
assert s.knowntype == int |
| 2326 |
|
| 2327 |
def test_call_memoized_function_with_bools(self): |
| 2328 |
fr1 = Freezing() |
| 2329 |
fr2 = Freezing() |
| 2330 |
def getorbuild(key, flag1, flag2): |
| 2331 |
a = 1 |
| 2332 |
if key is fr1: |
| 2333 |
result = eval("a+2") |
| 2334 |
else: |
| 2335 |
result = eval("a+6") |
| 2336 |
if flag1: |
| 2337 |
result += 100 |
| 2338 |
if flag2: |
| 2339 |
result += 1000 |
| 2340 |
return result |
| 2341 |
getorbuild._annspecialcase_ = "specialize:memo" |
| 2342 |
|
| 2343 |
def f1(i): |
| 2344 |
if i > 0: |
| 2345 |
fr = fr1 |
| 2346 |
else: |
| 2347 |
fr = fr2 |
| 2348 |
return getorbuild(fr, i % 2 == 0, i % 3 == 0) |
| 2349 |
|
| 2350 |
a = self.RPythonAnnotator() |
| 2351 |
s = a.build_types(f1, [int]) |
| 2352 |
assert s.knowntype == int |
| 2353 |
|
| 2354 |
def test_stored_bound_method(self): |
| 2355 |
|
| 2356 |
class H: |
| 2357 |
def h(self): |
| 2358 |
return 42 |
| 2359 |
class C: |
| 2360 |
def __init__(self, func): |
| 2361 |
self.f = func |
| 2362 |
def do(self): |
| 2363 |
return self.f() |
| 2364 |
def g(): |
| 2365 |
h = H() |
| 2366 |
c = C(h.h) |
| 2367 |
return c.do() |
| 2368 |
|
| 2369 |
a = self.RPythonAnnotator() |
| 2370 |
s = a.build_types(g, []) |
| 2371 |
assert s.is_constant() |
| 2372 |
assert s.const == 42 |
| 2373 |
|
| 2374 |
def test_stored_bound_method_2(self): |
| 2375 |
|
| 2376 |
class H: |
| 2377 |
pass |
| 2378 |
class H1(H): |
| 2379 |
def h(self): |
| 2380 |
return 42 |
| 2381 |
class H2(H): |
| 2382 |
def h(self): |
| 2383 |
return 17 |
| 2384 |
class C: |
| 2385 |
def __init__(self, func): |
| 2386 |
self.f = func |
| 2387 |
def do(self): |
| 2388 |
return self.f() |
| 2389 |
def g(flag): |
| 2390 |
if flag: |
| 2391 |
h = H1() |
| 2392 |
else: |
| 2393 |
h = H2() |
| 2394 |
c = C(h.h) |
| 2395 |
return c.do() |
| 2396 |
|
| 2397 |
a = self.RPythonAnnotator() |
| 2398 |
s = a.build_types(g, [int]) |
| 2399 |
assert s.knowntype == int |
| 2400 |
assert not s.is_constant() |
| 2401 |
|
| 2402 |
def test_getorbuild_as_attr(self): |
| 2403 |
from rpython.rlib.cache import Cache |
| 2404 |
class SpaceCache(Cache): |
| 2405 |
def _build(self, callable): |
| 2406 |
return callable() |
| 2407 |
class CacheX(Cache): |
| 2408 |
def _build(self, key): |
| 2409 |
return key.x |
| 2410 |
class CacheY(Cache): |
| 2411 |
def _build(self, key): |
| 2412 |
return key.y |
| 2413 |
class X: |
| 2414 |
def __init__(self, x): |
| 2415 |
self.x = x |
| 2416 |
def _freeze_(self): |
| 2417 |
return True |
| 2418 |
class Y: |
| 2419 |
def __init__(self, y): |
| 2420 |
self.y = y |
| 2421 |
def _freeze_(self): |
| 2422 |
return True |
| 2423 |
X1 = X(1) |
| 2424 |
Y2 = Y("hello") |
| 2425 |
fromcache = SpaceCache().getorbuild |
| 2426 |
def f(): |
| 2427 |
return (fromcache(CacheX).getorbuild(X1), |
| 2428 |
fromcache(CacheY).getorbuild(Y2)) |
| 2429 |
|
| 2430 |
a = self.RPythonAnnotator() |
| 2431 |
s = a.build_types(f, []) |
| 2432 |
assert s.items[0].knowntype == int |
| 2433 |
assert s.items[1].knowntype == str |
| 2434 |
|
| 2435 |
def test_constant_bound_method(self): |
| 2436 |
class C: |
| 2437 |
def __init__(self, value): |
| 2438 |
self.value = value |
| 2439 |
def meth(self): |
| 2440 |
return self.value |
| 2441 |
meth = C(1).meth |
| 2442 |
def f(): |
| 2443 |
return meth() |
| 2444 |
a = self.RPythonAnnotator() |
| 2445 |
s = a.build_types(f, []) |
| 2446 |
assert s.knowntype == int |
| 2447 |
|
| 2448 |
def test_annotate__del__(self): |
| 2449 |
class A(object): |
| 2450 |
def __init__(self): |
| 2451 |
self.a = 2 |
| 2452 |
def __del__(self): |
| 2453 |
self.a = 1 |
| 2454 |
def f(): |
| 2455 |
return A().a |
| 2456 |
a = self.RPythonAnnotator() |
| 2457 |
t = a.translator |
| 2458 |
s = a.build_types(f, []) |
| 2459 |
assert s.knowntype == int |
| 2460 |
graph = tgraphof(t, A.__del__.im_func) |
| 2461 |
assert graph.startblock in a.annotated |
| 2462 |
|
| 2463 |
def test_annotate__del__baseclass(self): |
| 2464 |
class A(object): |
| 2465 |
def __init__(self): |
| 2466 |
self.a = 2 |
| 2467 |
def __del__(self): |
| 2468 |
self.a = 1 |
| 2469 |
class B(A): |
| 2470 |
def __init__(self): |
| 2471 |
self.a = 3 |
| 2472 |
def f(): |
| 2473 |
return B().a |
| 2474 |
a = self.RPythonAnnotator() |
| 2475 |
t = a.translator |
| 2476 |
s = a.build_types(f, []) |
| 2477 |
assert s.knowntype == int |
| 2478 |
graph = tgraphof(t, A.__del__.im_func) |
| 2479 |
assert graph.startblock in a.annotated |
| 2480 |
|
| 2481 |
def test_annotate_type(self): |
| 2482 |
class A: |
| 2483 |
pass |
| 2484 |
x = [A(), A()] |
| 2485 |
def witness(t): |
| 2486 |
return type(t) |
| 2487 |
def get(i): |
| 2488 |
return x[i] |
| 2489 |
def f(i): |
| 2490 |
witness(None) |
| 2491 |
return witness(get(i)) |
| 2492 |
|
| 2493 |
a = self.RPythonAnnotator() |
| 2494 |
s = a.build_types(f, [int]) |
| 2495 |
assert isinstance(s, annmodel.SomeType) |
| 2496 |
|
| 2497 |
def test_annotate_iter_empty_container(self): |
| 2498 |
def f(): |
| 2499 |
n = 0 |
| 2500 |
d = {} |
| 2501 |
for x in []: n += x |
| 2502 |
for y in d: n += y |
| 2503 |
for z in d.iterkeys(): n += z |
| 2504 |
for s in d.itervalues(): n += s |
| 2505 |
for t, u in d.items(): n += t * u |
| 2506 |
for t, u in d.iteritems(): n += t * u |
| 2507 |
return n |
| 2508 |
|
| 2509 |
a = self.RPythonAnnotator() |
| 2510 |
s = a.build_types(f, []) |
| 2511 |
assert s.is_constant() |
| 2512 |
assert s.const == 0 |
| 2513 |
|
| 2514 |
def test_mixin(self): |
| 2515 |
class Mixin(object): |
| 2516 |
_mixin_ = True |
| 2517 |
|
| 2518 |
def m(self, v): |
| 2519 |
return v |
| 2520 |
|
| 2521 |
class Base(object): |
| 2522 |
pass |
| 2523 |
|
| 2524 |
class A(Base, Mixin): |
| 2525 |
pass |
| 2526 |
|
| 2527 |
class B(Base, Mixin): |
| 2528 |
pass |
| 2529 |
|
| 2530 |
class C(B): |
| 2531 |
pass |
| 2532 |
|
| 2533 |
def f(): |
| 2534 |
a = A() |
| 2535 |
v0 = a.m(2) |
| 2536 |
b = B() |
| 2537 |
v1 = b.m('x') |
| 2538 |
c = C() |
| 2539 |
v2 = c.m('y') |
| 2540 |
return v0, v1, v2 |
| 2541 |
|
| 2542 |
a = self.RPythonAnnotator() |
| 2543 |
s = a.build_types(f, []) |
| 2544 |
assert isinstance(s.items[0], annmodel.SomeInteger) |
| 2545 |
assert isinstance(s.items[1], annmodel.SomeChar) |
| 2546 |
assert isinstance(s.items[2], annmodel.SomeChar) |
| 2547 |
|
| 2548 |
def test_mixin_staticmethod(self): |
| 2549 |
class Mixin(object): |
| 2550 |
_mixin_ = True |
| 2551 |
|
| 2552 |
@staticmethod |
| 2553 |
def m(v): |
| 2554 |
return v |
| 2555 |
|
| 2556 |
class Base(object): |
| 2557 |
pass |
| 2558 |
|
| 2559 |
class A(Base, Mixin): |
| 2560 |
pass |
| 2561 |
|
| 2562 |
class B(Base, Mixin): |
| 2563 |
pass |
| 2564 |
|
| 2565 |
class C(B): |
| 2566 |
pass |
| 2567 |
|
| 2568 |
def f(): |
| 2569 |
a = A() |
| 2570 |
v0 = a.m(2) |
| 2571 |
b = B() |
| 2572 |
v1 = b.m('x') |
| 2573 |
c = C() |
| 2574 |
v2 = c.m('y') |
| 2575 |
return v0, v1, v2 |
| 2576 |
|
| 2577 |
a = self.RPythonAnnotator() |
| 2578 |
s = a.build_types(f, []) |
| 2579 |
assert isinstance(s.items[0], annmodel.SomeInteger) |
| 2580 |
assert isinstance(s.items[1], annmodel.SomeChar) |
| 2581 |
assert isinstance(s.items[2], annmodel.SomeChar) |
| 2582 |
|
| 2583 |
def test_mixin_first(self): |
| 2584 |
class Mixin(object): |
| 2585 |
_mixin_ = True |
| 2586 |
def foo(self): return 4 |
| 2587 |
class Base(object): |
| 2588 |
def foo(self): return 5 |
| 2589 |
class Concrete(Mixin, Base): |
| 2590 |
pass |
| 2591 |
def f(): |
| 2592 |
return Concrete().foo() |
| 2593 |
|
| 2594 |
assert f() == 4 |
| 2595 |
a = self.RPythonAnnotator() |
| 2596 |
s = a.build_types(f, []) |
| 2597 |
assert s.const == 4 |
| 2598 |
|
| 2599 |
def test_mixin_last(self): |
| 2600 |
class Mixin(object): |
| 2601 |
_mixin_ = True |
| 2602 |
def foo(self): return 4 |
| 2603 |
class Base(object): |
| 2604 |
def foo(self): return 5 |
| 2605 |
class Concrete(Base, Mixin): |
| 2606 |
pass |
| 2607 |
def f(): |
| 2608 |
return Concrete().foo() |
| 2609 |
|
| 2610 |
assert f() == 5 |
| 2611 |
a = self.RPythonAnnotator() |
| 2612 |
s = a.build_types(f, []) |
| 2613 |
assert s.const == 5 |
| 2614 |
|
| 2615 |
def test_mixin_concrete(self): |
| 2616 |
class Mixin(object): |
| 2617 |
_mixin_ = True |
| 2618 |
def foo(self): return 4 |
| 2619 |
class Concrete(Mixin): |
| 2620 |
def foo(self): return 5 |
| 2621 |
def f(): |
| 2622 |
return Concrete().foo() |
| 2623 |
|
| 2624 |
assert f() == 5 |
| 2625 |
a = self.RPythonAnnotator() |
| 2626 |
s = a.build_types(f, []) |
| 2627 |
assert s.const == 5 |
| 2628 |
|
| 2629 |
def test_multiple_mixins_mro(self): |
| 2630 |
|
| 2631 |
class A(object): |
| 2632 |
_mixin_ = True |
| 2633 |
def foo(self): return 1 |
| 2634 |
class B(A): |
| 2635 |
_mixin_ = True |
| 2636 |
def foo(self): return 2 |
| 2637 |
class C(A): |
| 2638 |
_mixin_ = True |
| 2639 |
class D(B, C): |
| 2640 |
_mixin_ = True |
| 2641 |
class Concrete(D): |
| 2642 |
pass |
| 2643 |
def f(): |
| 2644 |
return Concrete().foo() |
| 2645 |
|
| 2646 |
assert f() == 2 |
| 2647 |
a = self.RPythonAnnotator() |
| 2648 |
s = a.build_types(f, []) |
| 2649 |
assert s.const == 2 |
| 2650 |
|
| 2651 |
def test_multiple_mixins_mro_2(self): |
| 2652 |
class A(object): |
| 2653 |
_mixin_ = True |
| 2654 |
def foo(self): return 1 |
| 2655 |
class B(A): |
| 2656 |
_mixin_ = True |
| 2657 |
def foo(self): return 2 |
| 2658 |
class C(A): |
| 2659 |
_mixin_ = True |
| 2660 |
class Concrete(C, B): |
| 2661 |
pass |
| 2662 |
def f(): |
| 2663 |
return Concrete().foo() |
| 2664 |
|
| 2665 |
assert f() == 2 |
| 2666 |
a = self.RPythonAnnotator() |
| 2667 |
s = a.build_types(f, []) |
| 2668 |
assert s.const == 2 |
| 2669 |
|
| 2670 |
def test_cannot_use_directly_mixin(self): |
| 2671 |
class A(object): |
| 2672 |
_mixin_ = True |
| 2673 |
|
| 2674 |
def f(): |
| 2675 |
return A() |
| 2676 |
a = self.RPythonAnnotator() |
| 2677 |
py.test.raises(AnnotatorError, a.build_types, f, []) |
| 2678 |
|
| 2679 |
class B(object): |
| 2680 |
pass |
| 2681 |
x = B() |
| 2682 |
def g(): |
| 2683 |
return isinstance(x, A) |
| 2684 |
py.test.raises(AnnotatorError, a.build_types, g, []) |
| 2685 |
|
| 2686 |
def test_import_from_mixin(self): |
| 2687 |
class M(object): |
| 2688 |
def f(self): |
| 2689 |
return self.a |
| 2690 |
class I(object): |
| 2691 |
objectmodel.import_from_mixin(M) |
| 2692 |
def __init__(self, i): |
| 2693 |
self.a = i |
| 2694 |
class S(object): |
| 2695 |
objectmodel.import_from_mixin(M) |
| 2696 |
def __init__(self, s): |
| 2697 |
self.a = s |
| 2698 |
def f(n): |
| 2699 |
return (I(n).f(), S("a" * n).f()) |
| 2700 |
|
| 2701 |
assert f(3) == (3, "aaa") |
| 2702 |
a = self.RPythonAnnotator() |
| 2703 |
s = a.build_types(f, [int]) |
| 2704 |
assert isinstance(s.items[0], annmodel.SomeInteger) |
| 2705 |
assert isinstance(s.items[1], annmodel.SomeString) |
| 2706 |
|
| 2707 |
def test___class___attribute(self): |
| 2708 |
class Base(object): pass |
| 2709 |
class A(Base): pass |
| 2710 |
class B(Base): pass |
| 2711 |
class C(A): pass |
| 2712 |
def seelater(): |
| 2713 |
C() |
| 2714 |
def f(n): |
| 2715 |
if n == 1: |
| 2716 |
x = A() |
| 2717 |
else: |
| 2718 |
x = B() |
| 2719 |
y = B() |
| 2720 |
result = x.__class__, y.__class__ |
| 2721 |
seelater() |
| 2722 |
return result |
| 2723 |
|
| 2724 |
a = self.RPythonAnnotator() |
| 2725 |
s = a.build_types(f, [int]) |
| 2726 |
assert isinstance(s.items[0], annmodel.SomePBC) |
| 2727 |
assert len(s.items[0].descriptions) == 4 |
| 2728 |
assert isinstance(s.items[1], annmodel.SomePBC) |
| 2729 |
assert len(s.items[1].descriptions) == 1 |
| 2730 |
|
| 2731 |
def test_slots(self): |
| 2732 |
|
| 2733 |
|
| 2734 |
class A(object): |
| 2735 |
__slots__ = ('a', 'b') |
| 2736 |
def f(x): |
| 2737 |
a = A() |
| 2738 |
a.b = x |
| 2739 |
return a.b |
| 2740 |
|
| 2741 |
a = self.RPythonAnnotator() |
| 2742 |
s = a.build_types(f, [int]) |
| 2743 |
assert s.knowntype == int |
| 2744 |
|
| 2745 |
def test_slots_reads(self): |
| 2746 |
class A(object): |
| 2747 |
__slots__ = () |
| 2748 |
class B(A): |
| 2749 |
def __init__(self, x): |
| 2750 |
self.x = x |
| 2751 |
def f(x): |
| 2752 |
if x: |
| 2753 |
a = A() |
| 2754 |
else: |
| 2755 |
a = B(x) |
| 2756 |
return a.x |
| 2757 |
|
| 2758 |
a = self.RPythonAnnotator() |
| 2759 |
with py.test.raises(NoSuchAttrError) as excinfo: |
| 2760 |
a.build_types(f, [int]) |
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
def test_unboxed_value(self): |
| 2767 |
class A(object): |
| 2768 |
__slots__ = () |
| 2769 |
class C(A, objectmodel.UnboxedValue): |
| 2770 |
__slots__ = unboxedattrname = 'smallint' |
| 2771 |
def f(n): |
| 2772 |
return C(n).smallint |
| 2773 |
|
| 2774 |
a = self.RPythonAnnotator() |
| 2775 |
s = a.build_types(f, [int]) |
| 2776 |
assert s.knowntype == int |
| 2777 |
|
| 2778 |
|
| 2779 |
def test_annotate_bool(self): |
| 2780 |
def f(x): |
| 2781 |
return ~x |
| 2782 |
a = self.RPythonAnnotator() |
| 2783 |
s = a.build_types(f, [bool]) |
| 2784 |
assert s.knowntype == int |
| 2785 |
|
| 2786 |
|
| 2787 |
def f(x): |
| 2788 |
return -x |
| 2789 |
a = self.RPythonAnnotator() |
| 2790 |
s = a.build_types(f, [bool]) |
| 2791 |
assert s.knowntype == int |
| 2792 |
|
| 2793 |
def f(x): |
| 2794 |
return +x |
| 2795 |
a = self.RPythonAnnotator() |
| 2796 |
s = a.build_types(f, [bool]) |
| 2797 |
assert s.knowntype == int |
| 2798 |
|
| 2799 |
def f(x): |
| 2800 |
return abs(x) |
| 2801 |
a = self.RPythonAnnotator() |
| 2802 |
s = a.build_types(f, [bool]) |
| 2803 |
assert s.knowntype == int |
| 2804 |
|
| 2805 |
def f(x): |
| 2806 |
return int(x) |
| 2807 |
a = self.RPythonAnnotator() |
| 2808 |
s = a.build_types(f, [bool]) |
| 2809 |
assert s.knowntype == int |
| 2810 |
|
| 2811 |
|
| 2812 |
def f(x, y): |
| 2813 |
return x + y |
| 2814 |
a = self.RPythonAnnotator() |
| 2815 |
s = a.build_types(f, [bool, int]) |
| 2816 |
assert s.knowntype == int |
| 2817 |
|
| 2818 |
a = self.RPythonAnnotator() |
| 2819 |
s = a.build_types(f, [int, bool]) |
| 2820 |
assert s.knowntype == int |
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
def test_annotate_rarith(self): |
| 2825 |
inttypes = [int, r_uint, r_longlong, r_ulonglong] |
| 2826 |
for inttype in inttypes: |
| 2827 |
c = inttype() |
| 2828 |
def f(): |
| 2829 |
return c |
| 2830 |
a = self.RPythonAnnotator() |
| 2831 |
s = a.build_types(f, []) |
| 2832 |
assert isinstance(s, annmodel.SomeInteger) |
| 2833 |
assert s.knowntype == inttype |
| 2834 |
assert s.unsigned == (inttype(-1) > 0) |
| 2835 |
|
| 2836 |
for inttype in inttypes: |
| 2837 |
def f(): |
| 2838 |
return inttype(0) |
| 2839 |
a = self.RPythonAnnotator() |
| 2840 |
s = a.build_types(f, []) |
| 2841 |
assert isinstance(s, annmodel.SomeInteger) |
| 2842 |
assert s.knowntype == inttype |
| 2843 |
assert s.unsigned == (inttype(-1) > 0) |
| 2844 |
|
| 2845 |
for inttype in inttypes: |
| 2846 |
def f(x): |
| 2847 |
return x |
| 2848 |
a = self.RPythonAnnotator() |
| 2849 |
s = a.build_types(f, [inttype]) |
| 2850 |
assert isinstance(s, annmodel.SomeInteger) |
| 2851 |
assert s.knowntype == inttype |
| 2852 |
assert s.unsigned == (inttype(-1) > 0) |
| 2853 |
|
| 2854 |
def test_annotate_rshift(self): |
| 2855 |
def f(x): |
| 2856 |
return x >> 2 |
| 2857 |
a = self.RPythonAnnotator() |
| 2858 |
s = a.build_types(f, [annmodel.SomeInteger(nonneg=True)]) |
| 2859 |
assert isinstance(s, annmodel.SomeInteger) |
| 2860 |
assert s.nonneg |
| 2861 |
|
| 2862 |
def test_prebuilt_mutables(self): |
| 2863 |
class A: |
| 2864 |
pass |
| 2865 |
class B: |
| 2866 |
pass |
| 2867 |
a1 = A() |
| 2868 |
a2 = A() |
| 2869 |
a1.d = {} |
| 2870 |
a2.d = {} |
| 2871 |
a1.l = [] |
| 2872 |
a2.l = [] |
| 2873 |
b = B() |
| 2874 |
b.d1 = a1.d |
| 2875 |
b.d2 = a2.d |
| 2876 |
b.l1 = a1.l |
| 2877 |
b.l2 = a2.l |
| 2878 |
|
| 2879 |
def dmutate(d): |
| 2880 |
d[123] = 321 |
| 2881 |
|
| 2882 |
def lmutate(l): |
| 2883 |
l.append(42) |
| 2884 |
|
| 2885 |
def readout(d, l): |
| 2886 |
return len(d) + len(l) |
| 2887 |
|
| 2888 |
def f(): |
| 2889 |
dmutate(b.d1) |
| 2890 |
dmutate(b.d2) |
| 2891 |
dmutate(a1.d) |
| 2892 |
dmutate(a2.d) |
| 2893 |
lmutate(b.l1) |
| 2894 |
lmutate(b.l2) |
| 2895 |
lmutate(a1.l) |
| 2896 |
lmutate(a2.l) |
| 2897 |
return readout(a1.d, a1.l) + readout(a2.d, a2.l) |
| 2898 |
|
| 2899 |
a = self.RPythonAnnotator() |
| 2900 |
a.build_types(f, []) |
| 2901 |
v1, v2 = graphof(a, readout).getargs() |
| 2902 |
assert not a.binding(v1).is_constant() |
| 2903 |
assert not a.binding(v2).is_constant() |
| 2904 |
|
| 2905 |
def test_prebuilt_mutables_dont_use_eq(self): |
| 2906 |
|
| 2907 |
|
| 2908 |
class Base(object): |
| 2909 |
def __eq__(self, other): |
| 2910 |
if self is other: |
| 2911 |
return True |
| 2912 |
raise ValueError |
| 2913 |
def __hash__(self): |
| 2914 |
return 42 |
| 2915 |
class A(Base): |
| 2916 |
pass |
| 2917 |
class B(Base): |
| 2918 |
pass |
| 2919 |
a1 = A() |
| 2920 |
a2 = B() |
| 2921 |
a1.x = 5 |
| 2922 |
a2.x = 6 |
| 2923 |
|
| 2924 |
def f(): |
| 2925 |
return a1.x + a2.x |
| 2926 |
|
| 2927 |
a = self.RPythonAnnotator() |
| 2928 |
s = a.build_types(f, []) |
| 2929 |
assert s.knowntype == int |
| 2930 |
|
| 2931 |
def test_chr_out_of_bounds(self): |
| 2932 |
def g(n, max): |
| 2933 |
if n < max: |
| 2934 |
return chr(n) |
| 2935 |
else: |
| 2936 |
return '?' |
| 2937 |
def fun(max): |
| 2938 |
v = g(1000, max) |
| 2939 |
return g(ord(v), max) |
| 2940 |
|
| 2941 |
a = self.RPythonAnnotator() |
| 2942 |
s = a.build_types(fun, [int]) |
| 2943 |
assert isinstance(s, annmodel.SomeChar) |
| 2944 |
|
| 2945 |
def test_range_nonneg(self): |
| 2946 |
def fun(n, k): |
| 2947 |
for i in range(n): |
| 2948 |
if k == 17: |
| 2949 |
return i |
| 2950 |
return 0 |
| 2951 |
a = self.RPythonAnnotator() |
| 2952 |
s = a.build_types(fun, [int, int]) |
| 2953 |
assert isinstance(s, annmodel.SomeInteger) |
| 2954 |
assert s.nonneg |
| 2955 |
|
| 2956 |
def test_range_nonneg_variablestep(self): |
| 2957 |
def get_step(n): |
| 2958 |
if n == 1: |
| 2959 |
return 2 |
| 2960 |
else: |
| 2961 |
return 3 |
| 2962 |
def fun(n, k): |
| 2963 |
step = get_step(n) |
| 2964 |
for i in range(0, n, step): |
| 2965 |
if k == 17: |
| 2966 |
return i |
| 2967 |
return 0 |
| 2968 |
a = self.RPythonAnnotator() |
| 2969 |
s = a.build_types(fun, [int, int]) |
| 2970 |
assert isinstance(s, annmodel.SomeInteger) |
| 2971 |
assert s.nonneg |
| 2972 |
|
| 2973 |
def test_reverse_range_nonneg(self): |
| 2974 |
def fun(n, k): |
| 2975 |
for i in range(n-1, -1, -1): |
| 2976 |
if k == 17: |
| 2977 |
return i |
| 2978 |
return 0 |
| 2979 |
a = self.RPythonAnnotator() |
| 2980 |
s = a.build_types(fun, [int, int]) |
| 2981 |
assert isinstance(s, annmodel.SomeInteger) |
| 2982 |
assert s.nonneg |
| 2983 |
|
| 2984 |
def test_sig(self): |
| 2985 |
def fun(x, y): |
| 2986 |
return x+y |
| 2987 |
s_nonneg = annmodel.SomeInteger(nonneg=True) |
| 2988 |
fun._annenforceargs_ = Sig(int, s_nonneg) |
| 2989 |
|
| 2990 |
a = self.RPythonAnnotator() |
| 2991 |
s = a.build_types(fun, [s_nonneg, s_nonneg]) |
| 2992 |
assert isinstance(s, annmodel.SomeInteger) |
| 2993 |
assert not s.nonneg |
| 2994 |
with py.test.raises(SignatureError): |
| 2995 |
a.build_types(fun, [int, int]) |
| 2996 |
|
| 2997 |
def test_sig_simpler(self): |
| 2998 |
def fun(x, y): |
| 2999 |
return x+y |
| 3000 |
s_nonneg = annmodel.SomeInteger(nonneg=True) |
| 3001 |
fun._annenforceargs_ = (int, s_nonneg) |
| 3002 |
|
| 3003 |
a = self.RPythonAnnotator() |
| 3004 |
s = a.build_types(fun, [s_nonneg, s_nonneg]) |
| 3005 |
assert isinstance(s, annmodel.SomeInteger) |
| 3006 |
assert not s.nonneg |
| 3007 |
with py.test.raises(SignatureError): |
| 3008 |
a.build_types(fun, [int, int]) |
| 3009 |
|
| 3010 |
def test_sig_lambda(self): |
| 3011 |
def fun(x, y): |
| 3012 |
return y |
| 3013 |
s_nonneg = annmodel.SomeInteger(nonneg=True) |
| 3014 |
fun._annenforceargs_ = Sig(lambda s1,s2: s1, lambda s1,s2: s1) |
| 3015 |
|
| 3016 |
|
| 3017 |
|
| 3018 |
a = self.RPythonAnnotator() |
| 3019 |
s = a.build_types(fun, [int, s_nonneg]) |
| 3020 |
assert isinstance(s, annmodel.SomeInteger) |
| 3021 |
assert not s.nonneg |
| 3022 |
with py.test.raises(SignatureError): |
| 3023 |
a.build_types(fun, [s_nonneg, int]) |
| 3024 |
|
| 3025 |
def test_sig_bug(self): |
| 3026 |
def g(x, y=5): |
| 3027 |
return y == 5 |
| 3028 |
g._annenforceargs_ = (int, int) |
| 3029 |
def fun(x): |
| 3030 |
return g(x) |
| 3031 |
a = self.RPythonAnnotator() |
| 3032 |
s = a.build_types(fun, [int]) |
| 3033 |
assert s.knowntype is bool |
| 3034 |
assert s.is_constant() |
| 3035 |
|
| 3036 |
def test_sig_list(self): |
| 3037 |
def g(buf): |
| 3038 |
buf.append(5) |
| 3039 |
g._annenforceargs_ = ([int],) |
| 3040 |
def fun(): |
| 3041 |
lst = [] |
| 3042 |
g(lst) |
| 3043 |
return lst[0] |
| 3044 |
a = self.RPythonAnnotator() |
| 3045 |
s = a.build_types(fun, []) |
| 3046 |
assert s.knowntype is int |
| 3047 |
assert not s.is_constant() |
| 3048 |
|
| 3049 |
def test_slots_check(self): |
| 3050 |
class Base(object): |
| 3051 |
__slots__ = 'x' |
| 3052 |
class A(Base): |
| 3053 |
__slots__ = 'y' |
| 3054 |
def m(self): |
| 3055 |
return 65 |
| 3056 |
class C(Base): |
| 3057 |
__slots__ = 'z' |
| 3058 |
def m(self): |
| 3059 |
return 67 |
| 3060 |
for attrname, works in [('x', True), |
| 3061 |
('y', False), |
| 3062 |
('z', False), |
| 3063 |
('t', False)]: |
| 3064 |
def fun(n): |
| 3065 |
if n: o = A() |
| 3066 |
else: o = C() |
| 3067 |
setattr(o, attrname, 12) |
| 3068 |
return o.m() |
| 3069 |
a = self.RPythonAnnotator() |
| 3070 |
if works: |
| 3071 |
a.build_types(fun, [int]) |
| 3072 |
else: |
| 3073 |
with py.test.raises(NoSuchAttrError): |
| 3074 |
a.build_types(fun, [int]) |
| 3075 |
|
| 3076 |
def test_slots_enforce_attrs(self): |
| 3077 |
class Superbase(object): |
| 3078 |
__slots__ = 'x' |
| 3079 |
class Base(Superbase): |
| 3080 |
pass |
| 3081 |
class A(Base): |
| 3082 |
pass |
| 3083 |
class B(Base): |
| 3084 |
pass |
| 3085 |
def fun(s): |
| 3086 |
if s is None: |
| 3087 |
o = B() |
| 3088 |
o.x = 12 |
| 3089 |
elif len(s) > 5: |
| 3090 |
o = A() |
| 3091 |
else: |
| 3092 |
o = Base() |
| 3093 |
return o.x |
| 3094 |
a = self.RPythonAnnotator() |
| 3095 |
s = a.build_types(fun, [str]) |
| 3096 |
assert s == annmodel.s_ImpossibleValue |
| 3097 |
|
| 3098 |
def test_enforced_attrs_check(self): |
| 3099 |
class Base(object): |
| 3100 |
_attrs_ = 'x' |
| 3101 |
class A(Base): |
| 3102 |
_attrs_ = 'y' |
| 3103 |
def m(self): |
| 3104 |
return 65 |
| 3105 |
class C(Base): |
| 3106 |
_attrs_ = 'z' |
| 3107 |
def m(self): |
| 3108 |
return 67 |
| 3109 |
for attrname, works in [('x', True), |
| 3110 |
('y', False), |
| 3111 |
('z', False), |
| 3112 |
('t', False)]: |
| 3113 |
def fun(n): |
| 3114 |
if n: o = A() |
| 3115 |
else: o = C() |
| 3116 |
setattr(o, attrname, 12) |
| 3117 |
return o.m() |
| 3118 |
a = self.RPythonAnnotator() |
| 3119 |
if works: |
| 3120 |
a.build_types(fun, [int]) |
| 3121 |
else: |
| 3122 |
py.test.raises(NoSuchAttrError, a.build_types, fun, [int]) |
| 3123 |
|
| 3124 |
def test_attrs_enforce_attrs(self): |
| 3125 |
class Superbase(object): |
| 3126 |
_attrs_ = 'x' |
| 3127 |
class Base(Superbase): |
| 3128 |
pass |
| 3129 |
class A(Base): |
| 3130 |
pass |
| 3131 |
class B(Base): |
| 3132 |
pass |
| 3133 |
def fun(s): |
| 3134 |
if s is None: |
| 3135 |
o = B() |
| 3136 |
o.x = 12 |
| 3137 |
elif len(s) > 5: |
| 3138 |
o = A() |
| 3139 |
else: |
| 3140 |
o = Base() |
| 3141 |
return o.x |
| 3142 |
a = self.RPythonAnnotator() |
| 3143 |
s = a.build_types(fun, [str]) |
| 3144 |
assert s == annmodel.s_ImpossibleValue |
| 3145 |
|
| 3146 |
|
| 3147 |
def test_pbc_enforce_attrs(self): |
| 3148 |
class F(object): |
| 3149 |
_attrs_ = ['foo',] |
| 3150 |
|
| 3151 |
def _freeze_(self): |
| 3152 |
return True |
| 3153 |
|
| 3154 |
p1 = F() |
| 3155 |
p2 = F() |
| 3156 |
|
| 3157 |
def g(): pass |
| 3158 |
|
| 3159 |
def f(x): |
| 3160 |
if x: |
| 3161 |
p = p1 |
| 3162 |
else: |
| 3163 |
p = p2 |
| 3164 |
g() |
| 3165 |
return p.foo |
| 3166 |
|
| 3167 |
a = self.RPythonAnnotator() |
| 3168 |
a.build_types(f, [bool]) |
| 3169 |
|
| 3170 |
def test_float_cmp(self): |
| 3171 |
def fun(x, y): |
| 3172 |
return (x < y, |
| 3173 |
x <= y, |
| 3174 |
x == y, |
| 3175 |
x != y, |
| 3176 |
x > y, |
| 3177 |
x >= y) |
| 3178 |
|
| 3179 |
a = self.RPythonAnnotator(policy=AnnotatorPolicy()) |
| 3180 |
s = a.build_types(fun, [float, float]) |
| 3181 |
assert [s_item.knowntype for s_item in s.items] == [bool] * 6 |
| 3182 |
|
| 3183 |
def test_empty_range(self): |
| 3184 |
def g(lst): |
| 3185 |
total = 0 |
| 3186 |
for i in range(len(lst)): |
| 3187 |
total += lst[i] |
| 3188 |
return total |
| 3189 |
def fun(): |
| 3190 |
return g([]) |
| 3191 |
|
| 3192 |
a = self.RPythonAnnotator(policy=AnnotatorPolicy()) |
| 3193 |
s = a.build_types(fun, []) |
| 3194 |
assert s.const == 0 |
| 3195 |
|
| 3196 |
def test_compare_int_bool(self): |
| 3197 |
def fun(x): |
| 3198 |
return 50 < x |
| 3199 |
a = self.RPythonAnnotator(policy=AnnotatorPolicy()) |
| 3200 |
s = a.build_types(fun, [bool]) |
| 3201 |
assert isinstance(s, annmodel.SomeBool) |
| 3202 |
|
| 3203 |
def test_long_as_intermediate_value(self): |
| 3204 |
from sys import maxint |
| 3205 |
from rpython.rlib.rarithmetic import intmask |
| 3206 |
def fun(x): |
| 3207 |
if x > 0: |
| 3208 |
v = maxint |
| 3209 |
else: |
| 3210 |
v = -maxint |
| 3211 |
return intmask(v * 10) |
| 3212 |
P = AnnotatorPolicy() |
| 3213 |
a = self.RPythonAnnotator(policy=P) |
| 3214 |
s = a.build_types(fun, [bool]) |
| 3215 |
assert isinstance(s, annmodel.SomeInteger) |
| 3216 |
|
| 3217 |
def test_instance_with_flags(self): |
| 3218 |
from rpython.rlib.jit import hint |
| 3219 |
|
| 3220 |
class A: |
| 3221 |
_virtualizable_ = [] |
| 3222 |
class B(A): |
| 3223 |
def meth(self): |
| 3224 |
return self |
| 3225 |
class C(A): |
| 3226 |
def meth(self): |
| 3227 |
return self |
| 3228 |
|
| 3229 |
def f(n): |
| 3230 |
x = B() |
| 3231 |
x = hint(x, access_directly=True) |
| 3232 |
m = x.meth |
| 3233 |
for i in range(n): |
| 3234 |
x = C() |
| 3235 |
m = x.meth |
| 3236 |
return x, m, m() |
| 3237 |
|
| 3238 |
a = self.RPythonAnnotator() |
| 3239 |
s = a.build_types(f, [a.bookkeeper.immutablevalue(0)]) |
| 3240 |
assert isinstance(s.items[0], annmodel.SomeInstance) |
| 3241 |
assert s.items[0].flags == {'access_directly': True} |
| 3242 |
assert isinstance(s.items[1], annmodel.SomePBC) |
| 3243 |
assert len(s.items[1].descriptions) == 1 |
| 3244 |
assert s.items[1].any_description().flags == {'access_directly': |
| 3245 |
True} |
| 3246 |
assert isinstance(s.items[2], annmodel.SomeInstance) |
| 3247 |
assert s.items[2].flags == {'access_directly': True} |
| 3248 |
|
| 3249 |
a = self.RPythonAnnotator() |
| 3250 |
s = a.build_types(f, [int]) |
| 3251 |
assert isinstance(s.items[0], annmodel.SomeInstance) |
| 3252 |
assert s.items[0].flags == {} |
| 3253 |
assert isinstance(s.items[1], annmodel.SomePBC) |
| 3254 |
assert isinstance(s.items[2], annmodel.SomeInstance) |
| 3255 |
assert s.items[2].flags == {} |
| 3256 |
|
| 3257 |
@py.test.mark.xfail |
| 3258 |
def test_no_access_directly_on_heap(self): |
| 3259 |
from rpython.rlib.jit import hint |
| 3260 |
|
| 3261 |
class A: |
| 3262 |
_virtualizable_ = [] |
| 3263 |
|
| 3264 |
class I: |
| 3265 |
pass |
| 3266 |
|
| 3267 |
def f(): |
| 3268 |
x = A() |
| 3269 |
x = hint(x, access_directly=True) |
| 3270 |
i = I() |
| 3271 |
i.x = x |
| 3272 |
|
| 3273 |
a = self.RPythonAnnotator() |
| 3274 |
with py.test.raises(AnnotatorError): |
| 3275 |
a.build_types(f, []) |
| 3276 |
|
| 3277 |
|
| 3278 |
class M: |
| 3279 |
def __init__(self): |
| 3280 |
self.l = [] |
| 3281 |
self.d = {} |
| 3282 |
|
| 3283 |
class C: |
| 3284 |
def _freeze_(self): |
| 3285 |
return True |
| 3286 |
|
| 3287 |
def __init__(self): |
| 3288 |
self.m = M() |
| 3289 |
self.l2 = [] |
| 3290 |
|
| 3291 |
c = C() |
| 3292 |
|
| 3293 |
def f(): |
| 3294 |
x = A() |
| 3295 |
x = hint(x, access_directly=True) |
| 3296 |
c.m.l.append(x) |
| 3297 |
|
| 3298 |
a = self.RPythonAnnotator() |
| 3299 |
py.test.raises(AnnotatorError, a.build_types, f, []) |
| 3300 |
|
| 3301 |
def f(): |
| 3302 |
x = A() |
| 3303 |
x = hint(x, access_directly=True) |
| 3304 |
c.m.d[None] = x |
| 3305 |
|
| 3306 |
a = self.RPythonAnnotator() |
| 3307 |
py.test.raises(AnnotatorError, a.build_types, f, []) |
| 3308 |
|
| 3309 |
def f(): |
| 3310 |
x = A() |
| 3311 |
x = hint(x, access_directly=True) |
| 3312 |
c.m.d[x] = None |
| 3313 |
|
| 3314 |
a = self.RPythonAnnotator() |
| 3315 |
py.test.raises(AnnotatorError, a.build_types, f, []) |
| 3316 |
|
| 3317 |
def test_access_direct_eq_False(self): |
| 3318 |
from rpython.rlib import jit, debug |
| 3319 |
class Root: |
| 3320 |
def f(self): |
| 3321 |
debug.check_not_access_directly(self) |
| 3322 |
class Frame(Root): |
| 3323 |
_virtualizable_ = [] |
| 3324 |
def meth(self): |
| 3325 |
self = jit.hint(self, access_directly=False) |
| 3326 |
return self.f() |
| 3327 |
class C(Root): |
| 3328 |
def meth(self): |
| 3329 |
return self.f() |
| 3330 |
|
| 3331 |
def f(n): |
| 3332 |
if n == 0: |
| 3333 |
x = Frame() |
| 3334 |
x = jit.hint(x, access_directly=True) |
| 3335 |
else: |
| 3336 |
x = C() |
| 3337 |
x.meth() |
| 3338 |
a = self.RPythonAnnotator() |
| 3339 |
a.build_types(f, [int]) |
| 3340 |
|
| 3341 |
def test_access_direct_no_virtualizable(self): |
| 3342 |
from rpython.rlib import jit, debug |
| 3343 |
class Root: |
| 3344 |
def f(self): |
| 3345 |
debug.check_not_access_directly(self) |
| 3346 |
class Frame(Root): |
| 3347 |
|
| 3348 |
def meth(self): |
| 3349 |
return self.f() |
| 3350 |
class C(Root): |
| 3351 |
def meth(self): |
| 3352 |
return self.f() |
| 3353 |
|
| 3354 |
def f(n): |
| 3355 |
if n == 0: |
| 3356 |
x = Frame() |
| 3357 |
x = jit.hint(x, access_directly=True, fresh_virtualizable=True) |
| 3358 |
x.foo = 1 |
| 3359 |
else: |
| 3360 |
x = C() |
| 3361 |
x.meth() |
| 3362 |
a = self.RPythonAnnotator() |
| 3363 |
a.build_types(f, [int]) |
| 3364 |
|
| 3365 |
def test_weakref(self): |
| 3366 |
import weakref |
| 3367 |
|
| 3368 |
class A: |
| 3369 |
pass |
| 3370 |
class B(A): |
| 3371 |
pass |
| 3372 |
class C(A): |
| 3373 |
pass |
| 3374 |
|
| 3375 |
def f(n): |
| 3376 |
if n: |
| 3377 |
b = B() |
| 3378 |
b.hello = 42 |
| 3379 |
r = weakref.ref(b) |
| 3380 |
else: |
| 3381 |
c = C() |
| 3382 |
c.hello = 64 |
| 3383 |
r = weakref.ref(c) |
| 3384 |
return r().hello |
| 3385 |
a = self.RPythonAnnotator() |
| 3386 |
s = a.build_types(f, [int]) |
| 3387 |
assert isinstance(s, annmodel.SomeInteger) |
| 3388 |
assert not s.is_constant() |
| 3389 |
|
| 3390 |
def test_float_pow_unsupported(self): |
| 3391 |
def f(x, y): |
| 3392 |
x **= y |
| 3393 |
return x ** y |
| 3394 |
a = self.RPythonAnnotator() |
| 3395 |
py.test.raises(FlowingError, a.build_types, f, [int, int]) |
| 3396 |
a = self.RPythonAnnotator() |
| 3397 |
py.test.raises(FlowingError, a.build_types, f, [float, float]) |
| 3398 |
|
| 3399 |
def test_intcmp_bug(self): |
| 3400 |
def g(x, y): |
| 3401 |
return x <= y |
| 3402 |
def f(x, y): |
| 3403 |
if g(x, y): |
| 3404 |
g(x, r_uint(y)) |
| 3405 |
a = self.RPythonAnnotator() |
| 3406 |
with py.test.raises(UnionError): |
| 3407 |
a.build_types(f, [int, int]) |
| 3408 |
|
| 3409 |
def test_compare_with_zero(self): |
| 3410 |
def g(): |
| 3411 |
should_not_see_this |
| 3412 |
def f(n): |
| 3413 |
assert n >= 0 |
| 3414 |
if n < 0: |
| 3415 |
g() |
| 3416 |
if not (n >= 0): |
| 3417 |
g() |
| 3418 |
a = self.RPythonAnnotator() |
| 3419 |
a.build_types(f, [int]) |
| 3420 |
|
| 3421 |
def test_r_singlefloat(self): |
| 3422 |
z = r_singlefloat(0.4) |
| 3423 |
def g(n): |
| 3424 |
if n > 0: |
| 3425 |
return r_singlefloat(n * 0.1) |
| 3426 |
else: |
| 3427 |
return z |
| 3428 |
a = self.RPythonAnnotator() |
| 3429 |
s = a.build_types(g, [int]) |
| 3430 |
assert isinstance(s, annmodel.SomeSingleFloat) |
| 3431 |
|
| 3432 |
def test_unicode_simple(self): |
| 3433 |
def f(): |
| 3434 |
return u'xxx' |
| 3435 |
|
| 3436 |
a = self.RPythonAnnotator() |
| 3437 |
s = a.build_types(f, []) |
| 3438 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3439 |
|
| 3440 |
def test_unicode(self): |
| 3441 |
def g(n): |
| 3442 |
if n > 0: |
| 3443 |
return unichr(1234) |
| 3444 |
else: |
| 3445 |
return u"x\xe4x" |
| 3446 |
|
| 3447 |
def f(n): |
| 3448 |
x = g(0) |
| 3449 |
return x[n] |
| 3450 |
|
| 3451 |
a = self.RPythonAnnotator() |
| 3452 |
s = a.build_types(g, [int]) |
| 3453 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3454 |
a = self.RPythonAnnotator() |
| 3455 |
s = a.build_types(f, [int]) |
| 3456 |
assert isinstance(s, annmodel.SomeUnicodeCodePoint) |
| 3457 |
|
| 3458 |
def test_unicode_from_string(self): |
| 3459 |
def f(x): |
| 3460 |
return unicode(x) |
| 3461 |
|
| 3462 |
a = self.RPythonAnnotator() |
| 3463 |
s = a.build_types(f, [str]) |
| 3464 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3465 |
|
| 3466 |
def test_unicode_add(self): |
| 3467 |
def f(x): |
| 3468 |
return unicode(x) + unichr(1234) |
| 3469 |
|
| 3470 |
def g(x): |
| 3471 |
return unichr(x) + unichr(2) |
| 3472 |
|
| 3473 |
a = self.RPythonAnnotator() |
| 3474 |
s = a.build_types(f, [str]) |
| 3475 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3476 |
a = self.RPythonAnnotator() |
| 3477 |
s = a.build_types(f, [int]) |
| 3478 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3479 |
|
| 3480 |
def test_unicode_startswith(self): |
| 3481 |
def f(x): |
| 3482 |
return u'xxxx'.replace(x, u'z') |
| 3483 |
|
| 3484 |
a = self.RPythonAnnotator() |
| 3485 |
s = a.build_types(f, [unicode]) |
| 3486 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3487 |
|
| 3488 |
def test_unicode_buildtypes(self): |
| 3489 |
def f(x): |
| 3490 |
return x |
| 3491 |
|
| 3492 |
a = self.RPythonAnnotator() |
| 3493 |
s = a.build_types(f, [unicode]) |
| 3494 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3495 |
|
| 3496 |
def test_replace_annotations(self): |
| 3497 |
def f(x): |
| 3498 |
return 'a'.replace(x, 'b') |
| 3499 |
|
| 3500 |
a = self.RPythonAnnotator() |
| 3501 |
s = a.build_types(f, [str]) |
| 3502 |
assert isinstance(s, annmodel.SomeString) |
| 3503 |
assert s.no_nul |
| 3504 |
|
| 3505 |
def f(x): |
| 3506 |
return u'a'.replace(x, u'b') |
| 3507 |
|
| 3508 |
a = self.RPythonAnnotator() |
| 3509 |
s = a.build_types(f, [unicode]) |
| 3510 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3511 |
assert s.no_nul |
| 3512 |
|
| 3513 |
def test_strformatting_unicode(self): |
| 3514 |
def f(x): |
| 3515 |
return '%s' % unichr(x) |
| 3516 |
|
| 3517 |
a = self.RPythonAnnotator() |
| 3518 |
py.test.raises(AnnotatorError, a.build_types, f, [int]) |
| 3519 |
def f(x): |
| 3520 |
return '%s' % (unichr(x) * 3) |
| 3521 |
|
| 3522 |
a = self.RPythonAnnotator() |
| 3523 |
py.test.raises(AnnotatorError, a.build_types, f, [int]) |
| 3524 |
def f(x): |
| 3525 |
return '%s%s' % (1, unichr(x)) |
| 3526 |
|
| 3527 |
a = self.RPythonAnnotator() |
| 3528 |
py.test.raises(AnnotatorError, a.build_types, f, [int]) |
| 3529 |
def f(x): |
| 3530 |
return '%s%s' % (1, unichr(x) * 15) |
| 3531 |
|
| 3532 |
a = self.RPythonAnnotator() |
| 3533 |
py.test.raises(AnnotatorError, a.build_types, f, [int]) |
| 3534 |
|
| 3535 |
|
| 3536 |
def test_strformatting_tuple(self): |
| 3537 |
""" |
| 3538 |
A function which returns the result of interpolating a tuple of a |
| 3539 |
single str into a str format string should be annotated as returning |
| 3540 |
SomeString. |
| 3541 |
""" |
| 3542 |
def f(x): |
| 3543 |
return '%s' % (x,) |
| 3544 |
|
| 3545 |
a = self.RPythonAnnotator() |
| 3546 |
s = a.build_types(f, [str]) |
| 3547 |
assert isinstance(s, annmodel.SomeString) |
| 3548 |
|
| 3549 |
def test_unicodeformatting(self): |
| 3550 |
def f(x): |
| 3551 |
return u'%s' % x |
| 3552 |
|
| 3553 |
a = self.RPythonAnnotator() |
| 3554 |
s = a.build_types(f, [unicode]) |
| 3555 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3556 |
|
| 3557 |
def test_unicodeformatting_tuple(self): |
| 3558 |
def f(x): |
| 3559 |
return u'%s' % (x,) |
| 3560 |
|
| 3561 |
a = self.RPythonAnnotator() |
| 3562 |
s = a.build_types(f, [unicode]) |
| 3563 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 3564 |
|
| 3565 |
def test_extended_slice(self): |
| 3566 |
a = self.RPythonAnnotator() |
| 3567 |
def f(start, end, step): |
| 3568 |
return [1, 2, 3][start:end:step] |
| 3569 |
with py.test.raises(AnnotatorError): |
| 3570 |
a.build_types(f, [int, int, int]) |
| 3571 |
a = self.RPythonAnnotator() |
| 3572 |
with py.test.raises(AnnotatorError): |
| 3573 |
a.build_types(f, [annmodel.SomeInteger(nonneg=True), |
| 3574 |
annmodel.SomeInteger(nonneg=True), |
| 3575 |
annmodel.SomeInteger(nonneg=True)]) |
| 3576 |
def f(x): |
| 3577 |
return x[::-1] |
| 3578 |
a = self.RPythonAnnotator() |
| 3579 |
with py.test.raises(AnnotatorError): |
| 3580 |
a.build_types(f, [str]) |
| 3581 |
def f(x): |
| 3582 |
return x[::2] |
| 3583 |
a = self.RPythonAnnotator() |
| 3584 |
with py.test.raises(AnnotatorError): |
| 3585 |
a.build_types(f, [str]) |
| 3586 |
def f(x): |
| 3587 |
return x[1:2:1] |
| 3588 |
a = self.RPythonAnnotator() |
| 3589 |
with py.test.raises(AnnotatorError): |
| 3590 |
a.build_types(f, [str]) |
| 3591 |
|
| 3592 |
def test_negative_slice(self): |
| 3593 |
def f(s, e): |
| 3594 |
return [1, 2, 3][s:e] |
| 3595 |
|
| 3596 |
a = self.RPythonAnnotator() |
| 3597 |
py.test.raises(AnnotatorError, "a.build_types(f, [int, int])") |
| 3598 |
a.build_types(f, [annmodel.SomeInteger(nonneg=True), |
| 3599 |
annmodel.SomeInteger(nonneg=True)]) |
| 3600 |
def f(x): |
| 3601 |
return x[:-1] |
| 3602 |
|
| 3603 |
a.build_types(f, [str]) |
| 3604 |
|
| 3605 |
def test_negative_number_find(self): |
| 3606 |
def f(s, e): |
| 3607 |
return "xyz".find("x", s, e) |
| 3608 |
|
| 3609 |
a = self.RPythonAnnotator() |
| 3610 |
py.test.raises(AnnotatorError, "a.build_types(f, [int, int])") |
| 3611 |
a.build_types(f, [annmodel.SomeInteger(nonneg=True), |
| 3612 |
annmodel.SomeInteger(nonneg=True)]) |
| 3613 |
def f(s, e): |
| 3614 |
return "xyz".rfind("x", s, e) |
| 3615 |
|
| 3616 |
py.test.raises(AnnotatorError, "a.build_types(f, [int, int])") |
| 3617 |
a.build_types(f, [annmodel.SomeInteger(nonneg=True), |
| 3618 |
annmodel.SomeInteger(nonneg=True)]) |
| 3619 |
|
| 3620 |
def f(s, e): |
| 3621 |
return "xyz".count("x", s, e) |
| 3622 |
|
| 3623 |
py.test.raises(AnnotatorError, "a.build_types(f, [int, int])") |
| 3624 |
a.build_types(f, [annmodel.SomeInteger(nonneg=True), |
| 3625 |
annmodel.SomeInteger(nonneg=True)]) |
| 3626 |
|
| 3627 |
|
| 3628 |
def test_setslice(self): |
| 3629 |
def f(): |
| 3630 |
lst = [2, 5, 7] |
| 3631 |
lst[1:2] = [4] |
| 3632 |
return lst |
| 3633 |
|
| 3634 |
a = self.RPythonAnnotator() |
| 3635 |
s = a.build_types(f, []) |
| 3636 |
assert isinstance(s, annmodel.SomeList) |
| 3637 |
assert s.listdef.listitem.resized |
| 3638 |
assert not s.listdef.listitem.immutable |
| 3639 |
assert s.listdef.listitem.mutated |
| 3640 |
|
| 3641 |
def test_delslice(self): |
| 3642 |
def f(): |
| 3643 |
lst = [2, 5, 7] |
| 3644 |
del lst[1:2] |
| 3645 |
return lst |
| 3646 |
|
| 3647 |
a = self.RPythonAnnotator() |
| 3648 |
s = a.build_types(f, []) |
| 3649 |
assert isinstance(s, annmodel.SomeList) |
| 3650 |
assert s.listdef.listitem.resized |
| 3651 |
|
| 3652 |
def test_varargs(self): |
| 3653 |
def f(*args): |
| 3654 |
return args[0] + 42 |
| 3655 |
a = self.RPythonAnnotator() |
| 3656 |
s = a.build_types(f, [int, int]) |
| 3657 |
assert isinstance(s, annmodel.SomeInteger) |
| 3658 |
|
| 3659 |
def test_listitem_no_mutating(self): |
| 3660 |
from rpython.rlib.debug import check_annotation |
| 3661 |
called = [] |
| 3662 |
|
| 3663 |
def checker(ann, bk): |
| 3664 |
called.append(True) |
| 3665 |
assert not ann.listdef.listitem.mutated |
| 3666 |
ann.listdef.never_resize() |
| 3667 |
|
| 3668 |
def f(): |
| 3669 |
l = [1,2,3] |
| 3670 |
check_annotation(l, checker) |
| 3671 |
return l |
| 3672 |
|
| 3673 |
def g(): |
| 3674 |
l = f() |
| 3675 |
l.append(4) |
| 3676 |
|
| 3677 |
a = self.RPythonAnnotator() |
| 3678 |
py.test.raises(ListChangeUnallowed, a.build_types, g, []) |
| 3679 |
assert called |
| 3680 |
|
| 3681 |
def test_listitem_no_mutating2(self): |
| 3682 |
from rpython.rlib.debug import make_sure_not_resized |
| 3683 |
|
| 3684 |
def f(): |
| 3685 |
return make_sure_not_resized([1,2,3]) |
| 3686 |
|
| 3687 |
def g(): |
| 3688 |
l = [1,2,3] |
| 3689 |
l.append(4) |
| 3690 |
return l |
| 3691 |
|
| 3692 |
def fn(i): |
| 3693 |
if i: |
| 3694 |
func = f |
| 3695 |
else: |
| 3696 |
func = g |
| 3697 |
return func() |
| 3698 |
|
| 3699 |
a = self.RPythonAnnotator() |
| 3700 |
a.translator.config.translation.list_comprehension_operations = True |
| 3701 |
py.test.raises(ListChangeUnallowed, a.build_types, fn, [int]) |
| 3702 |
|
| 3703 |
def test_listitem_never_resize(self): |
| 3704 |
from rpython.rlib.debug import check_annotation |
| 3705 |
|
| 3706 |
def checker(ann, bk): |
| 3707 |
ann.listdef.never_resize() |
| 3708 |
|
| 3709 |
def f(): |
| 3710 |
l = [1,2,3] |
| 3711 |
l.append(4) |
| 3712 |
check_annotation(l, checker) |
| 3713 |
|
| 3714 |
a = self.RPythonAnnotator() |
| 3715 |
py.test.raises(ListChangeUnallowed, a.build_types, f, []) |
| 3716 |
|
| 3717 |
|
| 3718 |
def test_len_of_empty_list(self): |
| 3719 |
class X: |
| 3720 |
pass |
| 3721 |
def f(n): |
| 3722 |
x = X() |
| 3723 |
x.lst = None |
| 3724 |
if n < 0: |
| 3725 |
return len(x.lst) |
| 3726 |
x.lst = [] |
| 3727 |
return len(x.lst) |
| 3728 |
a = self.RPythonAnnotator() |
| 3729 |
s = a.build_types(f, [int]) |
| 3730 |
assert s.const == 0 |
| 3731 |
|
| 3732 |
def test_contains_of_empty_dict(self): |
| 3733 |
class A(object): |
| 3734 |
def meth(self): |
| 3735 |
return 1 |
| 3736 |
|
| 3737 |
def g(x, y): |
| 3738 |
d1 = {} |
| 3739 |
for i in range(y): |
| 3740 |
if x in d1: |
| 3741 |
return d1[x].meth() |
| 3742 |
d1[i+1] = A() |
| 3743 |
return 0 |
| 3744 |
|
| 3745 |
a = self.RPythonAnnotator() |
| 3746 |
s = a.build_types(g, [int, int]) |
| 3747 |
assert s.knowntype is int |
| 3748 |
|
| 3749 |
def f(x): |
| 3750 |
d0 = {} |
| 3751 |
if x in d0: |
| 3752 |
d0[x].meth() |
| 3753 |
return x+1 |
| 3754 |
|
| 3755 |
a = self.RPythonAnnotator() |
| 3756 |
s = a.build_types(f, [int]) |
| 3757 |
assert s.knowntype is int |
| 3758 |
|
| 3759 |
def test_relax(self): |
| 3760 |
def f(*args): |
| 3761 |
return args[0] + args[1] |
| 3762 |
f.relax_sig_check = True |
| 3763 |
def g(x): |
| 3764 |
return f(x, x - x) |
| 3765 |
a = self.RPythonAnnotator() |
| 3766 |
s = a.build_types(g, [int]) |
| 3767 |
assert a.bookkeeper.getdesc(f).getuniquegraph() |
| 3768 |
|
| 3769 |
def test_cannot_raise_ll_exception(self): |
| 3770 |
from rpython.rtyper.annlowlevel import cast_instance_to_base_ptr |
| 3771 |
|
| 3772 |
def f(): |
| 3773 |
e = OverflowError() |
| 3774 |
lle = cast_instance_to_base_ptr(e) |
| 3775 |
raise Exception(lle) |
| 3776 |
|
| 3777 |
a = self.RPythonAnnotator() |
| 3778 |
with py.test.raises(AssertionError): |
| 3779 |
a.build_types(f, []) |
| 3780 |
|
| 3781 |
def test_enumerate(self): |
| 3782 |
def f(): |
| 3783 |
for i, x in enumerate(['a', 'b', 'c', 'd']): |
| 3784 |
if i == 2: |
| 3785 |
return x |
| 3786 |
return '?' |
| 3787 |
a = self.RPythonAnnotator() |
| 3788 |
s = a.build_types(f, []) |
| 3789 |
assert isinstance(s, annmodel.SomeChar) |
| 3790 |
|
| 3791 |
def test_enumerate_startindex(self): |
| 3792 |
def f(): |
| 3793 |
for i, x in enumerate(['a', 'b', 'c', 'd'], 5): |
| 3794 |
if i == 7: |
| 3795 |
return x |
| 3796 |
return '?' |
| 3797 |
a = self.RPythonAnnotator() |
| 3798 |
s = a.build_types(f, []) |
| 3799 |
assert isinstance(s, annmodel.SomeChar) |
| 3800 |
|
| 3801 |
def test_context_manager(self): |
| 3802 |
class C: |
| 3803 |
def __init__(self): |
| 3804 |
pass |
| 3805 |
def __enter__(self): |
| 3806 |
self.x = 1 |
| 3807 |
def __exit__(self, *args): |
| 3808 |
self.x = 3 |
| 3809 |
def f(): |
| 3810 |
c = C() |
| 3811 |
with c: |
| 3812 |
pass |
| 3813 |
return c.x |
| 3814 |
|
| 3815 |
a = self.RPythonAnnotator() |
| 3816 |
s = a.build_types(f, []) |
| 3817 |
assert isinstance(s, annmodel.SomeInteger) |
| 3818 |
|
| 3819 |
assert not s.is_constant() |
| 3820 |
|
| 3821 |
def test_make_sure_not_resized(self): |
| 3822 |
from rpython.rlib.debug import make_sure_not_resized |
| 3823 |
|
| 3824 |
def pycode(consts): |
| 3825 |
make_sure_not_resized(consts) |
| 3826 |
def build1(): |
| 3827 |
return pycode(consts=[1]) |
| 3828 |
def build2(): |
| 3829 |
return pycode(consts=[0]) |
| 3830 |
def fn(): |
| 3831 |
build1() |
| 3832 |
build2() |
| 3833 |
|
| 3834 |
a = self.RPythonAnnotator() |
| 3835 |
a.translator.config.translation.list_comprehension_operations = True |
| 3836 |
a.build_types(fn, []) |
| 3837 |
|
| 3838 |
|
| 3839 |
def test_return_immutable_list(self): |
| 3840 |
class A: |
| 3841 |
_immutable_fields_ = ['lst[*]'] |
| 3842 |
def f(n): |
| 3843 |
a = A() |
| 3844 |
l1 = [n, 0] |
| 3845 |
l1[1] = n+1 |
| 3846 |
a.lst = l1 |
| 3847 |
return a.lst |
| 3848 |
|
| 3849 |
a = self.RPythonAnnotator() |
| 3850 |
s = a.build_types(f, [int]) |
| 3851 |
assert s.listdef.listitem.immutable |
| 3852 |
|
| 3853 |
def test_return_immutable_list_quasiimmut_field(self): |
| 3854 |
class A: |
| 3855 |
_immutable_fields_ = ['lst?[*]'] |
| 3856 |
def f(n): |
| 3857 |
a = A() |
| 3858 |
l1 = [n, 0] |
| 3859 |
l1[1] = n+1 |
| 3860 |
a.lst = l1 |
| 3861 |
return a.lst |
| 3862 |
|
| 3863 |
a = self.RPythonAnnotator() |
| 3864 |
s = a.build_types(f, [int]) |
| 3865 |
assert s.listdef.listitem.immutable |
| 3866 |
|
| 3867 |
def test_immutable_list_is_actually_resized(self): |
| 3868 |
class A: |
| 3869 |
_immutable_fields_ = ['lst[*]'] |
| 3870 |
def f(n): |
| 3871 |
a = A() |
| 3872 |
l1 = [n] |
| 3873 |
l1.append(n+1) |
| 3874 |
a.lst = l1 |
| 3875 |
return a.lst |
| 3876 |
|
| 3877 |
a = self.RPythonAnnotator() |
| 3878 |
py.test.raises(ListChangeUnallowed, a.build_types, f, [int]) |
| 3879 |
|
| 3880 |
def test_immutable_list_is_assigned_a_resizable_list(self): |
| 3881 |
class A: |
| 3882 |
_immutable_fields_ = ['lst[*]'] |
| 3883 |
def f(n): |
| 3884 |
a = A() |
| 3885 |
foo = [] |
| 3886 |
foo.append(n) |
| 3887 |
a.lst = foo |
| 3888 |
|
| 3889 |
a = self.RPythonAnnotator() |
| 3890 |
py.test.raises(ListChangeUnallowed, a.build_types, f, [int]) |
| 3891 |
|
| 3892 |
def test_can_merge_immutable_list_with_regular_list(self): |
| 3893 |
class A: |
| 3894 |
_immutable_fields_ = ['lst[*]'] |
| 3895 |
def foo(lst): |
| 3896 |
pass |
| 3897 |
|
| 3898 |
def f(n): |
| 3899 |
a = A() |
| 3900 |
l1 = [n, 0] |
| 3901 |
l1[1] = n+1 |
| 3902 |
a.lst = l1 |
| 3903 |
if n > 0: |
| 3904 |
foo(a.lst) |
| 3905 |
else: |
| 3906 |
lst = [0] |
| 3907 |
lst[0] = n |
| 3908 |
foo(lst) |
| 3909 |
|
| 3910 |
a = self.RPythonAnnotator() |
| 3911 |
a.build_types(f, [int]) |
| 3912 |
|
| 3913 |
def f(n): |
| 3914 |
a = A() |
| 3915 |
l1 = [n, 0] |
| 3916 |
l1[1] = n+1 |
| 3917 |
a.lst = l1 |
| 3918 |
if n > 0: |
| 3919 |
lst = [0] |
| 3920 |
lst[0] = n |
| 3921 |
foo(lst) |
| 3922 |
else: |
| 3923 |
foo(a.lst) |
| 3924 |
|
| 3925 |
a = self.RPythonAnnotator() |
| 3926 |
a.build_types(f, [int]) |
| 3927 |
|
| 3928 |
def test_immutable_field_subclass(self): |
| 3929 |
class Root: |
| 3930 |
pass |
| 3931 |
class A(Root): |
| 3932 |
_immutable_fields_ = ['_my_lst[*]'] |
| 3933 |
def __init__(self, lst): |
| 3934 |
self._my_lst = lst |
| 3935 |
def foo(x): |
| 3936 |
return len(x._my_lst) |
| 3937 |
|
| 3938 |
def f(n): |
| 3939 |
foo(A([2, n])) |
| 3940 |
foo(Root()) |
| 3941 |
|
| 3942 |
a = self.RPythonAnnotator() |
| 3943 |
e = py.test.raises(Exception, a.build_types, f, [int]) |
| 3944 |
assert "field '_my_lst' was migrated" in str(e.value) |
| 3945 |
|
| 3946 |
def test_range_variable_step(self): |
| 3947 |
def g(n): |
| 3948 |
return range(0, 10, n) |
| 3949 |
def f(n): |
| 3950 |
r = g(1) |
| 3951 |
s = g(n) |
| 3952 |
return r |
| 3953 |
a = self.RPythonAnnotator() |
| 3954 |
s = a.build_types(f, [int]) |
| 3955 |
assert s.listdef.listitem.range_step == 0 |
| 3956 |
|
| 3957 |
def test_specialize_arg_memo(self): |
| 3958 |
@objectmodel.specialize.memo() |
| 3959 |
def g(n): |
| 3960 |
return n |
| 3961 |
@objectmodel.specialize.arg(0) |
| 3962 |
def f(i): |
| 3963 |
return g(i) |
| 3964 |
def main(i): |
| 3965 |
if i == 2: |
| 3966 |
return f(2) |
| 3967 |
elif i == 3: |
| 3968 |
return f(3) |
| 3969 |
else: |
| 3970 |
raise NotImplementedError |
| 3971 |
|
| 3972 |
a = self.RPythonAnnotator() |
| 3973 |
s = a.build_types(main, [int]) |
| 3974 |
assert isinstance(s, annmodel.SomeInteger) |
| 3975 |
|
| 3976 |
def test_join_none_and_nonnull(self): |
| 3977 |
from rpython.rlib.rstring import assert_str0 |
| 3978 |
|
| 3979 |
def f(i): |
| 3980 |
a = str(i) |
| 3981 |
a = assert_str0(a) |
| 3982 |
return a.join([None]) |
| 3983 |
|
| 3984 |
a = self.RPythonAnnotator() |
| 3985 |
s = a.build_types(f, [int]) |
| 3986 |
assert isinstance(s, annmodel.SomeString) |
| 3987 |
assert not s.can_be_None |
| 3988 |
|
| 3989 |
def test_contains_no_nul(self): |
| 3990 |
def f(i): |
| 3991 |
if "\0" in i: |
| 3992 |
return None |
| 3993 |
else: |
| 3994 |
return i |
| 3995 |
a = self.RPythonAnnotator() |
| 3996 |
a.translator.config.translation.check_str_without_nul = True |
| 3997 |
s = a.build_types(f, [annmodel.SomeString(no_nul=False)]) |
| 3998 |
assert isinstance(s, annmodel.SomeString) |
| 3999 |
assert s.can_be_None |
| 4000 |
assert s.no_nul |
| 4001 |
|
| 4002 |
def test_contains_no_nul_unicode(self): |
| 4003 |
def f(i): |
| 4004 |
if u"\0" in i: |
| 4005 |
return None |
| 4006 |
else: |
| 4007 |
return i |
| 4008 |
a = self.RPythonAnnotator() |
| 4009 |
a.translator.config.translation.check_str_without_nul = True |
| 4010 |
s = a.build_types(f, [annmodel.SomeUnicodeString(no_nul=False)]) |
| 4011 |
assert isinstance(s, annmodel.SomeUnicodeString) |
| 4012 |
assert s.can_be_None |
| 4013 |
assert s.no_nul |
| 4014 |
|
| 4015 |
def test_no___call__(self): |
| 4016 |
class X(object): |
| 4017 |
def __call__(self): |
| 4018 |
xxx |
| 4019 |
x = X() |
| 4020 |
def f(): |
| 4021 |
return x |
| 4022 |
a = self.RPythonAnnotator() |
| 4023 |
e = py.test.raises(Exception, a.build_types, f, []) |
| 4024 |
assert 'object with a __call__ is not RPython' in str(e.value) |
| 4025 |
|
| 4026 |
def test_os_getcwd(self): |
| 4027 |
import os |
| 4028 |
def fn(): |
| 4029 |
return os.getcwd() |
| 4030 |
a = self.RPythonAnnotator() |
| 4031 |
s = a.build_types(fn, []) |
| 4032 |
assert isinstance(s, annmodel.SomeString) |
| 4033 |
assert s.no_nul |
| 4034 |
|
| 4035 |
def test_os_getenv(self): |
| 4036 |
import os |
| 4037 |
def fn(): |
| 4038 |
return os.environ.get('PATH') |
| 4039 |
a = self.RPythonAnnotator() |
| 4040 |
s = a.build_types(fn, []) |
| 4041 |
assert isinstance(s, annmodel.SomeString) |
| 4042 |
assert s.no_nul |
| 4043 |
|
| 4044 |
def test_base_iter(self): |
| 4045 |
class A(object): |
| 4046 |
def __iter__(self): |
| 4047 |
return self |
| 4048 |
|
| 4049 |
def fn(): |
| 4050 |
return iter(A()) |
| 4051 |
|
| 4052 |
a = self.RPythonAnnotator() |
| 4053 |
s = a.build_types(fn, []) |
| 4054 |
assert isinstance(s, annmodel.SomeInstance) |
| 4055 |
assert s.classdef.name.endswith('.A') |
| 4056 |
|
| 4057 |
def test_iter_next(self): |
| 4058 |
class A(object): |
| 4059 |
def __iter__(self): |
| 4060 |
return self |
| 4061 |
|
| 4062 |
def next(self): |
| 4063 |
return 1 |
| 4064 |
|
| 4065 |
def fn(): |
| 4066 |
s = 0 |
| 4067 |
for x in A(): |
| 4068 |
s += x |
| 4069 |
return s |
| 4070 |
|
| 4071 |
a = self.RPythonAnnotator() |
| 4072 |
s = a.build_types(fn, []) |
| 4073 |
assert len(a.translator.graphs) == 3 |
| 4074 |
assert isinstance(s, annmodel.SomeInteger) |
| 4075 |
|
| 4076 |
def test_next_function(self): |
| 4077 |
def fn(n): |
| 4078 |
x = [0, 1, n] |
| 4079 |
i = iter(x) |
| 4080 |
return next(i) + next(i) |
| 4081 |
|
| 4082 |
a = self.RPythonAnnotator() |
| 4083 |
s = a.build_types(fn, [int]) |
| 4084 |
assert isinstance(s, annmodel.SomeInteger) |
| 4085 |
|
| 4086 |
def test_instance_getitem(self): |
| 4087 |
class A(object): |
| 4088 |
def __getitem__(self, i): |
| 4089 |
return i * i |
| 4090 |
|
| 4091 |
def fn(i): |
| 4092 |
a = A() |
| 4093 |
return a[i] |
| 4094 |
|
| 4095 |
a = self.RPythonAnnotator() |
| 4096 |
s = a.build_types(fn, [int]) |
| 4097 |
assert len(a.translator.graphs) == 2 |
| 4098 |
assert isinstance(s, annmodel.SomeInteger) |
| 4099 |
|
| 4100 |
def test_instance_setitem(self): |
| 4101 |
class A(object): |
| 4102 |
def __setitem__(self, i, v): |
| 4103 |
self.value = i * v |
| 4104 |
|
| 4105 |
def fn(i, v): |
| 4106 |
a = A() |
| 4107 |
a[i] = v |
| 4108 |
return a.value |
| 4109 |
|
| 4110 |
a = self.RPythonAnnotator() |
| 4111 |
s = a.build_types(fn, [int, int]) |
| 4112 |
assert len(a.translator.graphs) == 2 |
| 4113 |
assert isinstance(s, annmodel.SomeInteger) |
| 4114 |
|
| 4115 |
def test_instance_delitem(self): |
| 4116 |
class A(object): |
| 4117 |
def __delitem__(self, i): |
| 4118 |
self.value = i |
| 4119 |
|
| 4120 |
def fn(i): |
| 4121 |
a = A() |
| 4122 |
del a[i] |
| 4123 |
return a.value |
| 4124 |
|
| 4125 |
a = self.RPythonAnnotator() |
| 4126 |
s = a.build_types(fn, [int]) |
| 4127 |
assert len(a.translator.graphs) == 2 |
| 4128 |
assert isinstance(s, annmodel.SomeInteger) |
| 4129 |
|
| 4130 |
|
| 4131 |
def test_instance_contains(self): |
| 4132 |
class A(object): |
| 4133 |
def __contains__(self, i): |
| 4134 |
return i & 1 == 0 |
| 4135 |
|
| 4136 |
def fn(i): |
| 4137 |
a = A() |
| 4138 |
return 0 in a and 1 not in a |
| 4139 |
|
| 4140 |
a = self.RPythonAnnotator() |
| 4141 |
s = a.build_types(fn, [int]) |
| 4142 |
assert len(a.translator.graphs) == 2 |
| 4143 |
assert isinstance(s, annmodel.SomeBool) |
| 4144 |
|
| 4145 |
def test_instance_getslice(self): |
| 4146 |
class A(object): |
| 4147 |
def __getslice__(self, stop, start): |
| 4148 |
return "Test"[stop:start] |
| 4149 |
|
| 4150 |
def fn(): |
| 4151 |
a = A() |
| 4152 |
return a[0:2] |
| 4153 |
|
| 4154 |
a = self.RPythonAnnotator() |
| 4155 |
s = a.build_types(fn, []) |
| 4156 |
assert len(a.translator.graphs) == 2 |
| 4157 |
assert isinstance(s, annmodel.SomeString) |
| 4158 |
|
| 4159 |
def test_instance_setslice(self): |
| 4160 |
class A(object): |
| 4161 |
def __setslice__(self, stop, start, value): |
| 4162 |
self.value = value |
| 4163 |
|
| 4164 |
def fn(): |
| 4165 |
a = A() |
| 4166 |
a[0:2] = '00' |
| 4167 |
return a.value |
| 4168 |
|
| 4169 |
a = self.RPythonAnnotator() |
| 4170 |
s = a.build_types(fn, []) |
| 4171 |
assert len(a.translator.graphs) == 2 |
| 4172 |
assert isinstance(s, annmodel.SomeString) |
| 4173 |
|
| 4174 |
def test_instance_len(self): |
| 4175 |
class A(object): |
| 4176 |
def __len__(self): |
| 4177 |
return 0 |
| 4178 |
|
| 4179 |
def fn(): |
| 4180 |
a = A() |
| 4181 |
return len(a) |
| 4182 |
|
| 4183 |
a = self.RPythonAnnotator() |
| 4184 |
s = a.build_types(fn, []) |
| 4185 |
assert len(a.translator.graphs) == 2 |
| 4186 |
assert isinstance(s, annmodel.SomeInteger) |
| 4187 |
|
| 4188 |
def test_reversed(self): |
| 4189 |
def fn(n): |
| 4190 |
for elem in reversed([1, 2, 3, 4, 5]): |
| 4191 |
return elem |
| 4192 |
return n |
| 4193 |
|
| 4194 |
a = self.RPythonAnnotator() |
| 4195 |
s = a.build_types(fn, [int]) |
| 4196 |
assert isinstance(s, annmodel.SomeInteger) |
| 4197 |
|
| 4198 |
def test_no_attr_on_common_exception_classes(self): |
| 4199 |
for cls in [ValueError, Exception]: |
| 4200 |
def fn(): |
| 4201 |
e = cls() |
| 4202 |
e.foo = "bar" |
| 4203 |
a = self.RPythonAnnotator() |
| 4204 |
with py.test.raises(NoSuchAttrError): |
| 4205 |
a.build_types(fn, []) |
| 4206 |
|
| 4207 |
def test_lower_char(self): |
| 4208 |
def fn(c): |
| 4209 |
return c.lower() |
| 4210 |
a = self.RPythonAnnotator() |
| 4211 |
s = a.build_types(fn, [annmodel.SomeChar()]) |
| 4212 |
assert s == annmodel.SomeChar() |
| 4213 |
|
| 4214 |
def test_isinstance_double_const(self): |
| 4215 |
class X(object): |
| 4216 |
def _freeze_(self): |
| 4217 |
return True |
| 4218 |
|
| 4219 |
x = X() |
| 4220 |
|
| 4221 |
def f(i): |
| 4222 |
if i: |
| 4223 |
x1 = x |
| 4224 |
else: |
| 4225 |
x1 = None |
| 4226 |
print("hello") |
| 4227 |
return isinstance(x1, X) |
| 4228 |
|
| 4229 |
a = self.RPythonAnnotator() |
| 4230 |
s = a.build_types(f, [annmodel.SomeInteger()]) |
| 4231 |
assert isinstance(s, annmodel.SomeBool) |
| 4232 |
|
| 4233 |
def test_doing_bad_things_with_print(self): |
| 4234 |
|
| 4235 |
def f(): |
| 4236 |
pronto = print |
| 4237 |
return pronto("the quick brown fox") |
| 4238 |
|
| 4239 |
a = self.RPythonAnnotator() |
| 4240 |
s = a.build_types(f, []) |
| 4241 |
assert isinstance(s, annmodel.SomeNone) |
| 4242 |
|
| 4243 |
def g(): |
| 4244 |
print = int |
| 4245 |
return print(3.0) |
| 4246 |
|
| 4247 |
a = self.RPythonAnnotator() |
| 4248 |
s = a.build_types(g, []) |
| 4249 |
assert isinstance(s, annmodel.SomeInteger) |
| 4250 |
|
| 4251 |
def test_object_init(self): |
| 4252 |
class A(object): |
| 4253 |
pass |
| 4254 |
|
| 4255 |
class B(A): |
| 4256 |
def __init__(self): |
| 4257 |
A.__init__(self) |
| 4258 |
|
| 4259 |
def f(): |
| 4260 |
B() |
| 4261 |
|
| 4262 |
a = self.RPythonAnnotator() |
| 4263 |
a.build_types(f, []) |
| 4264 |
|
| 4265 |
def test_bytearray(self): |
| 4266 |
def f(): |
| 4267 |
return bytearray("xyz") |
| 4268 |
|
| 4269 |
a = self.RPythonAnnotator() |
| 4270 |
s = a.build_types(f, []) |
| 4271 |
assert isinstance(s, annmodel.SomeByteArray) |
| 4272 |
assert not s.is_constant() |
| 4273 |
|
| 4274 |
def test_bytearray_add(self): |
| 4275 |
def f(a): |
| 4276 |
return a + bytearray("xyz") |
| 4277 |
|
| 4278 |
a = self.RPythonAnnotator() |
| 4279 |
assert isinstance(a.build_types(f, [annmodel.SomeByteArray()]), |
| 4280 |
annmodel.SomeByteArray) |
| 4281 |
a = self.RPythonAnnotator() |
| 4282 |
assert isinstance(a.build_types(f, [str]), |
| 4283 |
annmodel.SomeByteArray) |
| 4284 |
a = self.RPythonAnnotator() |
| 4285 |
assert isinstance(a.build_types(f, [annmodel.SomeChar()]), |
| 4286 |
annmodel.SomeByteArray) |
| 4287 |
|
| 4288 |
def test_bytearray_setitem_getitem(self): |
| 4289 |
def f(b, i, c): |
| 4290 |
b[i] = c |
| 4291 |
return b[i + 1] |
| 4292 |
|
| 4293 |
a = self.RPythonAnnotator() |
| 4294 |
assert isinstance(a.build_types(f, [annmodel.SomeByteArray(), |
| 4295 |
int, int]), |
| 4296 |
annmodel.SomeInteger) |
| 4297 |
|
| 4298 |
def test_constant_startswith_endswith(self): |
| 4299 |
def f(): |
| 4300 |
return "abc".startswith("ab") and "abc".endswith("bc") |
| 4301 |
a = self.RPythonAnnotator() |
| 4302 |
assert a.build_types(f, []).const is True |
| 4303 |
|
| 4304 |
def test_specific_attributes(self): |
| 4305 |
class A(object): |
| 4306 |
pass |
| 4307 |
|
| 4308 |
class B(A): |
| 4309 |
def __init__(self, x): |
| 4310 |
assert x >= 0 |
| 4311 |
self.x = x |
| 4312 |
|
| 4313 |
def fn(i): |
| 4314 |
if i % 2: |
| 4315 |
a = A() |
| 4316 |
else: |
| 4317 |
a = B(3) |
| 4318 |
if i % 3: |
| 4319 |
a.x = -3 |
| 4320 |
if isinstance(a, B): |
| 4321 |
return a.x |
| 4322 |
return 0 |
| 4323 |
|
| 4324 |
a = self.RPythonAnnotator() |
| 4325 |
assert not a.build_types(fn, [int]).nonneg |
| 4326 |
|
| 4327 |
def test_unionerror_attrs(self): |
| 4328 |
def f(x): |
| 4329 |
if x < 10: |
| 4330 |
return 1 |
| 4331 |
else: |
| 4332 |
return "bbb" |
| 4333 |
a = self.RPythonAnnotator() |
| 4334 |
|
| 4335 |
with py.test.raises(UnionError) as exc: |
| 4336 |
a.build_types(f, [int]) |
| 4337 |
|
| 4338 |
the_exc = exc.value |
| 4339 |
s_objs = set([type(the_exc.s_obj1), type(the_exc.s_obj2)]) |
| 4340 |
|
| 4341 |
assert s_objs == set([annmodel.SomeInteger, annmodel.SomeString]) |
| 4342 |
|
| 4343 |
def test_unionerror_tuple_size(self): |
| 4344 |
def f(x): |
| 4345 |
if x < 10: |
| 4346 |
return (1, ) |
| 4347 |
else: |
| 4348 |
return (1, 2) |
| 4349 |
a = self.RPythonAnnotator() |
| 4350 |
|
| 4351 |
with py.test.raises(UnionError) as exc: |
| 4352 |
a.build_types(f, [int]) |
| 4353 |
|
| 4354 |
assert "RPython cannot unify tuples of different length: 2 versus 1" in exc.value.msg |
| 4355 |
|
| 4356 |
def test_unionerror_signedness(self): |
| 4357 |
def f(x): |
| 4358 |
if x < 10: |
| 4359 |
return r_uint(99) |
| 4360 |
else: |
| 4361 |
return -1 |
| 4362 |
a = self.RPythonAnnotator() |
| 4363 |
|
| 4364 |
with py.test.raises(UnionError) as exc: |
| 4365 |
a.build_types(f, [int]) |
| 4366 |
|
| 4367 |
assert ("RPython cannot prove that these integers are of the " |
| 4368 |
"same signedness" in exc.value.msg) |
| 4369 |
|
| 4370 |
def test_unionerror_instance(self): |
| 4371 |
class A(object): pass |
| 4372 |
class B(object): pass |
| 4373 |
|
| 4374 |
def f(x): |
| 4375 |
if x < 10: |
| 4376 |
return A() |
| 4377 |
else: |
| 4378 |
return B() |
| 4379 |
a = self.RPythonAnnotator() |
| 4380 |
|
| 4381 |
with py.test.raises(UnionError) as exc: |
| 4382 |
a.build_types(f, [int]) |
| 4383 |
|
| 4384 |
assert ("RPython cannot unify instances with no common base class" |
| 4385 |
in exc.value.msg) |
| 4386 |
|
| 4387 |
def test_unionerror_iters(self): |
| 4388 |
|
| 4389 |
def f(x): |
| 4390 |
d = { 1 : "a", 2 : "b" } |
| 4391 |
if x < 10: |
| 4392 |
return d.iterkeys() |
| 4393 |
else: |
| 4394 |
return d.itervalues() |
| 4395 |
a = self.RPythonAnnotator() |
| 4396 |
|
| 4397 |
with py.test.raises(UnionError) as exc: |
| 4398 |
a.build_types(f, [int]) |
| 4399 |
|
| 4400 |
assert ("RPython cannot unify incompatible iterator variants" in |
| 4401 |
exc.value.msg) |
| 4402 |
|
| 4403 |
def test_variable_getattr(self): |
| 4404 |
class A(object): pass |
| 4405 |
def f(y): |
| 4406 |
a = A() |
| 4407 |
return getattr(a, y) |
| 4408 |
a = self.RPythonAnnotator() |
| 4409 |
with py.test.raises(AnnotatorError) as exc: |
| 4410 |
a.build_types(f, [str]) |
| 4411 |
assert ("variable argument to getattr" in exc.value.msg) |
| 4412 |
|
| 4413 |
def test_bad_call(self): |
| 4414 |
def f(x): |
| 4415 |
return x() |
| 4416 |
a = self.RPythonAnnotator() |
| 4417 |
with py.test.raises(AnnotatorError) as exc: |
| 4418 |
a.build_types(f, [str]) |
| 4419 |
assert ("Cannot prove that the object is callable" in exc.value.msg) |
| 4420 |
|
| 4421 |
def test_UnionError_on_PBC(self): |
| 4422 |
l = ['a', 1] |
| 4423 |
def f(x): |
| 4424 |
l.append(x) |
| 4425 |
a = self.RPythonAnnotator() |
| 4426 |
with py.test.raises(UnionError) as excinfo: |
| 4427 |
a.build_types(f, [int]) |
| 4428 |
assert 'Happened at file' in excinfo.value.source |
| 4429 |
assert 'Known variable annotations:' in excinfo.value.source |
| 4430 |
|
| 4431 |
def test_str_format_error(self): |
| 4432 |
def f(s, x): |
| 4433 |
return s.format(x) |
| 4434 |
a = self.RPythonAnnotator() |
| 4435 |
with py.test.raises(AnnotatorError) as exc: |
| 4436 |
a.build_types(f, [str, str]) |
| 4437 |
assert ("format() is not RPython" in exc.value.msg) |
| 4438 |
|
| 4439 |
def test_prebuilt_ordered_dict(self): |
| 4440 |
d = OrderedDict([("aa", 1)]) |
| 4441 |
|
| 4442 |
def f(): |
| 4443 |
return d |
| 4444 |
|
| 4445 |
a = self.RPythonAnnotator() |
| 4446 |
assert isinstance(a.build_types(f, []), annmodel.SomeOrderedDict) |
| 4447 |
|
| 4448 |
def test_enumerate_none(self): |
| 4449 |
|
| 4450 |
|
| 4451 |
|
| 4452 |
def f(flag): |
| 4453 |
if flag: |
| 4454 |
x = None |
| 4455 |
else: |
| 4456 |
x = [42] |
| 4457 |
return enumerate(x).next() |
| 4458 |
a = self.RPythonAnnotator() |
| 4459 |
s = a.build_types(f, [int]) |
| 4460 |
assert isinstance(s, annmodel.SomeTuple) |
| 4461 |
assert s.items[1].const == 42 |
| 4462 |
|
| 4463 |
def test_unpack_none_gets_a_blocked_block(self): |
| 4464 |
def f(x): |
| 4465 |
a, b = x |
| 4466 |
a = self.RPythonAnnotator() |
| 4467 |
py.test.raises(AnnotatorError, |
| 4468 |
a.build_types, f, [annmodel.s_None]) |
| 4469 |
|
| 4470 |
def test_class___name__(self): |
| 4471 |
class Abc(object): |
| 4472 |
pass |
| 4473 |
def f(): |
| 4474 |
return Abc().__class__.__name__ |
| 4475 |
a = self.RPythonAnnotator() |
| 4476 |
s = a.build_types(f, []) |
| 4477 |
assert isinstance(s, annmodel.SomeString) |
| 4478 |
|
| 4479 |
def test_isinstance_str_1(self): |
| 4480 |
def g(): |
| 4481 |
pass |
| 4482 |
def f(n): |
| 4483 |
if n > 5: |
| 4484 |
s = "foo" |
| 4485 |
else: |
| 4486 |
s = None |
| 4487 |
g() |
| 4488 |
return isinstance(s, str) |
| 4489 |
a = self.RPythonAnnotator() |
| 4490 |
s = a.build_types(f, [int]) |
| 4491 |
assert isinstance(s, annmodel.SomeBool) |
| 4492 |
assert not s.is_constant() |
| 4493 |
|
| 4494 |
def test_isinstance_str_2(self): |
| 4495 |
def g(): |
| 4496 |
pass |
| 4497 |
def f(n): |
| 4498 |
if n > 5: |
| 4499 |
s = "foo" |
| 4500 |
else: |
| 4501 |
s = None |
| 4502 |
g() |
| 4503 |
if isinstance(s, str): |
| 4504 |
return s |
| 4505 |
return "" |
| 4506 |
a = self.RPythonAnnotator() |
| 4507 |
s = a.build_types(f, [int]) |
| 4508 |
assert isinstance(s, annmodel.SomeString) |
| 4509 |
assert not s.can_be_none() |
| 4510 |
|
| 4511 |
def test_property_getter(self): |
| 4512 |
class O1(object): |
| 4513 |
def __init__(self, x): |
| 4514 |
self._x = x |
| 4515 |
@property |
| 4516 |
def x(self): |
| 4517 |
return self._x |
| 4518 |
def f(n): |
| 4519 |
o = O1(n) |
| 4520 |
return o.x + getattr(o, 'x') |
| 4521 |
a = self.RPythonAnnotator() |
| 4522 |
s = a.build_types(f, [int]) |
| 4523 |
assert isinstance(s, annmodel.SomeInteger) |
| 4524 |
op = list(graphof(a, f).iterblocks())[0].operations |
| 4525 |
i = 0 |
| 4526 |
c = 0 |
| 4527 |
while i < len(op): |
| 4528 |
if op[i].opname == 'getattr': |
| 4529 |
c += 1 |
| 4530 |
assert op[i].args[1].value == 'x__getter__' |
| 4531 |
i += 1 |
| 4532 |
assert i < len(op) and op[i].opname == 'simple_call' and \ |
| 4533 |
op[i].args[0] == op[i - 1].result |
| 4534 |
i += 1 |
| 4535 |
assert c == 2 |
| 4536 |
|
| 4537 |
def test_property_setter(self): |
| 4538 |
class O2(object): |
| 4539 |
def __init__(self): |
| 4540 |
self._x = 0 |
| 4541 |
def set_x(self, v): |
| 4542 |
self._x = v |
| 4543 |
x = property(fset=set_x) |
| 4544 |
def f(n): |
| 4545 |
o = O2() |
| 4546 |
o.x = n |
| 4547 |
setattr(o, 'x', n) |
| 4548 |
a = self.RPythonAnnotator() |
| 4549 |
s = a.build_types(f, [int]) |
| 4550 |
op = list(graphof(a, f).iterblocks())[0].operations |
| 4551 |
i = 0 |
| 4552 |
c = 0 |
| 4553 |
while i < len(op): |
| 4554 |
if op[i].opname == 'getattr': |
| 4555 |
c += 1 |
| 4556 |
assert op[i].args[1].value == 'x__setter__' |
| 4557 |
i += 1 |
| 4558 |
assert i < len(op) and op[i].opname == 'simple_call' and \ |
| 4559 |
op[i].args[0] == op[i - 1].result and len(op[i].args) == 2 |
| 4560 |
i += 1 |
| 4561 |
assert c == 2 |
| 4562 |
|
| 4563 |
def test_property_unionerr(self): |
| 4564 |
class O1(object): |
| 4565 |
def __init__(self, x): |
| 4566 |
self._x = x |
| 4567 |
@property |
| 4568 |
def x(self): |
| 4569 |
return self._x |
| 4570 |
class O2(O1): |
| 4571 |
def set_x(self, v): |
| 4572 |
self._x = v |
| 4573 |
x = property(fset=set_x) |
| 4574 |
def f1(n): |
| 4575 |
o = O2(n) |
| 4576 |
return o.x |
| 4577 |
def f2(n): |
| 4578 |
o = O2(n) |
| 4579 |
o.x = 20 |
| 4580 |
a = self.RPythonAnnotator() |
| 4581 |
with py.test.raises(UnionError) as exc: |
| 4582 |
a.build_types(f1, [int]) |
| 4583 |
a = self.RPythonAnnotator() |
| 4584 |
with py.test.raises(UnionError) as exc: |
| 4585 |
a.build_types(f2, [int]) |
| 4586 |
|
| 4587 |
@py.test.mark.xfail(reason="May produce garbage annotations instead of " |
| 4588 |
"raising AnnotatorError, depending on annotation order") |
| 4589 |
def test_property_union_2(self): |
| 4590 |
class Base(object): |
| 4591 |
pass |
| 4592 |
|
| 4593 |
class A(Base): |
| 4594 |
def __init__(self): |
| 4595 |
pass |
| 4596 |
|
| 4597 |
@property |
| 4598 |
def x(self): |
| 4599 |
return 42 |
| 4600 |
|
| 4601 |
class B(Base): |
| 4602 |
def __init__(self, x): |
| 4603 |
self.x = x |
| 4604 |
|
| 4605 |
def f(n): |
| 4606 |
if n < 0: |
| 4607 |
obj = A() |
| 4608 |
else: |
| 4609 |
obj = B(n) |
| 4610 |
return obj.x |
| 4611 |
a = self.RPythonAnnotator() |
| 4612 |
|
| 4613 |
|
| 4614 |
with py.test.raises(AnnotatorError): |
| 4615 |
a.build_types(f, [int]) |
| 4616 |
|
| 4617 |
@py.test.mark.xfail(reason="May produce garbage annotations instead of " |
| 4618 |
"raising AnnotatorError, depending on annotation order") |
| 4619 |
def test_property_union_3(self): |
| 4620 |
class Base(object): |
| 4621 |
pass |
| 4622 |
class A(Base): |
| 4623 |
@property |
| 4624 |
def x(self): |
| 4625 |
return 42 |
| 4626 |
class B(Base): |
| 4627 |
x = 43 |
| 4628 |
def f(n): |
| 4629 |
if n < 0: |
| 4630 |
obj = A() |
| 4631 |
else: |
| 4632 |
obj = B() |
| 4633 |
return obj.x |
| 4634 |
a = self.RPythonAnnotator() |
| 4635 |
with py.test.raises(AnnotatorError): |
| 4636 |
a.build_types(f, [int]) |
| 4637 |
|
| 4638 |
def test_dict_can_be_none_ordering_issue(self): |
| 4639 |
def g(d): |
| 4640 |
return 42 in d |
| 4641 |
def f(n): |
| 4642 |
g(None) |
| 4643 |
g({}) |
| 4644 |
a = self.RPythonAnnotator() |
| 4645 |
a.build_types(f, [int]) |
| 4646 |
|
| 4647 |
def test_numbers_dont_have_len(self): |
| 4648 |
def f(x): |
| 4649 |
return len(x) |
| 4650 |
a = self.RPythonAnnotator() |
| 4651 |
with py.test.raises(AnnotatorError): |
| 4652 |
a.build_types(f, [int]) |
| 4653 |
with py.test.raises(AnnotatorError): |
| 4654 |
a.build_types(f, [float]) |
| 4655 |
|
| 4656 |
def test_numbers_dont_contain(self): |
| 4657 |
def f(x): |
| 4658 |
return 1 in x |
| 4659 |
a = self.RPythonAnnotator() |
| 4660 |
with py.test.raises(AnnotatorError): |
| 4661 |
a.build_types(f, [int]) |
| 4662 |
with py.test.raises(AnnotatorError): |
| 4663 |
a.build_types(f, [float]) |
| 4664 |
|
| 4665 |
def test_Ellipsis_not_rpython(self): |
| 4666 |
def f(): |
| 4667 |
return Ellipsis |
| 4668 |
a = self.RPythonAnnotator() |
| 4669 |
e = py.test.raises(Exception, a.build_types, f, []) |
| 4670 |
assert "Don't know how to represent Ellipsis" in str(e.value) |
| 4671 |
|
| 4672 |
def test_must_be_light_finalizer(self): |
| 4673 |
from rpython.rlib import rgc |
| 4674 |
@rgc.must_be_light_finalizer |
| 4675 |
class A(object): |
| 4676 |
pass |
| 4677 |
class B(A): |
| 4678 |
def __del__(self): |
| 4679 |
pass |
| 4680 |
class C(A): |
| 4681 |
@rgc.must_be_light_finalizer |
| 4682 |
def __del__(self): |
| 4683 |
pass |
| 4684 |
class D(object): |
| 4685 |
def __del__(self): |
| 4686 |
pass |
| 4687 |
def fb(): |
| 4688 |
B() |
| 4689 |
def fc(): |
| 4690 |
C() |
| 4691 |
def fd(): |
| 4692 |
D() |
| 4693 |
a = self.RPythonAnnotator() |
| 4694 |
a.build_types(fc, []) |
| 4695 |
a.build_types(fd, []) |
| 4696 |
py.test.raises(AnnotatorError, a.build_types, fb, []) |
| 4697 |
|
| 4698 |
def test_annotate_generator_with_unreachable_yields(self): |
| 4699 |
def f(n): |
| 4700 |
if n < 0: |
| 4701 |
yield 42 |
| 4702 |
yield n |
| 4703 |
yield n |
| 4704 |
def main(n): |
| 4705 |
for x in f(abs(n)): |
| 4706 |
pass |
| 4707 |
|
| 4708 |
a = self.RPythonAnnotator() |
| 4709 |
a.build_types(main, [int]) |
| 4710 |
|
| 4711 |
def test_string_mod_nonconstant(self): |
| 4712 |
def f(x): |
| 4713 |
return x % 5 |
| 4714 |
a = self.RPythonAnnotator() |
| 4715 |
e = py.test.raises(AnnotatorError, a.build_types, f, [str]) |
| 4716 |
assert ('string formatting requires a constant string/unicode' |
| 4717 |
in str(e.value)) |
| 4718 |
|
| 4719 |
def test_cannot_raise_none(self): |
| 4720 |
def f(x): |
| 4721 |
s = None |
| 4722 |
if x > 5: |
| 4723 |
s = ValueError() |
| 4724 |
raise s |
| 4725 |
a = self.RPythonAnnotator() |
| 4726 |
a.build_types(f, [int]) |
| 4727 |
s_exc = a.binding(graphof(a, f).exceptblock.inputargs[1]) |
| 4728 |
assert not s_exc.can_be_none() |
| 4729 |
|
| 4730 |
def test_specialize_argtype_with_subclasses(self): |
| 4731 |
|
| 4732 |
|
| 4733 |
class A: |
| 4734 |
def foo(self): |
| 4735 |
return 123 |
| 4736 |
class B(A): |
| 4737 |
def foo(self): |
| 4738 |
return 456 |
| 4739 |
def f(x): |
| 4740 |
return x.foo() |
| 4741 |
f._annspecialcase_ = "specialize:argtype(0)" |
| 4742 |
def h(y): |
| 4743 |
if y > 5: |
| 4744 |
f(A()) |
| 4745 |
return f(B()) |
| 4746 |
a = self.RPythonAnnotator() |
| 4747 |
assert a.build_types(h, [int]).const == 456 |
| 4748 |
|
| 4749 |
def test_list_plus_equal_string(self): |
| 4750 |
def f(n): |
| 4751 |
lst = [chr(n), chr(n + 1)] |
| 4752 |
if n < 100: |
| 4753 |
lst.append(chr(n + 2)) |
| 4754 |
lst += str(n) |
| 4755 |
return lst |
| 4756 |
a = self.RPythonAnnotator() |
| 4757 |
s = a.build_types(f, [int]) |
| 4758 |
assert isinstance(listitem(s), annmodel.SomeChar) |
| 4759 |
|
| 4760 |
def test_union_of_methods_of_frozen(self): |
| 4761 |
class A(Freezing): |
| 4762 |
def foo(self): |
| 4763 |
return 42 |
| 4764 |
def bar(self): |
| 4765 |
return 43 |
| 4766 |
class B(A): |
| 4767 |
def foo(self): |
| 4768 |
return 44 |
| 4769 |
|
| 4770 |
a = A() |
| 4771 |
b = B() |
| 4772 |
def f(n): |
| 4773 |
x = a if n > 0 else b |
| 4774 |
return x.foo() |
| 4775 |
def g(n): |
| 4776 |
x = a if n > 0 else b |
| 4777 |
return x.bar() |
| 4778 |
ann = self.RPythonAnnotator() |
| 4779 |
with py.test.raises(AnnotatorError): |
| 4780 |
|
| 4781 |
s = ann.build_types(f, [int]) |
| 4782 |
|
| 4783 |
|
| 4784 |
s = ann.build_types(g, [int]) |
| 4785 |
assert s.const == 43 |
| 4786 |
|
| 4787 |
def test_isinstance_const(self): |
| 4788 |
class A(object): |
| 4789 |
pass |
| 4790 |
|
| 4791 |
class B(A): |
| 4792 |
pass |
| 4793 |
B.singleton = B() |
| 4794 |
class C(A): |
| 4795 |
pass |
| 4796 |
C.singleton = C() |
| 4797 |
def f(): |
| 4798 |
x = B.singleton |
| 4799 |
if not isinstance(x, B): |
| 4800 |
if not isinstance(x, C): |
| 4801 |
raise TypeError |
| 4802 |
return 12 |
| 4803 |
ann = self.RPythonAnnotator() |
| 4804 |
s = ann.build_types(f, []) |
| 4805 |
|
| 4806 |
def test_len_const_str(self): |
| 4807 |
def f(): |
| 4808 |
return len('abc') |
| 4809 |
ann = self.RPythonAnnotator() |
| 4810 |
s = ann.build_types(f, []) |
| 4811 |
assert s.is_constant() |
| 4812 |
assert s.const == 3 |
| 4813 |
|
| 4814 |
def g(s): |
| 4815 |
return len(s) |
| 4816 |
g._annspecialcase_ = "specialize:arg(0)" |
| 4817 |
def f(): |
| 4818 |
return g('abc') |
| 4819 |
s = ann.build_types(f, []) |
| 4820 |
assert s.is_constant() |
| 4821 |
assert s.const == 3 |
| 4822 |
|
| 4823 |
|
| 4824 |
def g(n): |
| 4825 |
return [0, 1, 2, n] |
| 4826 |
|
| 4827 |
|
| 4828 |
def f_calls_g(n): |
| 4829 |
total = 0 |
| 4830 |
lst = g(n) |
| 4831 |
i = 0 |
| 4832 |
while i < len(lst): |
| 4833 |
total += i |
| 4834 |
i += 1 |
| 4835 |
return total |
| 4836 |
|
| 4837 |
constant_unsigned_five = r_uint(5) |
| 4838 |
|
| 4839 |
class Freezing: |
| 4840 |
def _freeze_(self): |
| 4841 |
return True |