Debian Codegraph @

rpython/rlib/test/test_debug.py

raw symbols SCIP
1 import py
2 import pytest
3 from rpython.rlib.debug import (check_annotation, make_sure_not_resized,
4 debug_print, debug_start, debug_stop,
5 have_debug_prints, debug_offset, debug_flush,
6 check_nonneg, IntegerCanBeNegative,
7 mark_dict_non_null,
8 check_list_of_chars,
9 NotAListOfChars,
10 check_not_access_directly)
11 from rpython.rlib import debug
12 from rpython.rtyper.test.test_llinterp import interpret, gengraph
13
14 @pytest.fixture
15 def debuglog(monkeypatch):
16 dlog = debug.DebugLog()
17 monkeypatch.setattr(debug, '_log', dlog)
18 return dlog
19
20 def test_check_annotation():
21 class Error(Exception):
22 pass
23
24 def checker(ann, bk):
25 from rpython.annotator.model import SomeList, SomeInteger
26 if not isinstance(ann, SomeList):
27 raise Error()
28 if not isinstance(ann.listdef.listitem.s_value, SomeInteger):
29 raise Error()
30
31 def f(x):
32 result = [x]
33 check_annotation(result, checker)
34 return result
35
36 interpret(f, [3])
37
38 def g(x):
39 check_annotation(x, checker)
40 return x
41
42 py.test.raises(Error, "interpret(g, [3])")
43
44 def test_check_nonneg():
45 def f(x):
46 assert x >= 5
47 check_nonneg(x)
48 interpret(f, [9])
49
50 def g(x):
51 check_nonneg(x-1)
52 py.test.raises(IntegerCanBeNegative, interpret, g, [9])
53
54 def test_make_sure_not_resized():
55 from rpython.annotator.listdef import ListChangeUnallowed
56 def f():
57 result = [1,2,3]
58 make_sure_not_resized(result)
59 result.append(4)
60 return len(result)
61
62 py.test.raises(ListChangeUnallowed, interpret, f, [],
63 list_comprehension_operations=True)
64
65 def test_make_sure_not_resized_annorder():
66 def f(n):
67 if n > 5:
68 result = None
69 else:
70 result = [1,2,3]
71 make_sure_not_resized(result)
72 interpret(f, [10])
73
74 def test_mark_dict_non_null():
75 def f():
76 d = {"ac": "bx"}
77 mark_dict_non_null(d)
78 return d
79
80 t, typer, graph = gengraph(f, [])
81 assert sorted(graph.returnblock.inputargs[0].concretetype.TO.entries.TO.OF._flds.keys()) == ['key', 'value']
82
83
84 def test_check_list_of_chars():
85 def f(x):
86 result = []
87 check_list_of_chars(result)
88 result = [chr(x), 'a']
89 check_list_of_chars(result)
90 result = [unichr(x)]
91 check_list_of_chars(result)
92 return result
93
94 interpret(f, [3])
95
96 def g(x):
97 result = ['a', 'b', 'c', '']
98 check_list_of_chars(result)
99 return x
100
101 py.test.raises(NotAListOfChars, "interpret(g, [3])")
102
103
104 def test_debug_print_start_stop(debuglog):
105 def f(x):
106 debug_start("mycat")
107 debug_print("foo", 2, "bar", x)
108 debug_stop("mycat")
109 debug_flush() # does nothing
110 debug_offset() # should not explode at least
111 return have_debug_prints()
112
113 res = f(3)
114 assert res is True
115 assert debuglog == [("mycat", [('debug_print', 'foo', 2, 'bar', 3)])]
116 debuglog.reset()
117
118 res = interpret(f, [3])
119 assert res is True
120 assert debuglog == [("mycat", [('debug_print', 'foo', 2, 'bar', 3)])]
121
122 def test_debuglog_summary(debuglog):
123 debug_start('foo')
124 debug_start('bar') # this is nested, so not counted in the summary by default
125 debug_stop('bar')
126 debug_stop('foo')
127 debug_start('foo')
128 debug_stop('foo')
129 debug_start('bar')
130 debug_stop('bar')
131 #
132 assert debuglog.summary() == {'foo': 2, 'bar': 1}
133 assert debuglog.summary(flatten=True) == {'foo': 2, 'bar': 2}
134
135 def test_debug_start_stop_timestamp():
136 import time
137 def f(timestamp):
138 ts_a = debug_start('foo', timestamp=timestamp)
139 # simulate some CPU time
140 t = time.time()
141 while time.time()-t < 0.02:
142 pass
143 ts_b = debug_stop('foo', timestamp=timestamp)
144 return ts_b - ts_a
145
146 assert f(False) == 0
147 assert f(True) > 0
148 #
149 res = interpret(f, [False])
150 assert res == 0
151 res = interpret(f, [True])
152 assert res > 0
153
154
155 def test_debug_print_traceback():
156 from rpython.translator.c.test.test_genc import compile
157 from rpython.rtyper.lltypesystem import lltype
158 from rpython.rtyper.lltypesystem.lloperation import llop
159
160 def ggg(n):
161 if n < 10:
162 ggg(n + 1)
163 else:
164 raise ValueError
165 def recovery():
166 llop.debug_print_traceback(lltype.Void)
167 recovery._dont_inline_ = True
168 def fff():
169 try:
170 ggg(0)
171 except:
172 recovery()
173
174 fn = compile(fff, [], return_stderr=True)
175 stderr = fn()
176 assert 'RPython traceback:\n' in stderr
177 assert stderr.count('entry_point') == 1
178 assert stderr.count('ggg') == 11
179 assert stderr.count('recovery') == 0
180
181 def test_check_not_access_directly():
182 from rpython.rlib import jit
183 class Frame(object):
184 _virtualizable_ = []
185 def meth(self):
186 check_not_access_directly(self)
187
188 def f(n):
189 x = Frame()
190 x = jit.hint(x, access_directly=True)
191 x.meth()
192 with pytest.raises(AssertionError):
193 interpret(f, [9])