Debian Codegraph @

pypy/interpreter/test/test_executioncontext.py

raw symbols SCIP
1 import py
2 from pypy.interpreter import executioncontext
3 from pypy.interpreter.error import OperationError
4
5 class Finished(Exception):
6 pass
7
8
9 class TestExecutionContext:
10 def test_action(self):
11
12 class DemoAction(executioncontext.AsyncAction):
13 counter = 0
14 def perform(self, ec, frame):
15 self.counter += 1
16 if self.counter == 10:
17 raise Finished
18
19 space = self.space
20 a1 = DemoAction(space)
21 for i in range(20):
22 # assert does not raise:
23 space.appexec([], """():
24 n = 5
25 return n + 2
26 """)
27 try:
28 for i in range(20):
29 a1.fire()
30 space.appexec([], """():
31 n = 5
32 return n + 2
33 """)
34 assert a1.counter == i + 1
35 except Finished:
36 pass
37 assert i == 9
38
39 def test_action_queue(self):
40 events = []
41
42 class Action1(executioncontext.AsyncAction):
43 def perform(self, ec, frame):
44 events.append('one')
45
46 class Action2(executioncontext.AsyncAction):
47 def perform(self, ec, frame):
48 events.append('two')
49
50 space = self.space
51 a1 = Action1(space)
52 a2 = Action2(space)
53 a1.fire()
54 a2.fire()
55 space.appexec([], """():
56 n = 5
57 return n + 2
58 """)
59 assert events == ['one', 'two']
60 #
61 events[:] = []
62 a1.fire()
63 space.appexec([], """():
64 n = 5
65 return n + 2
66 """)
67 assert events == ['one']
68
69 def test_fire_inside_perform(self):
70 # test what happens if we call AsyncAction.fire() while we are in the
71 # middle of an AsyncAction.perform(). In particular, this happens when
72 # PyObjectDeallocAction.fire() is called by rawrefcount: see issue
73 # 2805
74 events = []
75
76 class Action1(executioncontext.AsyncAction):
77 _count = 0
78
79 def perform(self, ec, frame):
80 events.append('one')
81 if self._count == 0:
82 # a1 is no longer in the queue, so it will be enqueued
83 a1.fire()
84 #
85 # a2 is still in the queue, so the fire() is ignored and
86 # it's performed in its normal order, i.e. BEFORE a3
87 a2.fire()
88 self._count += 1
89
90 class Action2(executioncontext.AsyncAction):
91 def perform(self, ec, frame):
92 events.append('two')
93
94 class Action3(executioncontext.AsyncAction):
95 def perform(self, ec, frame):
96 events.append('three')
97
98 space = self.space
99 a1 = Action1(space)
100 a2 = Action2(space)
101 a3 = Action3(space)
102 a1.fire()
103 a2.fire()
104 a3.fire()
105 space.appexec([], """():
106 pass
107 """)
108 assert events == ['one', 'two', 'three', 'one']
109
110
111 def test_periodic_action(self):
112 from pypy.interpreter.executioncontext import ActionFlag
113
114 class DemoAction(executioncontext.PeriodicAsyncAction):
115 counter = 0
116 def perform(self, ec, frame):
117 self.counter += 1
118 print '->', self.counter
119 if self.counter == 3:
120 raise Finished
121
122 space = self.space
123 a2 = DemoAction(space)
124 try:
125 space.actionflag.setcheckinterval(100)
126 space.actionflag.register_periodic_action(a2, True)
127 try:
128 for i in range(500):
129 space.appexec([], """():
130 n = 5
131 return n + 2
132 """)
133 except Finished:
134 pass
135 finally:
136 space.actionflag = ActionFlag() # reset to default
137 assert 10 < i < 110
138
139 def test_llprofile(self):
140 l = []
141
142 def profile_func(space, w_arg, frame, event, w_aarg):
143 assert w_arg is space.w_None
144 l.append(event)
145
146 space = self.space
147 space.getexecutioncontext().setllprofile(profile_func, space.w_None)
148 space.appexec([], """():
149 pass
150 """)
151 space.getexecutioncontext().setllprofile(None, None)
152 assert l[-4:] == ['call', 'return', 'call', 'return']
153
154 def test_llprofile_c_call(self):
155 from pypy.interpreter.function import Function, Method
156 l = []
157 seen = []
158 space = self.space
159
160 def profile_func(space, w_arg, frame, event, w_func):
161 assert w_arg is space.w_None
162 l.append(event)
163 if event == 'c_call':
164 seen.append(w_func)
165
166 def check_snippet(snippet, expected_c_call):
167 del l[:]
168 del seen[:]
169 space.getexecutioncontext().setllprofile(profile_func,
170 space.w_None)
171 space.appexec([], """():
172 %s
173 return
174 """ % snippet)
175 space.getexecutioncontext().setllprofile(None, None)
176 assert l[-6:] == ['call', 'return', 'call', 'c_call', 'c_return', 'return']
177 if isinstance(seen[-1], Method):
178 w_class = space.type(seen[-1].w_instance)
179 found = 'method %s of %s' % (
180 seen[-1].w_function.name,
181 w_class.getname(space))
182 else:
183 assert isinstance(seen[-1], Function)
184 found = 'builtin %s' % seen[-1].name
185 assert found == expected_c_call
186
187 check_snippet('l = []; l.append(42)', 'method append of list')
188 check_snippet('max(1, 2)', 'builtin max')
189 check_snippet('args = (1, 2); max(*args)', 'builtin max')
190 check_snippet('max(1, 2, **{})', 'builtin max')
191 check_snippet('args = (1, 2); max(*args, **{})', 'builtin max')
192 check_snippet('abs(val=0)', 'builtin abs')
193
194 def test_llprofile_c_exception(self):
195 l = []
196
197 def profile_func(space, w_arg, frame, event, w_aarg):
198 assert w_arg is space.w_None
199 l.append(event)
200
201 space = self.space
202 space.getexecutioncontext().setllprofile(profile_func, space.w_None)
203
204 def check_snippet(snippet):
205 space.appexec([], """():
206 try:
207 %s
208 except:
209 pass
210 return
211 """ % snippet)
212 space.getexecutioncontext().setllprofile(None, None)
213 assert l[-6:] == ['call', 'return', 'call', 'c_call', 'c_exception', 'return']
214
215 check_snippet('d = {}; d.__getitem__(42)')
216
217 def test_c_call_setprofile_outer_frame(self):
218 space = self.space
219 w_events = space.appexec([], """():
220 import sys
221 l = []
222 def profile(frame, event, arg):
223 l.append(event)
224
225 def foo():
226 sys.setprofile(profile)
227
228 def bar():
229 foo()
230 max(1, 2)
231
232 bar()
233 sys.setprofile(None)
234 return l
235 """)
236 events = space.unwrap(w_events)
237 assert events == ['return', 'c_call', 'c_return', 'return', 'c_call']
238
239 def test_c_call_setprofile_kwargs(self):
240 space = self.space
241 w_events = space.appexec([], """():
242 import sys
243 l = []
244 def profile(frame, event, arg):
245 l.append(event)
246
247 def bar():
248 sys.setprofile(profile)
249 [].sort(reverse=True)
250 sys.setprofile(None)
251
252 bar()
253 return l
254 """)
255 events = space.unwrap(w_events)
256 assert events == ['c_call', 'c_return', 'c_call']
257
258 def test_c_call_setprofile_strange_method(self):
259 space = self.space
260 w_events = space.appexec([], """():
261 import sys
262 class A(object):
263 def __init__(self, value):
264 self.value = value
265 def meth(self):
266 pass
267 MethodType = type(A(0).meth)
268 strangemeth = MethodType(A, 42)
269 l = []
270 def profile(frame, event, arg):
271 l.append(event)
272
273 def foo():
274 sys.setprofile(profile)
275
276 def bar():
277 foo()
278 strangemeth()
279
280 bar()
281 sys.setprofile(None)
282 return l
283 """)
284 events = space.unwrap(w_events)
285 assert events == ['return', 'call', 'return', 'return', 'c_call']
286
287 def test_c_call_profiles_immediately(self):
288 space = self.space
289 w_events = space.appexec([], """():
290 import sys
291 l = []
292 def profile(frame, event, arg):
293 l.append((event, arg))
294
295 def bar():
296 sys.setprofile(profile)
297 max(3, 4)
298
299 bar()
300 sys.setprofile(None)
301 return l
302 """)
303 events = space.unwrap(w_events)
304 assert [i[0] for i in events] == ['c_call', 'c_return', 'return', 'c_call']
305 assert events[0][1] == events[1][1]
306
307 def test_profile_and_exception(self):
308 space = self.space
309 w_res = space.appexec([], """():
310 l = []
311
312 def profile(*args):
313 l.append(sys.exc_info()[0])
314
315 import sys
316 try:
317 sys.setprofile(profile)
318 try:
319 x
320 except:
321 expected = sys.exc_info()[0]
322 assert expected is NameError
323 for i in l:
324 assert expected is l[0]
325 finally:
326 sys.setprofile(None)
327 """)
328
329
330 class AppTestProfile:
331
332 def test_return(self):
333 import sys
334 l = []
335 def profile(frame, event, arg):
336 l.append((event, arg))
337
338 def bar(x):
339 return 40 + x
340
341 sys.setprofile(profile)
342 bar(2)
343 sys.setprofile(None)
344 assert l == [('call', None),
345 ('return', 42),
346 ('c_call', sys.setprofile)], repr(l)
347
348 def test_c_return(self):
349 import sys
350 l = []
351 def profile(frame, event, arg):
352 l.append((event, arg))
353
354 sys.setprofile(profile)
355 max(2, 42)
356 sys.setprofile(None)
357 assert l == [('c_call', max),
358 ('c_return', max),
359 ('c_call', sys.setprofile)], repr(l)
360
361 def test_exception(self):
362 import sys
363 l = []
364 def profile(frame, event, arg):
365 l.append((event, arg))
366
367 def f():
368 raise ValueError("foo")
369
370 sys.setprofile(profile)
371 try:
372 f()
373 except ValueError:
374 pass
375 sys.setprofile(None)
376 assert l == [('call', None),
377 ('return', None),
378 ('c_call', sys.setprofile)], repr(l)
379
380 def test_c_exception(self):
381 import sys
382 l = []
383 def profile(frame, event, arg):
384 l.append((event, arg))
385
386 sys.setprofile(profile)
387 try:
388 divmod(5, 0)
389 except ZeroDivisionError:
390 pass
391 sys.setprofile(None)
392 assert l == [('c_call', divmod),
393 ('c_exception', divmod),
394 ('c_call', sys.setprofile)], repr(l)