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