File size: 22,820 Bytes
5a9e79d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | diff --git a/Lib/test/test_pep669_monitoring_aug.py b/Lib/test/test_pep669_monitoring_aug.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_pep669_monitoring_aug.py
@@ -0,0 +1,549 @@
+import sys
+import unittest
+from contextlib import suppress
+
+
+TOOL = 4
+TOOL2 = 3
+TOOL3 = 5
+E = sys.monitoring.events
+
+
+def _clear_tool(tool):
+ with suppress(Exception):
+ sys.monitoring.set_events(tool, 0)
+ for event in (
+ E.PY_START, E.PY_RESUME, E.PY_RETURN, E.PY_YIELD,
+ E.PY_UNWIND, E.PY_THROW, E.CALL, E.C_RAISE, E.C_RETURN,
+ E.RAISE, E.EXCEPTION_HANDLED, E.LINE, E.INSTRUCTION,
+ E.JUMP, E.BRANCH, E.STOP_ITERATION,
+ ):
+ with suppress(Exception):
+ sys.monitoring.register_callback(tool, event, None)
+ with suppress(Exception):
+ sys.monitoring.free_tool_id(tool)
+
+
+class MonitoringAugTest(unittest.TestCase):
+ def setUp(self):
+ _clear_tool(TOOL)
+ _clear_tool(TOOL2)
+ _clear_tool(TOOL3)
+
+ def tearDown(self):
+ _clear_tool(TOOL)
+ _clear_tool(TOOL2)
+ _clear_tool(TOOL3)
+
+ def claim(self, tool=TOOL, name="pep669-aug"):
+ sys.monitoring.use_tool_id(tool, name)
+ self.addCleanup(_clear_tool, tool)
+ return tool
+
+ def test_public_constants_are_power_of_two_and_tool_lifecycle(self):
+ events = [
+ "PY_START", "PY_RESUME", "PY_RETURN", "PY_YIELD",
+ "PY_UNWIND", "PY_THROW", "CALL", "C_RAISE", "C_RETURN",
+ "RAISE", "EXCEPTION_HANDLED", "LINE", "INSTRUCTION",
+ "JUMP", "BRANCH", "STOP_ITERATION",
+ ]
+ values = [getattr(E, name) for name in events]
+ self.assertEqual(len(values), len(set(values)))
+ for value in values:
+ self.assertGreater(value, 0)
+ self.assertEqual(value & (value - 1), 0)
+
+ self.assertEqual(sys.monitoring.DEBUGGER_ID, 0)
+ self.assertEqual(sys.monitoring.COVERAGE_ID, 1)
+ self.assertEqual(sys.monitoring.PROFILER_ID, 2)
+ self.assertEqual(sys.monitoring.OPTIMIZER_ID, 5)
+ self.assertIs(sys.monitoring.DISABLE, sys.monitoring.DISABLE)
+ self.assertIs(sys.monitoring.MISSING, sys.monitoring.MISSING)
+ # DISABLE and MISSING are distinct documented sentinels — neither may
+ # alias the other.
+ self.assertIsNot(sys.monitoring.DISABLE, sys.monitoring.MISSING)
+
+ self.assertIsNone(sys.monitoring.get_tool(TOOL))
+ sys.monitoring.use_tool_id(TOOL, "first")
+ self.assertEqual(sys.monitoring.get_tool(TOOL), "first")
+ with self.assertRaises(ValueError):
+ sys.monitoring.use_tool_id(TOOL, "second")
+ with self.assertRaises(ValueError):
+ sys.monitoring.use_tool_id(6, "outside-range")
+ with self.assertRaises((TypeError, ValueError)):
+ sys.monitoring.use_tool_id(TOOL2, object())
+ sys.monitoring.free_tool_id(TOOL)
+ self.assertIsNone(sys.monitoring.get_tool(TOOL))
+
+ def test_register_callback_replaces_previous_and_rejects_event_sets(self):
+ self.claim()
+
+ def callback_one(*args):
+ return None
+
+ def callback_two(*args):
+ return None
+
+ self.assertIsNone(
+ sys.monitoring.register_callback(TOOL, E.PY_START, callback_one)
+ )
+ self.assertIs(
+ sys.monitoring.register_callback(TOOL, E.PY_START, callback_two),
+ callback_one,
+ )
+ with self.assertRaises(ValueError):
+ sys.monitoring.register_callback(
+ TOOL, E.PY_START | E.PY_RETURN, callback_one
+ )
+ self.assertIs(
+ sys.monitoring.register_callback(TOOL, E.PY_START, None),
+ callback_two,
+ )
+
+ def test_set_events_requires_claimed_tool_and_call_group(self):
+ with self.assertRaises(ValueError):
+ sys.monitoring.set_events(TOOL, E.PY_START)
+
+ self.claim()
+ with self.assertRaises(ValueError):
+ sys.monitoring.set_events(TOOL, E.C_RETURN)
+ with self.assertRaises(ValueError):
+ sys.monitoring.set_events(TOOL, E.C_RAISE)
+
+ seen = []
+
+ def call_cb(code, offset, func, arg):
+ seen.append(("call", getattr(func, "__name__", type(func).__name__), arg))
+
+ def return_cb(code, offset, func, arg):
+ seen.append(("return", getattr(func, "__name__", type(func).__name__), arg))
+
+ def raise_cb(code, offset, func, arg):
+ seen.append(("raise", getattr(func, "__name__", type(func).__name__), arg))
+
+ sys.monitoring.register_callback(TOOL, E.CALL, call_cb)
+ sys.monitoring.register_callback(TOOL, E.C_RETURN, return_cb)
+ sys.monitoring.register_callback(TOOL, E.C_RAISE, raise_cb)
+ mask = E.CALL | E.C_RETURN | E.C_RAISE
+ sys.monitoring.set_events(TOOL, mask)
+ self.assertEqual(sys.monitoring.get_events(TOOL), E.CALL)
+
+ len([])
+ with suppress(ValueError):
+ int("not-an-int")
+ sys.monitoring.set_events(TOOL, 0)
+
+ names = [item[1] for item in seen]
+ self.assertIn("len", names)
+ self.assertIn("int", names)
+ self.assertTrue(any(kind == "return" and name == "len"
+ for kind, name, arg in seen))
+ self.assertTrue(any(kind == "raise" and name == "int"
+ for kind, name, arg in seen))
+
+ def test_local_line_events_are_scoped_to_one_code_object(self):
+ self.claim()
+ seen = []
+
+ def target():
+ value = 1
+ value += 2
+ return value
+
+ def other():
+ value = 5
+ return value
+
+ def line_cb(code, line):
+ seen.append((code.co_name, line - code.co_firstlineno))
+
+ sys.monitoring.register_callback(TOOL, E.LINE, line_cb)
+ self.assertEqual(sys.monitoring.get_events(TOOL), 0)
+ sys.monitoring.set_local_events(TOOL, target.__code__, E.LINE)
+ self.assertEqual(
+ sys.monitoring.get_local_events(TOOL, target.__code__), E.LINE
+ )
+
+ self.assertEqual(target(), 3)
+ self.assertEqual(other(), 5)
+ sys.monitoring.set_local_events(TOOL, target.__code__, 0)
+ before = list(seen)
+ target()
+ self.assertEqual(seen, before)
+ self.assertGreaterEqual(len([name for name, line in seen
+ if name == "target"]), 3)
+ self.assertFalse(any(name == "other" for name, line in seen))
+
+ def test_instruction_and_branch_events_report_offsets(self):
+ self.claim()
+ instructions = []
+ branches = []
+
+ def target(flag):
+ if flag:
+ return "yes"
+ return "no"
+
+ def instruction_cb(code, offset):
+ if code is target.__code__:
+ instructions.append(offset)
+
+ def branch_cb(code, offset, destination):
+ if code is target.__code__:
+ branches.append((offset, destination))
+
+ sys.monitoring.register_callback(TOOL, E.INSTRUCTION, instruction_cb)
+ sys.monitoring.register_callback(TOOL, E.BRANCH, branch_cb)
+ sys.monitoring.set_local_events(
+ TOOL, target.__code__, E.INSTRUCTION | E.BRANCH
+ )
+ self.assertEqual(target(True), "yes")
+ self.assertEqual(target(False), "no")
+ sys.monitoring.set_local_events(TOOL, target.__code__, 0)
+
+ self.assertGreater(len(instructions), 4)
+ self.assertGreaterEqual(len(branches), 2)
+ self.assertTrue(all(isinstance(offset, int) and isinstance(dest, int)
+ for offset, dest in branches))
+
+ def test_exception_unwind_and_handled_events_are_distinct(self):
+ self.claim()
+ seen = []
+
+ def raise_cb(code, offset, exc):
+ seen.append(("raise", code.co_name, type(exc)))
+
+ def unwind_cb(code, offset):
+ seen.append(("unwind", code.co_name))
+
+ def handled_cb(code, offset, exc):
+ seen.append(("handled", code.co_name, type(exc)))
+
+ def leaf():
+ raise KeyError("pep669")
+
+ def target():
+ try:
+ leaf()
+ except KeyError:
+ return "handled"
+
+ sys.monitoring.register_callback(TOOL, E.RAISE, raise_cb)
+ sys.monitoring.register_callback(TOOL, E.PY_UNWIND, unwind_cb)
+ sys.monitoring.register_callback(TOOL, E.EXCEPTION_HANDLED, handled_cb)
+ sys.monitoring.set_events(
+ TOOL, E.RAISE | E.PY_UNWIND | E.EXCEPTION_HANDLED
+ )
+ self.assertEqual(target(), "handled")
+ sys.monitoring.set_events(TOOL, 0)
+
+ self.assertIn(("raise", "leaf", KeyError), seen)
+ self.assertIn(("unwind", "leaf"), seen)
+ self.assertTrue(any(item[0] == "handled" and item[2] is KeyError
+ for item in seen if len(item) == 3))
+
+ def test_disable_return_value_and_restart_events(self):
+ self.claim()
+ seen = []
+
+ def target():
+ first = 1
+ second = 2
+ return first + second
+
+ def line_once(code, line):
+ if code is target.__code__:
+ seen.append(line)
+ return sys.monitoring.DISABLE
+
+ sys.monitoring.register_callback(TOOL, E.LINE, line_once)
+ sys.monitoring.set_local_events(TOOL, target.__code__, E.LINE)
+ target()
+ first_count = len(seen)
+ target()
+ self.assertEqual(len(seen), first_count)
+ sys.monitoring.restart_events()
+ target()
+ self.assertGreater(len(seen), first_count)
+ sys.monitoring.set_local_events(TOOL, target.__code__, 0)
+
+ def test_stop_iteration_event_is_separate_from_raise(self):
+ self.claim()
+ seen = []
+
+ def stop_cb(code, offset, exc):
+ seen.append(("stop", code.co_name, type(exc)))
+
+ def raise_cb(code, offset, exc):
+ seen.append(("raise", code.co_name, type(exc)))
+
+ def gen():
+ yield 1
+ return 2
+
+ def consume():
+ for _ in gen():
+ pass
+
+ sys.monitoring.register_callback(TOOL, E.STOP_ITERATION, stop_cb)
+ sys.monitoring.register_callback(TOOL, E.RAISE, raise_cb)
+ sys.monitoring.set_events(TOOL, E.STOP_ITERATION | E.RAISE)
+ consume()
+ sys.monitoring.set_events(TOOL, 0)
+
+ self.assertIn(("stop", "consume", StopIteration), seen)
+ self.assertNotIn(("raise", "consume", StopIteration), seen)
+
+ def test_multiple_tools_receive_same_event_and_disable_is_per_tool(self):
+ self.claim(TOOL, "pep669-aug-one")
+ self.claim(TOOL2, "pep669-aug-two")
+ counts = {"first": 0, "second": 0}
+
+ def target():
+ return "target"
+
+ def first(code, *args):
+ if code is target.__code__:
+ counts["first"] += 1
+ return sys.monitoring.DISABLE
+
+ def second(code, *args):
+ if code is target.__code__:
+ counts["second"] += 1
+
+ sys.monitoring.register_callback(TOOL, E.PY_START, first)
+ sys.monitoring.register_callback(TOOL2, E.PY_START, second)
+ sys.monitoring.set_events(TOOL, E.PY_START)
+ sys.monitoring.set_events(TOOL2, E.PY_START)
+ target()
+ target()
+ self.assertEqual(counts["first"], 1)
+ self.assertGreaterEqual(counts["second"], 2)
+
+ sys.monitoring.restart_events()
+ target()
+ self.assertEqual(counts["first"], 2)
+
+ def test_global_line_events_cover_loop_branch_and_exception_paths(self):
+ self.claim()
+ seen = []
+
+ def loop_branch(limit):
+ total = 0
+ for item in range(limit):
+ if item % 2:
+ total += item
+ else:
+ total -= item
+ return total
+
+ def exception_path():
+ try:
+ raise LookupError("tracked")
+ except LookupError:
+ return "handled"
+
+ def line_cb(code, line):
+ if code.co_name in {"loop_branch", "exception_path"}:
+ seen.append((code.co_name, line - code.co_firstlineno))
+
+ sys.monitoring.register_callback(TOOL, E.LINE, line_cb)
+ sys.monitoring.set_events(TOOL, E.LINE)
+ self.assertEqual(loop_branch(4), 2)
+ self.assertEqual(exception_path(), "handled")
+ sys.monitoring.set_events(TOOL, 0)
+
+ names = {name for name, _line in seen}
+ self.assertEqual(names, {"loop_branch", "exception_path"})
+ self.assertGreaterEqual(len([item for item in seen if item[0] == "loop_branch"]), 6)
+
+ def test_global_call_group_reports_c_return_and_c_raise(self):
+ self.claim()
+ seen = []
+
+ def call_cb(code, offset, func, arg):
+ seen.append(("call", getattr(func, "__name__", type(func).__name__)))
+
+ def return_cb(code, offset, func, arg):
+ seen.append(("return", getattr(func, "__name__", type(func).__name__)))
+
+ def c_raise_cb(code, offset, func, arg):
+ seen.append(("c_raise", getattr(func, "__name__", type(func).__name__)))
+
+ def workload():
+ data = []
+ data.append(1)
+ try:
+ int("not-an-int")
+ except ValueError:
+ pass
+ return len(data)
+
+ sys.monitoring.register_callback(TOOL, E.CALL, call_cb)
+ sys.monitoring.register_callback(TOOL, E.C_RETURN, return_cb)
+ sys.monitoring.register_callback(TOOL, E.C_RAISE, c_raise_cb)
+ sys.monitoring.set_events(TOOL, E.CALL)
+ self.assertEqual(workload(), 1)
+ sys.monitoring.set_events(TOOL, 0)
+
+ self.assertIn(("return", "append"), seen)
+ self.assertIn(("c_raise", "int"), seen)
+
+ def test_local_many_events_are_limited_to_target_code_object(self):
+ self.claim()
+ seen = []
+
+ def target(flag):
+ data = []
+ data.append(1)
+ try:
+ if flag:
+ raise KeyError("flag")
+ except KeyError:
+ data.append(2)
+ return len(data)
+
+ def other():
+ return target(True)
+
+ def record(name):
+ def callback(code, *args):
+ if code.co_name in {"target", "other"}:
+ seen.append((name, code.co_name))
+ return callback
+
+ for event, name in (
+ (E.LINE, "line"),
+ (E.INSTRUCTION, "instruction"),
+ (E.CALL, "call"),
+ (E.C_RETURN, "c_return"),
+ (E.RAISE, "raise"),
+ (E.EXCEPTION_HANDLED, "handled"),
+ ):
+ sys.monitoring.register_callback(TOOL, event, record(name))
+
+ mask = E.LINE | E.INSTRUCTION | E.CALL | E.RAISE | E.EXCEPTION_HANDLED
+ sys.monitoring.set_local_events(TOOL, target.__code__, mask)
+ self.assertEqual(other(), 2)
+ sys.monitoring.set_local_events(TOOL, target.__code__, 0)
+
+ self.assertIn(("line", "target"), seen)
+ self.assertIn(("instruction", "target"), seen)
+ self.assertIn(("call", "target"), seen)
+ self.assertIn(("c_return", "target"), seen)
+ self.assertIn(("raise", "target"), seen)
+ self.assertIn(("handled", "target"), seen)
+ self.assertNotIn(("line", "other"), seen)
+
+ def test_generator_resume_yield_throw_and_unwind_events(self):
+ self.claim()
+ seen = []
+
+ def managed_gen():
+ try:
+ yield "first"
+ yield "second"
+ except KeyError:
+ raise
+
+ def callback(name):
+ def record(code, *args):
+ if code.co_name == "managed_gen":
+ seen.append(name)
+ return record
+
+ for event, name in (
+ (E.PY_START, "start"),
+ (E.PY_RESUME, "resume"),
+ (E.PY_YIELD, "yield"),
+ (E.PY_THROW, "throw"),
+ (E.PY_UNWIND, "unwind"),
+ ):
+ sys.monitoring.register_callback(TOOL, event, callback(name))
+
+ mask = E.PY_START | E.PY_RESUME | E.PY_YIELD | E.PY_THROW | E.PY_UNWIND
+ sys.monitoring.set_local_events(TOOL, managed_gen.__code__, mask)
+ iterator = managed_gen()
+ self.assertEqual(next(iterator), "first")
+ with self.assertRaises(KeyError):
+ iterator.throw(KeyError)
+ sys.monitoring.set_local_events(TOOL, managed_gen.__code__, 0)
+
+ self.assertIn("start", seen)
+ self.assertIn("yield", seen)
+ self.assertIn("throw", seen)
+ self.assertIn("unwind", seen)
+
+ def test_overlapping_local_line_events_for_two_tools(self):
+ self.claim(TOOL, "pep669-aug-line-one")
+ self.claim(TOOL2, "pep669-aug-line-two")
+ counts = {"first": 0, "second": 0}
+
+ def target():
+ total = 0
+ for item in (1, 2, 3):
+ total += item
+ return total
+
+ def first(code, line):
+ if code.co_name == "target":
+ counts["first"] += 1
+
+ def second(code, line):
+ if code.co_name == "target":
+ counts["second"] += 1
+
+ sys.monitoring.register_callback(TOOL, E.LINE, first)
+ sys.monitoring.register_callback(TOOL2, E.LINE, second)
+ sys.monitoring.set_local_events(TOOL, target.__code__, E.LINE)
+ sys.monitoring.set_local_events(TOOL2, target.__code__, E.LINE)
+ self.assertEqual(target(), 6)
+ sys.monitoring.set_local_events(TOOL, target.__code__, 0)
+ sys.monitoring.set_local_events(TOOL2, target.__code__, 0)
+
+ self.assertGreater(counts["first"], 2)
+ self.assertEqual(counts["first"], counts["second"])
+
+ def test_set_local_events_forces_reinstrumentation_when_up_to_date(self):
+ # Adding a local event to a code object that is ALREADY instrumented
+ # at the current monitoring version must still take effect: set_local_events
+ # has to force a re-instrumentation pass. The first call instruments the
+ # code (bringing its version up to date); the second adds INSTRUCTION
+ # while it is up to date, which must fire on the next call.
+ self.claim()
+ lines = []
+ insns = []
+
+ def target():
+ a = 1
+ b = a + 1
+ return b
+
+ sys.monitoring.register_callback(
+ TOOL, E.LINE,
+ lambda code, line: lines.append(line) if code is target.__code__ else None)
+ sys.monitoring.register_callback(
+ TOOL, E.INSTRUCTION,
+ lambda code, off: insns.append(off) if code is target.__code__ else None)
+
+ sys.monitoring.set_local_events(TOOL, target.__code__, E.LINE)
+ target()
+ self.assertTrue(lines)
+ self.assertFalse(insns)
+
+ lines.clear()
+ insns.clear()
+ sys.monitoring.set_local_events(
+ TOOL, target.__code__, E.LINE | E.INSTRUCTION)
+ target()
+ # Clear the local events while the tool is still claimed (the class
+ # tearDown frees the tool, so deferring this to addCleanup would race).
+ sys.monitoring.set_local_events(TOOL, target.__code__, 0)
+ self.assertTrue(
+ insns,
+ "added local INSTRUCTION events did not fire — set_local_events "
+ "failed to force re-instrumentation of an up-to-date code object")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/Lib/test/test_pep669_tracing_stability_aug.py b/Lib/test/test_pep669_tracing_stability_aug.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_pep669_tracing_stability_aug.py
@@ -0,0 +1,86 @@
+import sys
+import unittest
+
+
+class TracingStabilityAugTest(unittest.TestCase):
+ def test_settrace_line_events_for_branching_function(self):
+ seen = []
+
+ def tracer(frame, event, arg):
+ if event == "line" and frame.f_code.co_name == "target":
+ seen.append(frame.f_lineno - frame.f_code.co_firstlineno)
+ return tracer
+
+ def target(flag):
+ total = 0
+ if flag:
+ total += 1
+ else:
+ total += 2
+ return total
+
+ old_trace = sys.gettrace()
+ try:
+ sys.settrace(tracer)
+ self.assertEqual(target(True), 1)
+ self.assertEqual(target(False), 2)
+ finally:
+ sys.settrace(old_trace)
+
+ self.assertIn(1, seen)
+ self.assertIn(2, seen)
+ self.assertIn(6, seen)
+ self.assertTrue({3, 5}.issubset(seen))
+
+ def test_setprofile_call_return_exception_shape(self):
+ seen = []
+
+ def profiler(frame, event, arg):
+ if frame.f_code.co_name in {"target", "leaf"}:
+ seen.append((event, frame.f_code.co_name, type(arg).__name__))
+ return profiler
+
+ def leaf():
+ raise RuntimeError("profile")
+
+ def target():
+ try:
+ leaf()
+ except RuntimeError:
+ return "ok"
+
+ old_profile = sys.getprofile()
+ try:
+ sys.setprofile(profiler)
+ self.assertEqual(target(), "ok")
+ finally:
+ sys.setprofile(old_profile)
+
+ self.assertIn(("call", "target", "NoneType"), seen)
+ self.assertIn(("call", "leaf", "NoneType"), seen)
+ self.assertIn(("return", "target", "str"), seen)
+
+ def test_call_tracing_preserves_existing_reentrant_trace_semantics(self):
+ calls = []
+
+ def traced(value):
+ calls.append(value)
+ return value * 2
+
+ self.assertEqual(sys.call_tracing(traced, (21,)), 42)
+ self.assertEqual(calls, [21])
+
+ def test_frame_trace_opcodes_roundtrip_existing_attribute(self):
+ frame = sys._getframe()
+ old_value = frame.f_trace_opcodes
+ try:
+ frame.f_trace_opcodes = True
+ self.assertIs(frame.f_trace_opcodes, True)
+ frame.f_trace_opcodes = False
+ self.assertIs(frame.f_trace_opcodes, False)
+ finally:
+ frame.f_trace_opcodes = old_value
+
+
+if __name__ == "__main__":
+ unittest.main()
|