Download cpython_7/tests/private/eval_tests_aug.patch from lolbench26/LoLBench: direct link, hf CLI and curl.
- Browser
- Download file 22.8 kB
-
https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/cpython_7/tests/private/eval_tests_aug.patch
- Command line
-
hf download hf://datasets/lolbench26/LoLBench/cpython_7/tests/private/eval_tests_aug.patch
-
curl -L -o eval_tests_aug.patch https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/cpython_7/tests/private/eval_tests_aug.patch
22.8 kB
| 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 | |
| +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 | |
| +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() | |