Debian Codegraph @

Lib/test/test_monitoring.py

raw
1 """Test suite for the sys.monitoring."""
2
3 import collections
4 import dis
5 import functools
6 import inspect
7 import math
8 import operator
9 import sys
10 import textwrap
11 import types
12 import unittest
13
14 import test.support
15 from test.support import import_helper, requires_specialization_ft, script_helper
16
17 _testcapi = import_helper.import_module("_testcapi")
18 _testinternalcapi = import_helper.import_module("_testinternalcapi")
19
20 PAIR = (0,1)
21
22 def f1():
23 pass
24
25 def f2():
26 len([])
27 sys.getsizeof(0)
28
29 def floop():
30 for item in PAIR:
31 pass
32
33 def gen():
34 yield
35 yield
36
37 def g1():
38 for _ in gen():
39 pass
40
41 TEST_TOOL = 2
42 TEST_TOOL2 = 3
43 TEST_TOOL3 = 4
44
45 def nth_line(func, offset):
46 return func.__code__.co_firstlineno + offset
47
48 class MonitoringBasicTest(unittest.TestCase):
49
50 def tearDown(self):
51 sys.monitoring.free_tool_id(TEST_TOOL)
52
53 def test_has_objects(self):
54 m = sys.monitoring
55 m.events
56 m.use_tool_id
57 m.clear_tool_id
58 m.free_tool_id
59 m.get_tool
60 m.get_events
61 m.set_events
62 m.get_local_events
63 m.set_local_events
64 m.register_callback
65 m.restart_events
66 m.DISABLE
67 m.MISSING
68 m.events.NO_EVENTS
69
70 def test_tool(self):
71 sys.monitoring.use_tool_id(TEST_TOOL, "MonitoringTest.Tool")
72 self.assertEqual(sys.monitoring.get_tool(TEST_TOOL), "MonitoringTest.Tool")
73 sys.monitoring.set_events(TEST_TOOL, 15)
74 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), 15)
75 sys.monitoring.set_events(TEST_TOOL, 0)
76 with self.assertRaises(ValueError):
77 sys.monitoring.set_events(TEST_TOOL, sys.monitoring.events.C_RETURN)
78 with self.assertRaises(ValueError):
79 sys.monitoring.set_events(TEST_TOOL, sys.monitoring.events.C_RAISE)
80 sys.monitoring.free_tool_id(TEST_TOOL)
81 self.assertEqual(sys.monitoring.get_tool(TEST_TOOL), None)
82 with self.assertRaises(ValueError):
83 sys.monitoring.set_events(TEST_TOOL, sys.monitoring.events.CALL)
84
85 def test_clear(self):
86 events = []
87 sys.monitoring.use_tool_id(TEST_TOOL, "MonitoringTest.Tool")
88 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, lambda *args: events.append(args))
89 sys.monitoring.register_callback(TEST_TOOL, E.LINE, lambda *args: events.append(args))
90 def f():
91 a = 1
92 sys.monitoring.set_local_events(TEST_TOOL, f.__code__, E.LINE)
93 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
94
95 f()
96 sys.monitoring.clear_tool_id(TEST_TOOL)
97 f()
98
99 # the first f() should trigger a PY_START and a LINE event
100 # the second f() after clear_tool_id should not trigger any event
101 # the callback function should be cleared as well
102 self.assertEqual(len(events), 2)
103 callback = sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
104 self.assertIs(callback, None)
105
106 sys.monitoring.free_tool_id(TEST_TOOL)
107
108 events = []
109 sys.monitoring.use_tool_id(TEST_TOOL, "MonitoringTest.Tool")
110 sys.monitoring.register_callback(TEST_TOOL, E.LINE, lambda *args: events.append(args))
111 sys.monitoring.set_local_events(TEST_TOOL, f.__code__, E.LINE)
112 f()
113 sys.monitoring.free_tool_id(TEST_TOOL)
114 sys.monitoring.use_tool_id(TEST_TOOL, "MonitoringTest.Tool")
115 f()
116 # the first f() should trigger a LINE event, and even if we use the
117 # tool id immediately after freeing it, the second f() should not
118 # trigger any event
119 self.assertEqual(len(events), 1)
120 sys.monitoring.free_tool_id(TEST_TOOL)
121
122
123 class MonitoringTestBase:
124
125 def setUp(self):
126 # Check that a previous test hasn't left monitoring on.
127 for tool in range(6):
128 self.assertEqual(sys.monitoring.get_events(tool), 0)
129 self.assertIs(sys.monitoring.get_tool(TEST_TOOL), None)
130 self.assertIs(sys.monitoring.get_tool(TEST_TOOL2), None)
131 self.assertIs(sys.monitoring.get_tool(TEST_TOOL3), None)
132 sys.monitoring.use_tool_id(TEST_TOOL, "test " + self.__class__.__name__)
133 sys.monitoring.use_tool_id(TEST_TOOL2, "test2 " + self.__class__.__name__)
134 sys.monitoring.use_tool_id(TEST_TOOL3, "test3 " + self.__class__.__name__)
135
136 def tearDown(self):
137 # Check that test hasn't left monitoring on.
138 for tool in range(6):
139 self.assertEqual(sys.monitoring.get_events(tool), 0)
140 sys.monitoring.free_tool_id(TEST_TOOL)
141 sys.monitoring.free_tool_id(TEST_TOOL2)
142 sys.monitoring.free_tool_id(TEST_TOOL3)
143
144
145 class MonitoringCountTest(MonitoringTestBase, unittest.TestCase):
146
147 def check_event_count(self, func, event, expected):
148
149 class Counter:
150 def __init__(self):
151 self.count = 0
152 def __call__(self, *args):
153 self.count += 1
154
155 counter = Counter()
156 sys.monitoring.register_callback(TEST_TOOL, event, counter)
157 if event == E.C_RETURN or event == E.C_RAISE:
158 sys.monitoring.set_events(TEST_TOOL, E.CALL)
159 else:
160 sys.monitoring.set_events(TEST_TOOL, event)
161 self.assertEqual(counter.count, 0)
162 counter.count = 0
163 func()
164 self.assertEqual(counter.count, expected)
165 prev = sys.monitoring.register_callback(TEST_TOOL, event, None)
166 counter.count = 0
167 func()
168 self.assertEqual(counter.count, 0)
169 self.assertEqual(prev, counter)
170 sys.monitoring.set_events(TEST_TOOL, 0)
171
172 def test_start_count(self):
173 self.check_event_count(f1, E.PY_START, 1)
174
175 def test_resume_count(self):
176 self.check_event_count(g1, E.PY_RESUME, 2)
177
178 def test_return_count(self):
179 self.check_event_count(f1, E.PY_RETURN, 1)
180
181 def test_call_count(self):
182 self.check_event_count(f2, E.CALL, 3)
183
184 def test_c_return_count(self):
185 self.check_event_count(f2, E.C_RETURN, 2)
186
187
188 E = sys.monitoring.events
189
190 INSTRUMENTED_EVENTS = [
191 (E.PY_START, "start"),
192 (E.PY_RESUME, "resume"),
193 (E.PY_RETURN, "return"),
194 (E.PY_YIELD, "yield"),
195 (E.JUMP, "jump"),
196 (E.BRANCH, "branch"),
197 ]
198
199 EXCEPT_EVENTS = [
200 (E.RAISE, "raise"),
201 (E.PY_UNWIND, "unwind"),
202 (E.EXCEPTION_HANDLED, "exception_handled"),
203 ]
204
205 SIMPLE_EVENTS = INSTRUMENTED_EVENTS + EXCEPT_EVENTS + [
206 (E.C_RAISE, "c_raise"),
207 (E.C_RETURN, "c_return"),
208 ]
209
210
211 SIMPLE_EVENT_SET = functools.reduce(operator.or_, [ev for (ev, _) in SIMPLE_EVENTS], 0) | E.CALL
212
213
214 def just_pass():
215 pass
216
217 just_pass.events = [
218 "py_call",
219 "start",
220 "return",
221 ]
222
223 def just_raise():
224 raise Exception
225
226 just_raise.events = [
227 'py_call',
228 "start",
229 "raise",
230 "unwind",
231 ]
232
233 def just_call():
234 len([])
235
236 just_call.events = [
237 'py_call',
238 "start",
239 "c_call",
240 "c_return",
241 "return",
242 ]
243
244 def caught():
245 try:
246 1/0
247 except Exception:
248 pass
249
250 caught.events = [
251 'py_call',
252 "start",
253 "raise",
254 "exception_handled",
255 "branch",
256 "return",
257 ]
258
259 def nested_call():
260 just_pass()
261
262 nested_call.events = [
263 "py_call",
264 "start",
265 "py_call",
266 "start",
267 "return",
268 "return",
269 ]
270
271 PY_CALLABLES = (types.FunctionType, types.MethodType)
272
273 class MonitoringEventsBase(MonitoringTestBase):
274
275 def gather_events(self, func):
276 events = []
277 for event, event_name in SIMPLE_EVENTS:
278 def record(*args, event_name=event_name):
279 events.append(event_name)
280 sys.monitoring.register_callback(TEST_TOOL, event, record)
281 def record_call(code, offset, obj, arg):
282 if isinstance(obj, PY_CALLABLES):
283 events.append("py_call")
284 else:
285 events.append("c_call")
286 sys.monitoring.register_callback(TEST_TOOL, E.CALL, record_call)
287 sys.monitoring.set_events(TEST_TOOL, SIMPLE_EVENT_SET)
288 events = []
289 try:
290 func()
291 except:
292 pass
293 sys.monitoring.set_events(TEST_TOOL, 0)
294 #Remove the final event, the call to `sys.monitoring.set_events`
295 events = events[:-1]
296 return events
297
298 def check_events(self, func, expected=None):
299 events = self.gather_events(func)
300 if expected is None:
301 expected = func.events
302 self.assertEqual(events, expected)
303
304 class MonitoringEventsTest(MonitoringEventsBase, unittest.TestCase):
305
306 def test_just_pass(self):
307 self.check_events(just_pass)
308
309 def test_just_raise(self):
310 try:
311 self.check_events(just_raise)
312 except Exception:
313 pass
314 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), 0)
315
316 def test_just_call(self):
317 self.check_events(just_call)
318
319 def test_caught(self):
320 self.check_events(caught)
321
322 def test_nested_call(self):
323 self.check_events(nested_call)
324
325 UP_EVENTS = (E.C_RETURN, E.C_RAISE, E.PY_RETURN, E.PY_UNWIND, E.PY_YIELD)
326 DOWN_EVENTS = (E.PY_START, E.PY_RESUME)
327
328 from test.profilee import testfunc
329
330 class SimulateProfileTest(MonitoringEventsBase, unittest.TestCase):
331
332 def test_balanced(self):
333 events = self.gather_events(testfunc)
334 c = collections.Counter(events)
335 self.assertEqual(c["c_call"], c["c_return"])
336 self.assertEqual(c["start"], c["return"] + c["unwind"])
337 self.assertEqual(c["raise"], c["exception_handled"] + c["unwind"])
338
339 def test_frame_stack(self):
340 self.maxDiff = None
341 stack = []
342 errors = []
343 seen = set()
344 def up(*args):
345 frame = sys._getframe(1)
346 if not stack:
347 errors.append("empty")
348 else:
349 expected = stack.pop()
350 if frame != expected:
351 errors.append(f" Popping {frame} expected {expected}")
352 def down(*args):
353 frame = sys._getframe(1)
354 stack.append(frame)
355 seen.add(frame.f_code)
356 def call(code, offset, callable, arg):
357 if not isinstance(callable, PY_CALLABLES):
358 stack.append(sys._getframe(1))
359 for event in UP_EVENTS:
360 sys.monitoring.register_callback(TEST_TOOL, event, up)
361 for event in DOWN_EVENTS:
362 sys.monitoring.register_callback(TEST_TOOL, event, down)
363 sys.monitoring.register_callback(TEST_TOOL, E.CALL, call)
364 sys.monitoring.set_events(TEST_TOOL, SIMPLE_EVENT_SET)
365 testfunc()
366 sys.monitoring.set_events(TEST_TOOL, 0)
367 self.assertEqual(errors, [])
368 self.assertEqual(stack, [sys._getframe()])
369 self.assertEqual(len(seen), 9)
370
371
372 class CounterWithDisable:
373
374 def __init__(self):
375 self.disable = False
376 self.count = 0
377
378 def __call__(self, *args):
379 self.count += 1
380 if self.disable:
381 return sys.monitoring.DISABLE
382
383
384 class RecorderWithDisable:
385
386 def __init__(self, events):
387 self.disable = False
388 self.events = events
389
390 def __call__(self, code, event):
391 self.events.append(event)
392 if self.disable:
393 return sys.monitoring.DISABLE
394
395
396 class MontoringDisableAndRestartTest(MonitoringTestBase, unittest.TestCase):
397
398 def test_disable(self):
399 try:
400 counter = CounterWithDisable()
401 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, counter)
402 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
403 self.assertEqual(counter.count, 0)
404 counter.count = 0
405 f1()
406 self.assertEqual(counter.count, 1)
407 counter.disable = True
408 counter.count = 0
409 f1()
410 self.assertEqual(counter.count, 1)
411 counter.count = 0
412 f1()
413 self.assertEqual(counter.count, 0)
414 sys.monitoring.set_events(TEST_TOOL, 0)
415 finally:
416 sys.monitoring.restart_events()
417
418 def test_restart(self):
419 try:
420 counter = CounterWithDisable()
421 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, counter)
422 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
423 counter.disable = True
424 f1()
425 counter.count = 0
426 f1()
427 self.assertEqual(counter.count, 0)
428 sys.monitoring.restart_events()
429 counter.count = 0
430 f1()
431 self.assertEqual(counter.count, 1)
432 sys.monitoring.set_events(TEST_TOOL, 0)
433 finally:
434 sys.monitoring.restart_events()
435
436
437 class MultipleMonitorsTest(MonitoringTestBase, unittest.TestCase):
438
439 def test_two_same(self):
440 try:
441 self.assertEqual(sys.monitoring._all_events(), {})
442 counter1 = CounterWithDisable()
443 counter2 = CounterWithDisable()
444 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, counter1)
445 sys.monitoring.register_callback(TEST_TOOL2, E.PY_START, counter2)
446 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
447 sys.monitoring.set_events(TEST_TOOL2, E.PY_START)
448 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), E.PY_START)
449 self.assertEqual(sys.monitoring.get_events(TEST_TOOL2), E.PY_START)
450 self.assertEqual(sys.monitoring._all_events(), {'PY_START': (1 << TEST_TOOL) | (1 << TEST_TOOL2)})
451 counter1.count = 0
452 counter2.count = 0
453 f1()
454 count1 = counter1.count
455 count2 = counter2.count
456 self.assertEqual((count1, count2), (1, 1))
457 finally:
458 sys.monitoring.set_events(TEST_TOOL, 0)
459 sys.monitoring.set_events(TEST_TOOL2, 0)
460 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, None)
461 sys.monitoring.register_callback(TEST_TOOL2, E.PY_START, None)
462 self.assertEqual(sys.monitoring._all_events(), {})
463
464 def test_three_same(self):
465 try:
466 self.assertEqual(sys.monitoring._all_events(), {})
467 counter1 = CounterWithDisable()
468 counter2 = CounterWithDisable()
469 counter3 = CounterWithDisable()
470 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, counter1)
471 sys.monitoring.register_callback(TEST_TOOL2, E.PY_START, counter2)
472 sys.monitoring.register_callback(TEST_TOOL3, E.PY_START, counter3)
473 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
474 sys.monitoring.set_events(TEST_TOOL2, E.PY_START)
475 sys.monitoring.set_events(TEST_TOOL3, E.PY_START)
476 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), E.PY_START)
477 self.assertEqual(sys.monitoring.get_events(TEST_TOOL2), E.PY_START)
478 self.assertEqual(sys.monitoring.get_events(TEST_TOOL3), E.PY_START)
479 self.assertEqual(sys.monitoring._all_events(), {'PY_START': (1 << TEST_TOOL) | (1 << TEST_TOOL2) | (1 << TEST_TOOL3)})
480 counter1.count = 0
481 counter2.count = 0
482 counter3.count = 0
483 f1()
484 count1 = counter1.count
485 count2 = counter2.count
486 count3 = counter3.count
487 self.assertEqual((count1, count2, count3), (1, 1, 1))
488 finally:
489 sys.monitoring.set_events(TEST_TOOL, 0)
490 sys.monitoring.set_events(TEST_TOOL2, 0)
491 sys.monitoring.set_events(TEST_TOOL3, 0)
492 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, None)
493 sys.monitoring.register_callback(TEST_TOOL2, E.PY_START, None)
494 sys.monitoring.register_callback(TEST_TOOL3, E.PY_START, None)
495 self.assertEqual(sys.monitoring._all_events(), {})
496
497 def test_two_different(self):
498 try:
499 self.assertEqual(sys.monitoring._all_events(), {})
500 counter1 = CounterWithDisable()
501 counter2 = CounterWithDisable()
502 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, counter1)
503 sys.monitoring.register_callback(TEST_TOOL2, E.PY_RETURN, counter2)
504 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
505 sys.monitoring.set_events(TEST_TOOL2, E.PY_RETURN)
506 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), E.PY_START)
507 self.assertEqual(sys.monitoring.get_events(TEST_TOOL2), E.PY_RETURN)
508 self.assertEqual(sys.monitoring._all_events(), {'PY_START': 1 << TEST_TOOL, 'PY_RETURN': 1 << TEST_TOOL2})
509 counter1.count = 0
510 counter2.count = 0
511 f1()
512 count1 = counter1.count
513 count2 = counter2.count
514 self.assertEqual((count1, count2), (1, 1))
515 finally:
516 sys.monitoring.set_events(TEST_TOOL, 0)
517 sys.monitoring.set_events(TEST_TOOL2, 0)
518 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, None)
519 sys.monitoring.register_callback(TEST_TOOL2, E.PY_RETURN, None)
520 self.assertEqual(sys.monitoring._all_events(), {})
521
522 def test_two_with_disable(self):
523 try:
524 self.assertEqual(sys.monitoring._all_events(), {})
525 counter1 = CounterWithDisable()
526 counter2 = CounterWithDisable()
527 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, counter1)
528 sys.monitoring.register_callback(TEST_TOOL2, E.PY_START, counter2)
529 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
530 sys.monitoring.set_events(TEST_TOOL2, E.PY_START)
531 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), E.PY_START)
532 self.assertEqual(sys.monitoring.get_events(TEST_TOOL2), E.PY_START)
533 self.assertEqual(sys.monitoring._all_events(), {'PY_START': (1 << TEST_TOOL) | (1 << TEST_TOOL2)})
534 counter1.count = 0
535 counter2.count = 0
536 counter1.disable = True
537 f1()
538 count1 = counter1.count
539 count2 = counter2.count
540 self.assertEqual((count1, count2), (1, 1))
541 counter1.count = 0
542 counter2.count = 0
543 f1()
544 count1 = counter1.count
545 count2 = counter2.count
546 self.assertEqual((count1, count2), (0, 1))
547 finally:
548 sys.monitoring.set_events(TEST_TOOL, 0)
549 sys.monitoring.set_events(TEST_TOOL2, 0)
550 sys.monitoring.register_callback(TEST_TOOL, E.PY_START, None)
551 sys.monitoring.register_callback(TEST_TOOL2, E.PY_START, None)
552 self.assertEqual(sys.monitoring._all_events(), {})
553 sys.monitoring.restart_events()
554
555 def test_with_instruction_event(self):
556 """Test that the second tool can set events with instruction events set by the first tool."""
557 def f():
558 pass
559 code = f.__code__
560
561 try:
562 self.assertEqual(sys.monitoring._all_events(), {})
563 sys.monitoring.set_local_events(TEST_TOOL, code, E.INSTRUCTION | E.LINE)
564 sys.monitoring.set_local_events(TEST_TOOL2, code, E.LINE)
565 finally:
566 sys.monitoring.set_events(TEST_TOOL, 0)
567 sys.monitoring.set_events(TEST_TOOL2, 0)
568 self.assertEqual(sys.monitoring._all_events(), {})
569
570
571 class LineMonitoringTest(MonitoringTestBase, unittest.TestCase):
572
573 def test_lines_single(self):
574 try:
575 self.assertEqual(sys.monitoring._all_events(), {})
576 events = []
577 recorder = RecorderWithDisable(events)
578 sys.monitoring.register_callback(TEST_TOOL, E.LINE, recorder)
579 sys.monitoring.set_events(TEST_TOOL, E.LINE)
580 f1()
581 sys.monitoring.set_events(TEST_TOOL, 0)
582 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
583 start = nth_line(LineMonitoringTest.test_lines_single, 0)
584 self.assertEqual(events, [start+7, nth_line(f1, 1), start+8])
585 finally:
586 sys.monitoring.set_events(TEST_TOOL, 0)
587 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
588 self.assertEqual(sys.monitoring._all_events(), {})
589 sys.monitoring.restart_events()
590
591 def test_lines_loop(self):
592 try:
593 self.assertEqual(sys.monitoring._all_events(), {})
594 events = []
595 recorder = RecorderWithDisable(events)
596 sys.monitoring.register_callback(TEST_TOOL, E.LINE, recorder)
597 sys.monitoring.set_events(TEST_TOOL, E.LINE)
598 floop()
599 sys.monitoring.set_events(TEST_TOOL, 0)
600 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
601 start = nth_line(LineMonitoringTest.test_lines_loop, 0)
602 floop_1 = nth_line(floop, 1)
603 floop_2 = nth_line(floop, 2)
604 self.assertEqual(
605 events,
606 [start+7, floop_1, floop_2, floop_1, floop_2, floop_1, start+8]
607 )
608 finally:
609 sys.monitoring.set_events(TEST_TOOL, 0)
610 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
611 self.assertEqual(sys.monitoring._all_events(), {})
612 sys.monitoring.restart_events()
613
614 def test_lines_two(self):
615 try:
616 self.assertEqual(sys.monitoring._all_events(), {})
617 events = []
618 recorder = RecorderWithDisable(events)
619 events2 = []
620 recorder2 = RecorderWithDisable(events2)
621 sys.monitoring.register_callback(TEST_TOOL, E.LINE, recorder)
622 sys.monitoring.register_callback(TEST_TOOL2, E.LINE, recorder2)
623 sys.monitoring.set_events(TEST_TOOL, E.LINE); sys.monitoring.set_events(TEST_TOOL2, E.LINE)
624 f1()
625 sys.monitoring.set_events(TEST_TOOL, 0); sys.monitoring.set_events(TEST_TOOL2, 0)
626 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
627 sys.monitoring.register_callback(TEST_TOOL2, E.LINE, None)
628 start = nth_line(LineMonitoringTest.test_lines_two, 0)
629 expected = [start+10, nth_line(f1, 1), start+11]
630 self.assertEqual(events, expected)
631 self.assertEqual(events2, expected)
632 finally:
633 sys.monitoring.set_events(TEST_TOOL, 0)
634 sys.monitoring.set_events(TEST_TOOL2, 0)
635 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
636 sys.monitoring.register_callback(TEST_TOOL2, E.LINE, None)
637 self.assertEqual(sys.monitoring._all_events(), {})
638 sys.monitoring.restart_events()
639
640 def check_lines(self, func, expected, tool=TEST_TOOL):
641 try:
642 self.assertEqual(sys.monitoring._all_events(), {})
643 events = []
644 recorder = RecorderWithDisable(events)
645 sys.monitoring.register_callback(tool, E.LINE, recorder)
646 sys.monitoring.set_events(tool, E.LINE)
647 func()
648 sys.monitoring.set_events(tool, 0)
649 sys.monitoring.register_callback(tool, E.LINE, None)
650 lines = [ line - func.__code__.co_firstlineno for line in events[1:-1] ]
651 self.assertEqual(lines, expected)
652 finally:
653 sys.monitoring.set_events(tool, 0)
654
655
656 def test_linear(self):
657
658 def func():
659 line = 1
660 line = 2
661 line = 3
662 line = 4
663 line = 5
664
665 self.check_lines(func, [1,2,3,4,5])
666
667 def test_branch(self):
668 def func():
669 if "true".startswith("t"):
670 line = 2
671 line = 3
672 else:
673 line = 5
674 line = 6
675
676 self.check_lines(func, [1,2,3,6])
677
678 def test_try_except(self):
679
680 def func1():
681 try:
682 line = 2
683 line = 3
684 except:
685 line = 5
686 line = 6
687
688 self.check_lines(func1, [1,2,3,6])
689
690 def func2():
691 try:
692 line = 2
693 raise 3
694 except:
695 line = 5
696 line = 6
697
698 self.check_lines(func2, [1,2,3,4,5,6])
699
700 def test_generator_with_line(self):
701
702 def f():
703 def a():
704 yield
705 def b():
706 yield from a()
707 next(b())
708
709 self.check_lines(f, [1,3,5,4,2,4])
710
711 class TestDisable(MonitoringTestBase, unittest.TestCase):
712
713 def gen(self, cond):
714 for i in range(10):
715 if cond:
716 yield 1
717 else:
718 yield 2
719
720 def raise_handle_reraise(self):
721 try:
722 1/0
723 except:
724 raise
725
726 def test_disable_legal_events(self):
727 for event, name in INSTRUMENTED_EVENTS:
728 try:
729 counter = CounterWithDisable()
730 counter.disable = True
731 sys.monitoring.register_callback(TEST_TOOL, event, counter)
732 sys.monitoring.set_events(TEST_TOOL, event)
733 for _ in self.gen(1):
734 pass
735 self.assertLess(counter.count, 4)
736 finally:
737 sys.monitoring.set_events(TEST_TOOL, 0)
738 sys.monitoring.register_callback(TEST_TOOL, event, None)
739
740
741 def test_disable_illegal_events(self):
742 for event, name in EXCEPT_EVENTS:
743 try:
744 counter = CounterWithDisable()
745 counter.disable = True
746 sys.monitoring.register_callback(TEST_TOOL, event, counter)
747 sys.monitoring.set_events(TEST_TOOL, event)
748 with self.assertRaises(ValueError):
749 self.raise_handle_reraise()
750 finally:
751 sys.monitoring.set_events(TEST_TOOL, 0)
752 sys.monitoring.register_callback(TEST_TOOL, event, None)
753
754
755 class ExceptionRecorder:
756
757 event_type = E.RAISE
758
759 def __init__(self, events):
760 self.events = events
761
762 def __call__(self, code, offset, exc):
763 self.events.append(("raise", type(exc)))
764
765 class CheckEvents(MonitoringTestBase, unittest.TestCase):
766
767 def get_events(self, func, tool, recorders):
768 try:
769 self.assertEqual(sys.monitoring._all_events(), {})
770 event_list = []
771 all_events = 0
772 for recorder in recorders:
773 ev = recorder.event_type
774 sys.monitoring.register_callback(tool, ev, recorder(event_list))
775 all_events |= ev
776 sys.monitoring.set_events(tool, all_events)
777 func()
778 sys.monitoring.set_events(tool, 0)
779 for recorder in recorders:
780 sys.monitoring.register_callback(tool, recorder.event_type, None)
781 return event_list
782 finally:
783 sys.monitoring.set_events(tool, 0)
784 for recorder in recorders:
785 sys.monitoring.register_callback(tool, recorder.event_type, None)
786
787 def check_events(self, func, expected, tool=TEST_TOOL, recorders=(ExceptionRecorder,)):
788 events = self.get_events(func, tool, recorders)
789 self.assertEqual(events, expected)
790
791 def check_balanced(self, func, recorders):
792 events = self.get_events(func, TEST_TOOL, recorders)
793 self.assertEqual(len(events)%2, 0)
794 for r, h in zip(events[::2],events[1::2]):
795 r0 = r[0]
796 self.assertIn(r0, ("raise", "reraise"))
797 h0 = h[0]
798 self.assertIn(h0, ("handled", "unwind"))
799 self.assertEqual(r[1], h[1])
800
801
802 class StopiterationRecorder(ExceptionRecorder):
803
804 event_type = E.STOP_ITERATION
805
806 class ReraiseRecorder(ExceptionRecorder):
807
808 event_type = E.RERAISE
809
810 def __call__(self, code, offset, exc):
811 self.events.append(("reraise", type(exc)))
812
813 class UnwindRecorder(ExceptionRecorder):
814
815 event_type = E.PY_UNWIND
816
817 def __call__(self, code, offset, exc):
818 self.events.append(("unwind", type(exc), code.co_name))
819
820 class ExceptionHandledRecorder(ExceptionRecorder):
821
822 event_type = E.EXCEPTION_HANDLED
823
824 def __call__(self, code, offset, exc):
825 self.events.append(("handled", type(exc)))
826
827 class ThrowRecorder(ExceptionRecorder):
828
829 event_type = E.PY_THROW
830
831 def __call__(self, code, offset, exc):
832 self.events.append(("throw", type(exc)))
833
834 class CallRecorder:
835
836 event_type = E.CALL
837
838 def __init__(self, events):
839 self.events = events
840
841 def __call__(self, code, offset, func, arg):
842 self.events.append(("call", func.__name__, arg))
843
844 class ReturnRecorder:
845
846 event_type = E.PY_RETURN
847
848 def __init__(self, events):
849 self.events = events
850
851 def __call__(self, code, offset, val):
852 self.events.append(("return", code.co_name, val))
853
854
855 class ExceptionMonitoringTest(CheckEvents):
856
857 exception_recorders = (
858 ExceptionRecorder,
859 ReraiseRecorder,
860 ExceptionHandledRecorder,
861 UnwindRecorder
862 )
863
864 def test_simple_try_except(self):
865
866 def func1():
867 try:
868 line = 2
869 raise KeyError
870 except:
871 line = 5
872 line = 6
873
874 self.check_events(func1, [("raise", KeyError)])
875
876 def test_implicit_stop_iteration(self):
877 """Generators are documented as raising a StopIteration
878 when they terminate.
879 However, we don't do that if we can avoid it, for speed.
880 sys.monitoring handles that by injecting a STOP_ITERATION
881 event when we would otherwise have skip the RAISE event.
882 This test checks that both paths record an equivalent event.
883 """
884
885 def gen():
886 yield 1
887 return 2
888
889 def implicit_stop_iteration(iterator=None):
890 if iterator is None:
891 iterator = gen()
892 for _ in iterator:
893 pass
894
895 recorders=(ExceptionRecorder, StopiterationRecorder,)
896 expected = [("raise", StopIteration)]
897
898 # Make sure that the loop is unspecialized, and that it will not
899 # re-specialize immediately, so that we can we can test the
900 # unspecialized version of the loop first.
901 # Note: this assumes that we don't specialize loops over sets.
902 implicit_stop_iteration(set(range(_testinternalcapi.SPECIALIZATION_THRESHOLD)))
903
904 # This will record a RAISE event for the StopIteration.
905 self.check_events(implicit_stop_iteration, expected, recorders=recorders)
906
907 # Now specialize, so that we see a STOP_ITERATION event.
908 for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN):
909 implicit_stop_iteration()
910
911 # This will record a STOP_ITERATION event for the StopIteration.
912 self.check_events(implicit_stop_iteration, expected, recorders=recorders)
913
914 initial = [
915 ("raise", ZeroDivisionError),
916 ("handled", ZeroDivisionError)
917 ]
918
919 reraise = [
920 ("reraise", ZeroDivisionError),
921 ("handled", ZeroDivisionError)
922 ]
923
924 def test_explicit_reraise(self):
925
926 def func():
927 try:
928 try:
929 1/0
930 except:
931 raise
932 except:
933 pass
934
935 self.check_balanced(
936 func,
937 recorders = self.exception_recorders)
938
939 def test_explicit_reraise_named(self):
940
941 def func():
942 try:
943 try:
944 1/0
945 except Exception as ex:
946 raise
947 except:
948 pass
949
950 self.check_balanced(
951 func,
952 recorders = self.exception_recorders)
953
954 def test_implicit_reraise(self):
955
956 def func():
957 try:
958 try:
959 1/0
960 except ValueError:
961 pass
962 except:
963 pass
964
965 self.check_balanced(
966 func,
967 recorders = self.exception_recorders)
968
969
970 def test_implicit_reraise_named(self):
971
972 def func():
973 try:
974 try:
975 1/0
976 except ValueError as ex:
977 pass
978 except:
979 pass
980
981 self.check_balanced(
982 func,
983 recorders = self.exception_recorders)
984
985 def test_try_finally(self):
986
987 def func():
988 try:
989 try:
990 1/0
991 finally:
992 pass
993 except:
994 pass
995
996 self.check_balanced(
997 func,
998 recorders = self.exception_recorders)
999
1000 def test_async_for(self):
1001
1002 def func():
1003
1004 async def async_generator():
1005 for i in range(1):
1006 raise ZeroDivisionError
1007 yield i
1008
1009 async def async_loop():
1010 try:
1011 async for item in async_generator():
1012 pass
1013 except Exception:
1014 pass
1015
1016 try:
1017 async_loop().send(None)
1018 except StopIteration:
1019 pass
1020
1021 self.check_balanced(
1022 func,
1023 recorders = self.exception_recorders)
1024
1025 def test_throw(self):
1026
1027 def gen():
1028 yield 1
1029 yield 2
1030
1031 def func():
1032 try:
1033 g = gen()
1034 next(g)
1035 g.throw(IndexError)
1036 except IndexError:
1037 pass
1038
1039 self.check_balanced(
1040 func,
1041 recorders = self.exception_recorders)
1042
1043 events = self.get_events(
1044 func,
1045 TEST_TOOL,
1046 self.exception_recorders + (ThrowRecorder,)
1047 )
1048 self.assertEqual(events[0], ("throw", IndexError))
1049
1050 @requires_specialization_ft
1051 def test_no_unwind_for_shim_frame(self):
1052 class ValueErrorRaiser:
1053 def __init__(self):
1054 raise ValueError()
1055
1056 def f():
1057 try:
1058 return ValueErrorRaiser()
1059 except ValueError:
1060 pass
1061
1062 for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
1063 f()
1064 recorders = (
1065 ReturnRecorder,
1066 UnwindRecorder
1067 )
1068 events = self.get_events(f, TEST_TOOL, recorders)
1069 adaptive_insts = dis.get_instructions(f, adaptive=True)
1070 self.assertIn(
1071 "CALL_ALLOC_AND_ENTER_INIT",
1072 [i.opname for i in adaptive_insts]
1073 )
1074 #There should be only one unwind event
1075 expected = [
1076 ('unwind', ValueError, '__init__'),
1077 ('return', 'f', None),
1078 ]
1079
1080 self.assertEqual(events, expected)
1081
1082 # gh-140373
1083 def test_gen_unwind(self):
1084 def gen():
1085 yield 1
1086
1087 def f():
1088 g = gen()
1089 next(g)
1090 g.close()
1091
1092 recorders = (
1093 UnwindRecorder,
1094 )
1095 events = self.get_events(f, TEST_TOOL, recorders)
1096 expected = [
1097 ("unwind", GeneratorExit, "gen"),
1098 ]
1099 self.assertEqual(events, expected)
1100
1101 class LineRecorder:
1102
1103 event_type = E.LINE
1104
1105
1106 def __init__(self, events):
1107 self.events = events
1108
1109 def __call__(self, code, line):
1110 self.events.append(("line", code.co_name, line - code.co_firstlineno))
1111
1112 class CEventRecorder:
1113
1114 def __init__(self, events):
1115 self.events = events
1116
1117 def __call__(self, code, offset, func, arg):
1118 self.events.append((self.event_name, func.__name__, arg))
1119
1120 class CReturnRecorder(CEventRecorder):
1121
1122 event_type = E.C_RETURN
1123 event_name = "C return"
1124
1125 class CRaiseRecorder(CEventRecorder):
1126
1127 event_type = E.C_RAISE
1128 event_name = "C raise"
1129
1130 MANY_RECORDERS = ExceptionRecorder, CallRecorder, LineRecorder, CReturnRecorder, CRaiseRecorder
1131
1132 class TestManyEvents(CheckEvents):
1133
1134 def test_simple(self):
1135
1136 def func1():
1137 line1 = 1
1138 line2 = 2
1139 line3 = 3
1140
1141 self.check_events(func1, recorders = MANY_RECORDERS, expected = [
1142 ('line', 'get_events', 10),
1143 ('call', 'func1', sys.monitoring.MISSING),
1144 ('line', 'func1', 1),
1145 ('line', 'func1', 2),
1146 ('line', 'func1', 3),
1147 ('line', 'get_events', 11),
1148 ('call', 'set_events', 2)])
1149
1150 def test_c_call(self):
1151
1152 def func2():
1153 line1 = 1
1154 [].append(2)
1155 line3 = 3
1156
1157 self.check_events(func2, recorders = MANY_RECORDERS, expected = [
1158 ('line', 'get_events', 10),
1159 ('call', 'func2', sys.monitoring.MISSING),
1160 ('line', 'func2', 1),
1161 ('line', 'func2', 2),
1162 ('call', 'append', [2]),
1163 ('C return', 'append', [2]),
1164 ('line', 'func2', 3),
1165 ('line', 'get_events', 11),
1166 ('call', 'set_events', 2)])
1167
1168 def test_try_except(self):
1169
1170 def func3():
1171 try:
1172 line = 2
1173 raise KeyError
1174 except:
1175 line = 5
1176 line = 6
1177
1178 self.check_events(func3, recorders = MANY_RECORDERS, expected = [
1179 ('line', 'get_events', 10),
1180 ('call', 'func3', sys.monitoring.MISSING),
1181 ('line', 'func3', 1),
1182 ('line', 'func3', 2),
1183 ('line', 'func3', 3),
1184 ('raise', KeyError),
1185 ('line', 'func3', 4),
1186 ('line', 'func3', 5),
1187 ('line', 'func3', 6),
1188 ('line', 'get_events', 11),
1189 ('call', 'set_events', 2)])
1190
1191 class InstructionRecorder:
1192
1193 event_type = E.INSTRUCTION
1194
1195 def __init__(self, events):
1196 self.events = events
1197
1198 def __call__(self, code, offset):
1199 # Filter out instructions in check_events to lower noise
1200 if code.co_name != "get_events":
1201 self.events.append(("instruction", code.co_name, offset))
1202
1203
1204 LINE_AND_INSTRUCTION_RECORDERS = InstructionRecorder, LineRecorder
1205
1206 class TestLineAndInstructionEvents(CheckEvents):
1207 maxDiff = None
1208
1209 def test_simple(self):
1210
1211 def func1():
1212 line1 = 1
1213 line2 = 2
1214 line3 = 3
1215
1216 self.check_events(func1, recorders = LINE_AND_INSTRUCTION_RECORDERS, expected = [
1217 ('line', 'get_events', 10),
1218 ('line', 'func1', 1),
1219 ('instruction', 'func1', 2),
1220 ('instruction', 'func1', 4),
1221 ('line', 'func1', 2),
1222 ('instruction', 'func1', 6),
1223 ('instruction', 'func1', 8),
1224 ('line', 'func1', 3),
1225 ('instruction', 'func1', 10),
1226 ('instruction', 'func1', 12),
1227 ('instruction', 'func1', 14),
1228 ('instruction', 'func1', 16),
1229 ('line', 'get_events', 11)])
1230
1231 def test_c_call(self):
1232
1233 def func2():
1234 line1 = 1
1235 [].append(2)
1236 line3 = 3
1237
1238 self.check_events(func2, recorders = LINE_AND_INSTRUCTION_RECORDERS, expected = [
1239 ('line', 'get_events', 10),
1240 ('line', 'func2', 1),
1241 ('instruction', 'func2', 2),
1242 ('instruction', 'func2', 4),
1243 ('line', 'func2', 2),
1244 ('instruction', 'func2', 6),
1245 ('instruction', 'func2', 8),
1246 ('instruction', 'func2', 28),
1247 ('instruction', 'func2', 30),
1248 ('instruction', 'func2', 38),
1249 ('line', 'func2', 3),
1250 ('instruction', 'func2', 40),
1251 ('instruction', 'func2', 42),
1252 ('instruction', 'func2', 44),
1253 ('instruction', 'func2', 46),
1254 ('line', 'get_events', 11)])
1255
1256 def test_try_except(self):
1257
1258 def func3():
1259 try:
1260 line = 2
1261 raise KeyError
1262 except:
1263 line = 5
1264 line = 6
1265
1266 self.check_events(func3, recorders = LINE_AND_INSTRUCTION_RECORDERS, expected = [
1267 ('line', 'get_events', 10),
1268 ('line', 'func3', 1),
1269 ('instruction', 'func3', 2),
1270 ('line', 'func3', 2),
1271 ('instruction', 'func3', 4),
1272 ('instruction', 'func3', 6),
1273 ('line', 'func3', 3),
1274 ('instruction', 'func3', 8),
1275 ('instruction', 'func3', 18),
1276 ('instruction', 'func3', 20),
1277 ('line', 'func3', 4),
1278 ('instruction', 'func3', 22),
1279 ('line', 'func3', 5),
1280 ('instruction', 'func3', 24),
1281 ('instruction', 'func3', 26),
1282 ('instruction', 'func3', 28),
1283 ('line', 'func3', 6),
1284 ('instruction', 'func3', 30),
1285 ('instruction', 'func3', 32),
1286 ('instruction', 'func3', 34),
1287 ('instruction', 'func3', 36),
1288 ('line', 'get_events', 11)])
1289
1290 def test_with_restart(self):
1291 def func1():
1292 line1 = 1
1293 line2 = 2
1294 line3 = 3
1295
1296 self.check_events(func1, recorders = LINE_AND_INSTRUCTION_RECORDERS, expected = [
1297 ('line', 'get_events', 10),
1298 ('line', 'func1', 1),
1299 ('instruction', 'func1', 2),
1300 ('instruction', 'func1', 4),
1301 ('line', 'func1', 2),
1302 ('instruction', 'func1', 6),
1303 ('instruction', 'func1', 8),
1304 ('line', 'func1', 3),
1305 ('instruction', 'func1', 10),
1306 ('instruction', 'func1', 12),
1307 ('instruction', 'func1', 14),
1308 ('instruction', 'func1', 16),
1309 ('line', 'get_events', 11)])
1310
1311 sys.monitoring.restart_events()
1312
1313 self.check_events(func1, recorders = LINE_AND_INSTRUCTION_RECORDERS, expected = [
1314 ('line', 'get_events', 10),
1315 ('line', 'func1', 1),
1316 ('instruction', 'func1', 2),
1317 ('instruction', 'func1', 4),
1318 ('line', 'func1', 2),
1319 ('instruction', 'func1', 6),
1320 ('instruction', 'func1', 8),
1321 ('line', 'func1', 3),
1322 ('instruction', 'func1', 10),
1323 ('instruction', 'func1', 12),
1324 ('instruction', 'func1', 14),
1325 ('instruction', 'func1', 16),
1326 ('line', 'get_events', 11)])
1327
1328 def test_turn_off_only_instruction(self):
1329 """
1330 LINE events should be recorded after INSTRUCTION event is turned off
1331 """
1332 events = []
1333 def line(*args):
1334 events.append("line")
1335 sys.monitoring.set_events(TEST_TOOL, 0)
1336 sys.monitoring.register_callback(TEST_TOOL, E.LINE, line)
1337 sys.monitoring.register_callback(TEST_TOOL, E.INSTRUCTION, lambda *args: None)
1338 sys.monitoring.set_events(TEST_TOOL, E.LINE | E.INSTRUCTION)
1339 sys.monitoring.set_events(TEST_TOOL, E.LINE)
1340 events = []
1341 a = 0
1342 sys.monitoring.set_events(TEST_TOOL, 0)
1343 self.assertGreater(len(events), 0)
1344
1345 class TestInstallIncrementally(MonitoringTestBase, unittest.TestCase):
1346
1347 def check_events(self, func, must_include, tool=TEST_TOOL, recorders=(ExceptionRecorder,)):
1348 try:
1349 self.assertEqual(sys.monitoring._all_events(), {})
1350 event_list = []
1351 all_events = 0
1352 for recorder in recorders:
1353 all_events |= recorder.event_type
1354 sys.monitoring.set_events(tool, all_events)
1355 for recorder in recorders:
1356 sys.monitoring.register_callback(tool, recorder.event_type, recorder(event_list))
1357 func()
1358 sys.monitoring.set_events(tool, 0)
1359 for recorder in recorders:
1360 sys.monitoring.register_callback(tool, recorder.event_type, None)
1361 for line in must_include:
1362 self.assertIn(line, event_list)
1363 finally:
1364 sys.monitoring.set_events(tool, 0)
1365 for recorder in recorders:
1366 sys.monitoring.register_callback(tool, recorder.event_type, None)
1367
1368 @staticmethod
1369 def func1():
1370 line1 = 1
1371
1372 MUST_INCLUDE_LI = [
1373 ('instruction', 'func1', 2),
1374 ('line', 'func1', 2),
1375 ('instruction', 'func1', 4),
1376 ('instruction', 'func1', 6)]
1377
1378 def test_line_then_instruction(self):
1379 recorders = [ LineRecorder, InstructionRecorder ]
1380 self.check_events(self.func1,
1381 recorders = recorders, must_include = self.MUST_INCLUDE_LI)
1382
1383 def test_instruction_then_line(self):
1384 recorders = [ InstructionRecorder, LineRecorder ]
1385 self.check_events(self.func1,
1386 recorders = recorders, must_include = self.MUST_INCLUDE_LI)
1387
1388 @staticmethod
1389 def func2():
1390 len(())
1391
1392 MUST_INCLUDE_CI = [
1393 ('instruction', 'func2', 2),
1394 ('call', 'func2', sys.monitoring.MISSING),
1395 ('call', 'len', ()),
1396 ('instruction', 'func2', 12),
1397 ('instruction', 'func2', 14)]
1398
1399
1400
1401 def test_call_then_instruction(self):
1402 recorders = [ CallRecorder, InstructionRecorder ]
1403 self.check_events(self.func2,
1404 recorders = recorders, must_include = self.MUST_INCLUDE_CI)
1405
1406 def test_instruction_then_call(self):
1407 recorders = [ InstructionRecorder, CallRecorder ]
1408 self.check_events(self.func2,
1409 recorders = recorders, must_include = self.MUST_INCLUDE_CI)
1410
1411 LOCAL_RECORDERS = CallRecorder, LineRecorder, CReturnRecorder, CRaiseRecorder
1412
1413 class TestLocalEvents(MonitoringTestBase, unittest.TestCase):
1414
1415 def check_events(self, func, expected, tool=TEST_TOOL, recorders=()):
1416 try:
1417 self.assertEqual(sys.monitoring._all_events(), {})
1418 event_list = []
1419 all_events = 0
1420 for recorder in recorders:
1421 ev = recorder.event_type
1422 sys.monitoring.register_callback(tool, ev, recorder(event_list))
1423 all_events |= ev
1424 sys.monitoring.set_local_events(tool, func.__code__, all_events)
1425 func()
1426 sys.monitoring.set_local_events(tool, func.__code__, 0)
1427 for recorder in recorders:
1428 sys.monitoring.register_callback(tool, recorder.event_type, None)
1429 self.assertEqual(event_list, expected)
1430 finally:
1431 sys.monitoring.set_local_events(tool, func.__code__, 0)
1432 for recorder in recorders:
1433 sys.monitoring.register_callback(tool, recorder.event_type, None)
1434
1435
1436 def test_simple(self):
1437
1438 def func1():
1439 line1 = 1
1440 line2 = 2
1441 line3 = 3
1442
1443 self.check_events(func1, recorders = LOCAL_RECORDERS, expected = [
1444 ('line', 'func1', 1),
1445 ('line', 'func1', 2),
1446 ('line', 'func1', 3)])
1447
1448 def test_c_call(self):
1449
1450 def func2():
1451 line1 = 1
1452 [].append(2)
1453 line3 = 3
1454
1455 self.check_events(func2, recorders = LOCAL_RECORDERS, expected = [
1456 ('line', 'func2', 1),
1457 ('line', 'func2', 2),
1458 ('call', 'append', [2]),
1459 ('C return', 'append', [2]),
1460 ('line', 'func2', 3)])
1461
1462 def test_try_except(self):
1463
1464 def func3():
1465 try:
1466 line = 2
1467 raise KeyError
1468 except:
1469 line = 5
1470 line = 6
1471
1472 self.check_events(func3, recorders = LOCAL_RECORDERS, expected = [
1473 ('line', 'func3', 1),
1474 ('line', 'func3', 2),
1475 ('line', 'func3', 3),
1476 ('line', 'func3', 4),
1477 ('line', 'func3', 5),
1478 ('line', 'func3', 6)])
1479
1480 def test_set_non_local_event(self):
1481 with self.assertRaises(ValueError):
1482 sys.monitoring.set_local_events(TEST_TOOL, just_call.__code__, E.RAISE)
1483
1484 def line_from_offset(code, offset):
1485 for start, end, line in code.co_lines():
1486 if start <= offset < end:
1487 if line is None:
1488 return f"[offset={offset}]"
1489 return line - code.co_firstlineno
1490 return -1
1491
1492 class JumpRecorder:
1493
1494 event_type = E.JUMP
1495 name = "jump"
1496
1497 def __init__(self, events):
1498 self.events = events
1499
1500 def __call__(self, code, from_, to):
1501 from_line = line_from_offset(code, from_)
1502 to_line = line_from_offset(code, to)
1503 self.events.append((self.name, code.co_name, from_line, to_line))
1504
1505
1506 class BranchRecorder(JumpRecorder):
1507
1508 event_type = E.BRANCH
1509 name = "branch"
1510
1511 class BranchRightRecorder(JumpRecorder):
1512
1513 event_type = E.BRANCH_RIGHT
1514 name = "branch right"
1515
1516 class BranchLeftRecorder(JumpRecorder):
1517
1518 event_type = E.BRANCH_LEFT
1519 name = "branch left"
1520
1521 class JumpOffsetRecorder:
1522
1523 event_type = E.JUMP
1524 name = "jump"
1525
1526 def __init__(self, events, offsets=False):
1527 self.events = events
1528
1529 def __call__(self, code, from_, to):
1530 self.events.append((self.name, code.co_name, from_, to))
1531
1532 class BranchLeftOffsetRecorder(JumpOffsetRecorder):
1533
1534 event_type = E.BRANCH_LEFT
1535 name = "branch left"
1536
1537 class BranchRightOffsetRecorder(JumpOffsetRecorder):
1538
1539 event_type = E.BRANCH_RIGHT
1540 name = "branch right"
1541
1542
1543 JUMP_AND_BRANCH_RECORDERS = JumpRecorder, BranchRecorder
1544 JUMP_BRANCH_AND_LINE_RECORDERS = JumpRecorder, BranchRecorder, LineRecorder
1545 FLOW_AND_LINE_RECORDERS = JumpRecorder, BranchRecorder, LineRecorder, ExceptionRecorder, ReturnRecorder
1546
1547 BRANCHES_RECORDERS = BranchLeftRecorder, BranchRightRecorder
1548 BRANCH_OFFSET_RECORDERS = BranchLeftOffsetRecorder, BranchRightOffsetRecorder
1549
1550 class TestBranchAndJumpEvents(CheckEvents):
1551 maxDiff = None
1552
1553 def test_loop(self):
1554
1555 def func():
1556 x = 1
1557 for a in range(2):
1558 if a:
1559 x = 4
1560 else:
1561 x = 6
1562 7
1563
1564 def whilefunc(n=0):
1565 while n < 3:
1566 n += 1 # line 2
1567 3
1568
1569 self.check_events(func, recorders = JUMP_AND_BRANCH_RECORDERS, expected = [
1570 ('branch', 'func', 2, 2),
1571 ('branch', 'func', 3, 6),
1572 ('jump', 'func', 6, 2),
1573 ('branch', 'func', 2, 2),
1574 ('branch', 'func', 3, 4),
1575 ('jump', 'func', 4, 2),
1576 ('branch', 'func', 2, 7)])
1577
1578 self.check_events(func, recorders = JUMP_BRANCH_AND_LINE_RECORDERS, expected = [
1579 ('line', 'get_events', 10),
1580 ('line', 'func', 1),
1581 ('line', 'func', 2),
1582 ('branch', 'func', 2, 2),
1583 ('line', 'func', 3),
1584 ('branch', 'func', 3, 6),
1585 ('line', 'func', 6),
1586 ('jump', 'func', 6, 2),
1587 ('line', 'func', 2),
1588 ('branch', 'func', 2, 2),
1589 ('line', 'func', 3),
1590 ('branch', 'func', 3, 4),
1591 ('line', 'func', 4),
1592 ('jump', 'func', 4, 2),
1593 ('line', 'func', 2),
1594 ('branch', 'func', 2, 7),
1595 ('line', 'func', 7),
1596 ('line', 'get_events', 11)])
1597
1598 self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
1599 ('branch left', 'func', 2, 2),
1600 ('branch right', 'func', 3, 6),
1601 ('branch left', 'func', 2, 2),
1602 ('branch left', 'func', 3, 4),
1603 ('branch right', 'func', 2, 7)])
1604
1605 self.check_events(whilefunc, recorders = BRANCHES_RECORDERS, expected = [
1606 ('branch left', 'whilefunc', 1, 2),
1607 ('branch left', 'whilefunc', 1, 2),
1608 ('branch left', 'whilefunc', 1, 2),
1609 ('branch right', 'whilefunc', 1, 3)])
1610
1611 self.check_events(func, recorders = BRANCH_OFFSET_RECORDERS, expected = [
1612 ('branch left', 'func', 28, 32),
1613 ('branch right', 'func', 44, 58),
1614 ('branch left', 'func', 28, 32),
1615 ('branch left', 'func', 44, 50),
1616 ('branch right', 'func', 28, 70)])
1617
1618 def test_except_star(self):
1619
1620 class Foo:
1621 def meth(self):
1622 pass
1623
1624 def func():
1625 try:
1626 try:
1627 raise KeyError
1628 except* Exception as e:
1629 f = Foo(); f.meth()
1630 except KeyError:
1631 pass
1632
1633
1634 self.check_events(func, recorders = JUMP_BRANCH_AND_LINE_RECORDERS, expected = [
1635 ('line', 'get_events', 10),
1636 ('line', 'func', 1),
1637 ('line', 'func', 2),
1638 ('line', 'func', 3),
1639 ('line', 'func', 4),
1640 ('branch', 'func', 4, 4),
1641 ('line', 'func', 5),
1642 ('line', 'meth', 1),
1643 ('jump', 'func', 5, '[offset=120]'),
1644 ('branch', 'func', '[offset=124]', '[offset=130]'),
1645 ('line', 'get_events', 11)])
1646
1647 self.check_events(func, recorders = FLOW_AND_LINE_RECORDERS, expected = [
1648 ('line', 'get_events', 10),
1649 ('line', 'func', 1),
1650 ('line', 'func', 2),
1651 ('line', 'func', 3),
1652 ('raise', KeyError),
1653 ('line', 'func', 4),
1654 ('branch', 'func', 4, 4),
1655 ('line', 'func', 5),
1656 ('line', 'meth', 1),
1657 ('return', 'meth', None),
1658 ('jump', 'func', 5, '[offset=120]'),
1659 ('branch', 'func', '[offset=124]', '[offset=130]'),
1660 ('return', 'func', None),
1661 ('line', 'get_events', 11)])
1662
1663 def test_while_offset_consistency(self):
1664
1665 def foo(n=0):
1666 while n<4:
1667 pass
1668 n += 1
1669 return None
1670
1671 in_loop = ('branch left', 'foo', 10, 16)
1672 exit_loop = ('branch right', 'foo', 10, 40)
1673 self.check_events(foo, recorders = BRANCH_OFFSET_RECORDERS, expected = [
1674 in_loop,
1675 in_loop,
1676 in_loop,
1677 in_loop,
1678 exit_loop])
1679
1680 def test_async_for(self):
1681
1682 def func():
1683 async def gen():
1684 yield 2
1685 yield 3
1686
1687 async def foo():
1688 async for y in gen():
1689 2
1690 pass # line 3
1691
1692 try:
1693 foo().send(None)
1694 except StopIteration:
1695 pass
1696
1697 self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
1698 ('branch left', 'foo', 1, 1),
1699 ('branch left', 'foo', 1, 1),
1700 ('branch right', 'foo', 1, 3),
1701 ('branch left', 'func', 12, 12)])
1702
1703
1704 def test_match(self):
1705
1706 def func(v=1):
1707 x = 0
1708 for v in range(4):
1709 match v:
1710 case 1:
1711 x += 1
1712 case 2:
1713 x += 2
1714 case _:
1715 x += 3
1716 return x
1717
1718 self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
1719 ('branch left', 'func', 2, 2),
1720 ('branch right', 'func', 4, 6),
1721 ('branch right', 'func', 6, 8),
1722 ('branch left', 'func', 2, 2),
1723 ('branch left', 'func', 4, 5),
1724 ('branch left', 'func', 2, 2),
1725 ('branch right', 'func', 4, 6),
1726 ('branch left', 'func', 6, 7),
1727 ('branch left', 'func', 2, 2),
1728 ('branch right', 'func', 4, 6),
1729 ('branch right', 'func', 6, 8),
1730 ('branch right', 'func', 2, 10)])
1731
1732 def test_callback_set_frame_lineno(self):
1733 def func(s: str) -> int:
1734 if s.startswith("t"):
1735 return 1
1736 else:
1737 return 0
1738
1739 def callback(code, from_, to):
1740 # try set frame.f_lineno
1741 frame = inspect.currentframe()
1742 while frame and frame.f_code is not code:
1743 frame = frame.f_back
1744
1745 self.assertIsNotNone(frame)
1746 frame.f_lineno = frame.f_lineno + 1 # run next instruction
1747
1748 sys.monitoring.set_local_events(TEST_TOOL, func.__code__, E.BRANCH_LEFT)
1749 sys.monitoring.register_callback(TEST_TOOL, E.BRANCH_LEFT, callback)
1750
1751 self.assertEqual(func("true"), 1)
1752
1753
1754 class TestBranchConsistency(MonitoringTestBase, unittest.TestCase):
1755
1756 def check_branches(self, run_func, test_func=None, tool=TEST_TOOL, recorders=BRANCH_OFFSET_RECORDERS):
1757 if test_func is None:
1758 test_func = run_func
1759 try:
1760 self.assertEqual(sys.monitoring._all_events(), {})
1761 event_list = []
1762 all_events = 0
1763 for recorder in recorders:
1764 ev = recorder.event_type
1765 sys.monitoring.register_callback(tool, ev, recorder(event_list))
1766 all_events |= ev
1767 sys.monitoring.set_local_events(tool, test_func.__code__, all_events)
1768 run_func()
1769 sys.monitoring.set_local_events(tool, test_func.__code__, 0)
1770 for recorder in recorders:
1771 sys.monitoring.register_callback(tool, recorder.event_type, None)
1772 lefts = set()
1773 rights = set()
1774 for (src, left, right) in test_func.__code__.co_branches():
1775 lefts.add((src, left))
1776 rights.add((src, right))
1777 for event in event_list:
1778 way, _, src, dest = event
1779 if "left" in way:
1780 self.assertIn((src, dest), lefts)
1781 else:
1782 self.assertIn("right", way)
1783 self.assertIn((src, dest), rights)
1784 finally:
1785 sys.monitoring.set_local_events(tool, test_func.__code__, 0)
1786 for recorder in recorders:
1787 sys.monitoring.register_callback(tool, recorder.event_type, None)
1788
1789 def test_simple(self):
1790
1791 def func():
1792 x = 1
1793 for a in range(2):
1794 if a:
1795 x = 4
1796 else:
1797 x = 6
1798 7
1799
1800 self.check_branches(func)
1801
1802 def whilefunc(n=0):
1803 while n < 3:
1804 n += 1 # line 2
1805 3
1806
1807 self.check_branches(whilefunc)
1808
1809 def test_except_star(self):
1810
1811 class Foo:
1812 def meth(self):
1813 pass
1814
1815 def func():
1816 try:
1817 try:
1818 raise KeyError
1819 except* Exception as e:
1820 f = Foo(); f.meth()
1821 except KeyError:
1822 pass
1823
1824
1825 self.check_branches(func)
1826
1827 def test4(self):
1828
1829 def foo(n=0):
1830 while n<4:
1831 pass
1832 n += 1
1833 return None
1834
1835 self.check_branches(foo)
1836
1837 def test_async_for(self):
1838
1839 async def gen():
1840 yield 2
1841 yield 3
1842
1843 async def foo():
1844 async for y in gen():
1845 2
1846 pass # line 3
1847
1848 def func():
1849 try:
1850 foo().send(None)
1851 except StopIteration:
1852 pass
1853
1854 self.check_branches(func, foo)
1855
1856
1857 class TestLoadSuperAttr(CheckEvents):
1858 RECORDERS = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder
1859
1860 def _exec(self, co):
1861 d = {}
1862 exec(co, d, d)
1863 return d
1864
1865 def _exec_super(self, codestr, optimized=False):
1866 # The compiler checks for statically visible shadowing of the name
1867 # `super`, and declines to emit `LOAD_SUPER_ATTR` if shadowing is found.
1868 # So inserting `super = super` prevents the compiler from emitting
1869 # `LOAD_SUPER_ATTR`, and allows us to test that monitoring events for
1870 # `LOAD_SUPER_ATTR` are equivalent to those we'd get from the
1871 # un-optimized `LOAD_GLOBAL super; CALL; LOAD_ATTR` form.
1872 assignment = "x = 1" if optimized else "super = super"
1873 codestr = f"{assignment}\n{textwrap.dedent(codestr)}"
1874 co = compile(codestr, "<string>", "exec")
1875 # validate that we really do have a LOAD_SUPER_ATTR, only when optimized
1876 self.assertEqual(self._has_load_super_attr(co), optimized)
1877 return self._exec(co)
1878
1879 def _has_load_super_attr(self, co):
1880 has = any(instr.opname == "LOAD_SUPER_ATTR" for instr in dis.get_instructions(co))
1881 if not has:
1882 has = any(
1883 isinstance(c, types.CodeType) and self._has_load_super_attr(c)
1884 for c in co.co_consts
1885 )
1886 return has
1887
1888 def _super_method_call(self, optimized=False):
1889 codestr = """
1890 class A:
1891 def method(self, x):
1892 return x
1893
1894 class B(A):
1895 def method(self, x):
1896 return super(
1897 ).method(
1898 x
1899 )
1900
1901 b = B()
1902 def f():
1903 return b.method(1)
1904 """
1905 d = self._exec_super(codestr, optimized)
1906 expected = [
1907 ('line', 'get_events', 10),
1908 ('call', 'f', sys.monitoring.MISSING),
1909 ('line', 'f', 1),
1910 ('call', 'method', d["b"]),
1911 ('line', 'method', 1),
1912 ('call', 'super', sys.monitoring.MISSING),
1913 ('C return', 'super', sys.monitoring.MISSING),
1914 ('line', 'method', 2),
1915 ('line', 'method', 3),
1916 ('line', 'method', 2),
1917 ('call', 'method', d["b"]),
1918 ('line', 'method', 1),
1919 ('line', 'method', 1),
1920 ('line', 'get_events', 11),
1921 ('call', 'set_events', 2),
1922 ]
1923 return d["f"], expected
1924
1925 def test_method_call(self):
1926 nonopt_func, nonopt_expected = self._super_method_call(optimized=False)
1927 opt_func, opt_expected = self._super_method_call(optimized=True)
1928
1929 self.check_events(nonopt_func, recorders=self.RECORDERS, expected=nonopt_expected)
1930 self.check_events(opt_func, recorders=self.RECORDERS, expected=opt_expected)
1931
1932 def _super_method_call_error(self, optimized=False):
1933 codestr = """
1934 class A:
1935 def method(self, x):
1936 return x
1937
1938 class B(A):
1939 def method(self, x):
1940 return super(
1941 x,
1942 self,
1943 ).method(
1944 x
1945 )
1946
1947 b = B()
1948 def f():
1949 try:
1950 return b.method(1)
1951 except TypeError:
1952 pass
1953 else:
1954 assert False, "should have raised TypeError"
1955 """
1956 d = self._exec_super(codestr, optimized)
1957 expected = [
1958 ('line', 'get_events', 10),
1959 ('call', 'f', sys.monitoring.MISSING),
1960 ('line', 'f', 1),
1961 ('line', 'f', 2),
1962 ('call', 'method', d["b"]),
1963 ('line', 'method', 1),
1964 ('line', 'method', 2),
1965 ('line', 'method', 3),
1966 ('line', 'method', 1),
1967 ('call', 'super', 1),
1968 ('C raise', 'super', 1),
1969 ('line', 'f', 3),
1970 ('line', 'f', 4),
1971 ('line', 'get_events', 11),
1972 ('call', 'set_events', 2),
1973 ]
1974 return d["f"], expected
1975
1976 def test_method_call_error(self):
1977 nonopt_func, nonopt_expected = self._super_method_call_error(optimized=False)
1978 opt_func, opt_expected = self._super_method_call_error(optimized=True)
1979
1980 self.check_events(nonopt_func, recorders=self.RECORDERS, expected=nonopt_expected)
1981 self.check_events(opt_func, recorders=self.RECORDERS, expected=opt_expected)
1982
1983 def _super_attr(self, optimized=False):
1984 codestr = """
1985 class A:
1986 x = 1
1987
1988 class B(A):
1989 def method(self):
1990 return super(
1991 ).x
1992
1993 b = B()
1994 def f():
1995 return b.method()
1996 """
1997 d = self._exec_super(codestr, optimized)
1998 expected = [
1999 ('line', 'get_events', 10),
2000 ('call', 'f', sys.monitoring.MISSING),
2001 ('line', 'f', 1),
2002 ('call', 'method', d["b"]),
2003 ('line', 'method', 1),
2004 ('call', 'super', sys.monitoring.MISSING),
2005 ('C return', 'super', sys.monitoring.MISSING),
2006 ('line', 'method', 2),
2007 ('line', 'method', 1),
2008 ('line', 'get_events', 11),
2009 ('call', 'set_events', 2)
2010 ]
2011 return d["f"], expected
2012
2013 def test_attr(self):
2014 nonopt_func, nonopt_expected = self._super_attr(optimized=False)
2015 opt_func, opt_expected = self._super_attr(optimized=True)
2016
2017 self.check_events(nonopt_func, recorders=self.RECORDERS, expected=nonopt_expected)
2018 self.check_events(opt_func, recorders=self.RECORDERS, expected=opt_expected)
2019
2020 def test_vs_other_type_call(self):
2021 code_template = textwrap.dedent("""
2022 class C:
2023 def method(self):
2024 return {cls}().__repr__{call}
2025 c = C()
2026 def f():
2027 return c.method()
2028 """)
2029
2030 def get_expected(name, call_method, ns):
2031 repr_arg = 0 if name == "int" else sys.monitoring.MISSING
2032 return [
2033 ('line', 'get_events', 10),
2034 ('call', 'f', sys.monitoring.MISSING),
2035 ('line', 'f', 1),
2036 ('call', 'method', ns["c"]),
2037 ('line', 'method', 1),
2038 ('call', name, sys.monitoring.MISSING),
2039 ('C return', name, sys.monitoring.MISSING),
2040 *(
2041 [
2042 ('call', '__repr__', repr_arg),
2043 ('C return', '__repr__', repr_arg),
2044 ] if call_method else []
2045 ),
2046 ('line', 'get_events', 11),
2047 ('call', 'set_events', 2),
2048 ]
2049
2050 for call_method in [True, False]:
2051 with self.subTest(call_method=call_method):
2052 call_str = "()" if call_method else ""
2053 code_super = code_template.format(cls="super", call=call_str)
2054 code_int = code_template.format(cls="int", call=call_str)
2055 co_super = compile(code_super, '<string>', 'exec')
2056 self.assertTrue(self._has_load_super_attr(co_super))
2057 ns_super = self._exec(co_super)
2058 ns_int = self._exec(code_int)
2059
2060 self.check_events(
2061 ns_super["f"],
2062 recorders=self.RECORDERS,
2063 expected=get_expected("super", call_method, ns_super)
2064 )
2065 self.check_events(
2066 ns_int["f"],
2067 recorders=self.RECORDERS,
2068 expected=get_expected("int", call_method, ns_int)
2069 )
2070
2071
2072 class TestSetGetEvents(MonitoringTestBase, unittest.TestCase):
2073
2074 def test_global(self):
2075 sys.monitoring.set_events(TEST_TOOL, E.PY_START)
2076 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), E.PY_START)
2077 sys.monitoring.set_events(TEST_TOOL2, E.PY_START)
2078 self.assertEqual(sys.monitoring.get_events(TEST_TOOL2), E.PY_START)
2079 sys.monitoring.set_events(TEST_TOOL, 0)
2080 self.assertEqual(sys.monitoring.get_events(TEST_TOOL), 0)
2081 sys.monitoring.set_events(TEST_TOOL2,0)
2082 self.assertEqual(sys.monitoring.get_events(TEST_TOOL2), 0)
2083
2084 def test_local(self):
2085 code = f1.__code__
2086 sys.monitoring.set_local_events(TEST_TOOL, code, E.PY_START)
2087 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), E.PY_START)
2088 sys.monitoring.set_local_events(TEST_TOOL, code, 0)
2089 sys.monitoring.set_local_events(TEST_TOOL, code, E.BRANCH)
2090 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), E.BRANCH_LEFT | E.BRANCH_RIGHT)
2091 sys.monitoring.set_local_events(TEST_TOOL, code, 0)
2092 sys.monitoring.set_local_events(TEST_TOOL2, code, E.PY_START)
2093 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL2, code), E.PY_START)
2094 sys.monitoring.set_local_events(TEST_TOOL, code, 0)
2095 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), 0)
2096 sys.monitoring.set_local_events(TEST_TOOL2, code, 0)
2097 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL2, code), 0)
2098
2099 class TestUninitialized(unittest.TestCase, MonitoringTestBase):
2100
2101 @staticmethod
2102 def f():
2103 pass
2104
2105 def test_get_local_events_uninitialized(self):
2106 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, self.f.__code__), 0)
2107
2108 class TestRegressions(MonitoringTestBase, unittest.TestCase):
2109
2110 def test_105162(self):
2111 caught = None
2112
2113 def inner():
2114 nonlocal caught
2115 try:
2116 yield
2117 except Exception:
2118 caught = "inner"
2119 yield
2120
2121 def outer():
2122 nonlocal caught
2123 try:
2124 yield from inner()
2125 except Exception:
2126 caught = "outer"
2127 yield
2128
2129 def run():
2130 gen = outer()
2131 gen.send(None)
2132 gen.throw(Exception)
2133 run()
2134 self.assertEqual(caught, "inner")
2135 caught = None
2136 try:
2137 sys.monitoring.set_events(TEST_TOOL, E.PY_RESUME)
2138 run()
2139 self.assertEqual(caught, "inner")
2140 finally:
2141 sys.monitoring.set_events(TEST_TOOL, 0)
2142
2143 def test_108390(self):
2144
2145 class Foo:
2146 def __init__(self, set_event):
2147 if set_event:
2148 sys.monitoring.set_events(TEST_TOOL, E.PY_RESUME)
2149
2150 def make_foo_optimized_then_set_event():
2151 for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD + 1):
2152 Foo(i == _testinternalcapi.SPECIALIZATION_THRESHOLD)
2153
2154 try:
2155 make_foo_optimized_then_set_event()
2156 finally:
2157 sys.monitoring.set_events(TEST_TOOL, 0)
2158
2159 def test_gh108976(self):
2160 sys.monitoring.use_tool_id(0, "test")
2161 self.addCleanup(sys.monitoring.free_tool_id, 0)
2162 sys.monitoring.set_events(0, 0)
2163 sys.monitoring.register_callback(0, E.LINE, lambda *args: sys.monitoring.set_events(0, 0))
2164 sys.monitoring.register_callback(0, E.INSTRUCTION, lambda *args: 0)
2165 sys.monitoring.set_events(0, E.LINE | E.INSTRUCTION)
2166 sys.monitoring.set_events(0, 0)
2167
2168 def test_call_function_ex(self):
2169 def f(a=1, b=2):
2170 return a + b
2171 args = (1, 2)
2172 empty_args = []
2173
2174 call_data = []
2175 sys.monitoring.use_tool_id(0, "test")
2176 self.addCleanup(sys.monitoring.free_tool_id, 0)
2177 sys.monitoring.set_events(0, 0)
2178 sys.monitoring.register_callback(0, E.CALL, lambda code, offset, callable, arg0: call_data.append((callable, arg0)))
2179 sys.monitoring.set_events(0, E.CALL)
2180 f(*args)
2181 f(*empty_args)
2182 sys.monitoring.set_events(0, 0)
2183 self.assertEqual(call_data[0], (f, 1))
2184 self.assertEqual(call_data[1], (f, sys.monitoring.MISSING))
2185
2186 def test_instruction_explicit_callback(self):
2187 # gh-122247
2188 # Calling the instruction event callback explicitly should not
2189 # crash CPython
2190 def callback(code, instruction_offset):
2191 pass
2192
2193 sys.monitoring.use_tool_id(0, "test")
2194 self.addCleanup(sys.monitoring.free_tool_id, 0)
2195 sys.monitoring.register_callback(0, sys.monitoring.events.INSTRUCTION, callback)
2196 sys.monitoring.set_events(0, sys.monitoring.events.INSTRUCTION)
2197 callback(None, 0) # call the *same* handler while it is registered
2198 sys.monitoring.restart_events()
2199 sys.monitoring.set_events(0, 0)
2200
2201
2202 class TestOptimizer(MonitoringTestBase, unittest.TestCase):
2203
2204 def test_for_loop(self):
2205 def test_func(x):
2206 i = 0
2207 while i < x:
2208 i += 1
2209
2210 code = test_func.__code__
2211 sys.monitoring.set_local_events(TEST_TOOL, code, E.PY_START)
2212 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), E.PY_START)
2213 test_func(1000)
2214 sys.monitoring.set_local_events(TEST_TOOL, code, 0)
2215 self.assertEqual(sys.monitoring.get_local_events(TEST_TOOL, code), 0)
2216
2217 class TestTier2Optimizer(CheckEvents):
2218
2219 def test_monitoring_already_opimized_loop(self):
2220 def test_func(recorder):
2221 set_events = sys.monitoring.set_events
2222 line = E.LINE
2223 i = 0
2224 for i in range(_testinternalcapi.SPECIALIZATION_THRESHOLD + 51):
2225 # Turn on events without branching once i reaches _testinternalcapi.SPECIALIZATION_THRESHOLD.
2226 set_events(TEST_TOOL, line * int(i >= _testinternalcapi.SPECIALIZATION_THRESHOLD))
2227 pass
2228 pass
2229 pass
2230
2231 self.assertEqual(sys.monitoring._all_events(), {})
2232 events = []
2233 recorder = LineRecorder(events)
2234 sys.monitoring.register_callback(TEST_TOOL, E.LINE, recorder)
2235 try:
2236 test_func(recorder)
2237 finally:
2238 sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
2239 sys.monitoring.set_events(TEST_TOOL, 0)
2240 self.assertGreater(len(events), 250)
2241
2242 class TestMonitoringAtShutdown(unittest.TestCase):
2243
2244 def test_monitoring_live_at_shutdown(self):
2245 # gh-115832: An object destructor running during the final GC of
2246 # interpreter shutdown triggered an infinite loop in the
2247 # instrumentation code.
2248 script = test.support.findfile("_test_monitoring_shutdown.py")
2249 script_helper.run_test_script(script)
2250
2251
2252 class TestCApiEventGeneration(MonitoringTestBase, unittest.TestCase):
2253
2254 class Scope:
2255 def __init__(self, *args):
2256 self.args = args
2257
2258 def __enter__(self):
2259 _testcapi.monitoring_enter_scope(*self.args)
2260
2261 def __exit__(self, *args):
2262 _testcapi.monitoring_exit_scope()
2263
2264 def setUp(self):
2265 super(TestCApiEventGeneration, self).setUp()
2266
2267 capi = _testcapi
2268
2269 self.codelike = capi.CodeLike(2)
2270
2271 self.cases = [
2272 # (Event, function, *args)
2273 ( 1, E.PY_START, capi.fire_event_py_start),
2274 ( 1, E.PY_RESUME, capi.fire_event_py_resume),
2275 ( 1, E.PY_YIELD, capi.fire_event_py_yield, 10),
2276 ( 1, E.PY_RETURN, capi.fire_event_py_return, 20),
2277 ( 2, E.CALL, capi.fire_event_call, callable, 40),
2278 ( 1, E.JUMP, capi.fire_event_jump, 60),
2279 ( 1, E.BRANCH_RIGHT, capi.fire_event_branch_right, 70),
2280 ( 1, E.BRANCH_LEFT, capi.fire_event_branch_left, 80),
2281 ( 1, E.PY_THROW, capi.fire_event_py_throw, ValueError(1)),
2282 ( 1, E.RAISE, capi.fire_event_raise, ValueError(2)),
2283 ( 1, E.EXCEPTION_HANDLED, capi.fire_event_exception_handled, ValueError(5)),
2284 ( 1, E.PY_UNWIND, capi.fire_event_py_unwind, ValueError(6)),
2285 ( 1, E.STOP_ITERATION, capi.fire_event_stop_iteration, 7),
2286 ( 1, E.STOP_ITERATION, capi.fire_event_stop_iteration, StopIteration(8)),
2287 ]
2288
2289 self.EXPECT_RAISED_EXCEPTION = [E.PY_THROW, E.RAISE, E.EXCEPTION_HANDLED, E.PY_UNWIND]
2290
2291
2292 def check_event_count(self, event, func, args, expected, callback_raises=None):
2293 class Counter:
2294 def __init__(self, callback_raises):
2295 self.callback_raises = callback_raises
2296 self.count = 0
2297
2298 def __call__(self, *args):
2299 self.count += 1
2300 if self.callback_raises:
2301 exc = self.callback_raises
2302 self.callback_raises = None
2303 raise exc
2304
2305 try:
2306 counter = Counter(callback_raises)
2307 sys.monitoring.register_callback(TEST_TOOL, event, counter)
2308 if event == E.C_RETURN or event == E.C_RAISE:
2309 sys.monitoring.set_events(TEST_TOOL, E.CALL)
2310 else:
2311 sys.monitoring.set_events(TEST_TOOL, event)
2312 event_value = int(math.log2(event))
2313 with self.Scope(self.codelike, event_value):
2314 counter.count = 0
2315 try:
2316 func(*args)
2317 except ValueError as e:
2318 self.assertIsInstance(expected, ValueError)
2319 self.assertEqual(str(e), str(expected))
2320 return
2321 else:
2322 self.assertEqual(counter.count, expected)
2323
2324 prev = sys.monitoring.register_callback(TEST_TOOL, event, None)
2325 with self.Scope(self.codelike, event_value):
2326 counter.count = 0
2327 func(*args)
2328 self.assertEqual(counter.count, 0)
2329 self.assertEqual(prev, counter)
2330 finally:
2331 sys.monitoring.set_events(TEST_TOOL, 0)
2332
2333 def test_fire_event(self):
2334 for expected, event, function, *args in self.cases:
2335 offset = 0
2336 self.codelike = _testcapi.CodeLike(1)
2337 with self.subTest(function.__name__):
2338 args_ = (self.codelike, offset) + tuple(args)
2339 self.check_event_count(event, function, args_, expected)
2340
2341 def test_missing_exception(self):
2342 for _, event, function, *args in self.cases:
2343 if event not in self.EXPECT_RAISED_EXCEPTION:
2344 continue
2345 assert args and isinstance(args[-1], BaseException)
2346 offset = 0
2347 self.codelike = _testcapi.CodeLike(1)
2348 with self.subTest(function.__name__):
2349 args_ = (self.codelike, offset) + tuple(args[:-1]) + (None,)
2350 evt = int(math.log2(event))
2351 expected = ValueError(f"Firing event {evt} with no exception set")
2352 self.check_event_count(event, function, args_, expected)
2353
2354 def test_fire_event_failing_callback(self):
2355 for expected, event, function, *args in self.cases:
2356 offset = 0
2357 self.codelike = _testcapi.CodeLike(1)
2358 with self.subTest(function.__name__):
2359 args_ = (self.codelike, offset) + tuple(args)
2360 exc = OSError(42)
2361 with self.assertRaises(type(exc)):
2362 self.check_event_count(event, function, args_, expected,
2363 callback_raises=exc)
2364
2365
2366 CANNOT_DISABLE = { E.PY_THROW, E.RAISE, E.RERAISE,
2367 E.EXCEPTION_HANDLED, E.PY_UNWIND }
2368
2369 def check_disable(self, event, func, args, expected):
2370 try:
2371 counter = CounterWithDisable()
2372 sys.monitoring.register_callback(TEST_TOOL, event, counter)
2373 if event == E.C_RETURN or event == E.C_RAISE:
2374 sys.monitoring.set_events(TEST_TOOL, E.CALL)
2375 else:
2376 sys.monitoring.set_events(TEST_TOOL, event)
2377 event_value = int(math.log2(event))
2378 with self.Scope(self.codelike, event_value):
2379 counter.count = 0
2380 func(*args)
2381 self.assertEqual(counter.count, expected)
2382 counter.disable = True
2383 if event in self.CANNOT_DISABLE:
2384 # use try-except rather then assertRaises to avoid
2385 # events from framework code
2386 try:
2387 counter.count = 0
2388 func(*args)
2389 self.assertEqual(counter.count, expected)
2390 except ValueError:
2391 pass
2392 else:
2393 self.Error("Expected a ValueError")
2394 else:
2395 counter.count = 0
2396 func(*args)
2397 self.assertEqual(counter.count, expected)
2398 counter.count = 0
2399 func(*args)
2400 self.assertEqual(counter.count, expected - 1)
2401 finally:
2402 sys.monitoring.set_events(TEST_TOOL, 0)
2403
2404 def test_disable_event(self):
2405 for expected, event, function, *args in self.cases:
2406 offset = 0
2407 self.codelike = _testcapi.CodeLike(2)
2408 with self.subTest(function.__name__):
2409 args_ = (self.codelike, 0) + tuple(args)
2410 self.check_disable(event, function, args_, expected)
2411
2412 def test_enter_scope_two_events(self):
2413 try:
2414 yield_counter = CounterWithDisable()
2415 unwind_counter = CounterWithDisable()
2416 sys.monitoring.register_callback(TEST_TOOL, E.PY_YIELD, yield_counter)
2417 sys.monitoring.register_callback(TEST_TOOL, E.PY_UNWIND, unwind_counter)
2418 sys.monitoring.set_events(TEST_TOOL, E.PY_YIELD | E.PY_UNWIND)
2419
2420 yield_value = int(math.log2(E.PY_YIELD))
2421 unwind_value = int(math.log2(E.PY_UNWIND))
2422 cl = _testcapi.CodeLike(2)
2423 common_args = (cl, 0)
2424 with self.Scope(cl, yield_value, unwind_value):
2425 yield_counter.count = 0
2426 unwind_counter.count = 0
2427
2428 _testcapi.fire_event_py_unwind(*common_args, ValueError(42))
2429 assert(yield_counter.count == 0)
2430 assert(unwind_counter.count == 1)
2431
2432 _testcapi.fire_event_py_yield(*common_args, ValueError(42))
2433 assert(yield_counter.count == 1)
2434 assert(unwind_counter.count == 1)
2435
2436 yield_counter.disable = True
2437 _testcapi.fire_event_py_yield(*common_args, ValueError(42))
2438 assert(yield_counter.count == 2)
2439 assert(unwind_counter.count == 1)
2440
2441 _testcapi.fire_event_py_yield(*common_args, ValueError(42))
2442 assert(yield_counter.count == 2)
2443 assert(unwind_counter.count == 1)
2444
2445 finally:
2446 sys.monitoring.set_events(TEST_TOOL, 0)