Debian Codegraph @

rpython/annotator/test/test_annrpython_py2.py

raw symbols SCIP
1 """In which we test that various pieces of py2-only syntax are supported."""
2 from __future__ import with_statement
3
4 from rpython.conftest import option
5
6 from rpython.annotator import model as annmodel
7 from rpython.annotator.annrpython import RPythonAnnotator as _RPythonAnnotator
8 from rpython.translator.test import snippet
9
10 from .test_annrpython import (
11 TestAnnotateTestCase as _TestAnnotateTestCase, graphof
12 )
13
14
15 class TestAnnotateTestCase:
16 def teardown_method(self, meth):
17 assert annmodel.s_Bool == annmodel.SomeBool()
18
19 class RPythonAnnotator(_RPythonAnnotator):
20 def build_types(self, *args):
21 s = _RPythonAnnotator.build_types(self, *args)
22 self.validate()
23 if option.view:
24 self.translator.view()
25 return s
26
27 def test_harmonic(self):
28 a = self.RPythonAnnotator()
29 s = a.build_types(snippet.harmonic, [int])
30 assert s.knowntype == float
31 # check that the list produced by range() is not mutated or resized
32 graph = graphof(a, snippet.harmonic)
33 all_vars = set().union(*[block.getvariables() for block in graph.iterblocks()])
34 print all_vars
35 for var in all_vars:
36 s_value = var.annotation
37 if isinstance(s_value, annmodel.SomeList):
38 assert not s_value.listdef.listitem.resized
39 assert not s_value.listdef.listitem.mutated
40 assert s_value.listdef.listitem.range_step
41
42 def test_prebuilt_long_that_is_not_too_long(self):
43 small_constant = 12L
44 def f():
45 return small_constant
46 a = self.RPythonAnnotator()
47 s = a.build_types(f, [])
48 assert s.const == 12
49 assert s.nonneg
50 assert not s.unsigned
51 #
52 small_constant = -23L
53 def f():
54 return small_constant
55 a = self.RPythonAnnotator()
56 s = a.build_types(f, [])
57 assert s.const == -23
58 assert not s.nonneg
59 assert not s.unsigned
60
61 def test_isinstance_double_const(self):
62 class X(object):
63 def _freeze_(self):
64 return True
65
66 x = X()
67
68 def f(i):
69 if i:
70 x1 = x
71 else:
72 x1 = None
73 print "hello" # this is to force the merge of blocks
74 return isinstance(x1, X)
75
76 a = self.RPythonAnnotator()
77 s = a.build_types(f, [annmodel.SomeInteger()])
78 assert isinstance(s, annmodel.SomeBool)