diff --git a/Doc/library/sys.monitoring.rst b/Doc/library/sys.monitoring.rst index 0a653506b3d4959..1b53f6bd936e322 100644 --- a/Doc/library/sys.monitoring.rst +++ b/Doc/library/sys.monitoring.rst @@ -54,6 +54,8 @@ Registering and using tools Unregister all events and callback functions associated with *tool_id*. + .. audit-event:: sys.monitoring.clear_tool_id tool_id sys.monitoring.clear_tool_id + .. versionadded:: 3.14 .. function:: free_tool_id(tool_id: int, /) -> None @@ -61,6 +63,8 @@ Registering and using tools Should be called once a tool no longer requires *tool_id*. Will call :func:`clear_tool_id` before releasing *tool_id*. + .. audit-event:: sys.monitoring.free_tool_id tool_id sys.monitoring.free_tool_id + .. versionchanged:: 3.14 Now calls :func:`clear_tool_id` before releasing *tool_id*. Previously, it would not disable global or local events associated diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 53bd369fabe21f7..1ae710d0ba019c7 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -648,6 +648,19 @@ def hook(event, args): sys.monitoring.register_callback(1, 1, None) +def test_sys_monitoring_clear_free_tool_id(): + import sys + + def hook(event, args): + if event.startswith("sys.monitoring"): + print(event, args) + + sys.addaudithook(hook) + sys.monitoring.use_tool_id(1, "test") + sys.monitoring.clear_tool_id(1) + sys.monitoring.free_tool_id(1) + + def test_winapi_createnamedpipe(pipe_name): import _winapi diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index e9e546fdbfd6fbe..1056c000f64f6f5 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -304,6 +304,21 @@ def test_sys_monitoring_register_callback(self): self.assertEqual(actual, expected) + def test_sys_monitoring_clear_free_tool_id(self): + returncode, events, stderr = self.run_python("test_sys_monitoring_clear_free_tool_id") + if returncode: + self.fail(stderr) + + if support.verbose: + print(*events, sep='\n') + actual = [(ev[0], ev[2]) for ev in events] + expected = [ + ("sys.monitoring.clear_tool_id", "(1,)"), + ("sys.monitoring.free_tool_id", "(1,)"), + ] + + self.assertEqual(actual, expected) + def test_winapi_createnamedpipe(self): winapi = import_helper.import_module("_winapi") diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-15-00-30-00.gh-issue-159379.a3b7c2.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-15-00-30-00.gh-issue-159379.a3b7c2.rst new file mode 100644 index 000000000000000..664cde01a736d7f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-15-00-30-00.gh-issue-159379.a3b7c2.rst @@ -0,0 +1 @@ +Emit audit events when ``sys.monitoring`` tool IDs are cleared or freed. diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 3c9b2afcde100f3..b5551a068df74d0 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2226,6 +2226,10 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id) return NULL; } + if (PySys_Audit("sys.monitoring.clear_tool_id", "i", tool_id) < 0) { + return NULL; + } + PyInterpreterState *interp = _PyInterpreterState_GET(); if (interp->monitoring_tool_names[tool_id] != NULL) { @@ -2252,6 +2256,11 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id) if (check_valid_tool(tool_id)) { return NULL; } + + if (PySys_Audit("sys.monitoring.free_tool_id", "i", tool_id) < 0) { + return NULL; + } + PyInterpreterState *interp = _PyInterpreterState_GET(); if (interp->monitoring_tool_names[tool_id] != NULL) {