Skip to content

Commit e3d77ac

Browse files
committed
Add audit events for monitoring tool IDs
1 parent b968417 commit e3d77ac

5 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎Doc/library/sys.monitoring.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ Registering and using tools
5454

5555
Unregister all events and callback functions associated with *tool_id*.
5656

57+
.. audit-event:: sys.monitoring.clear_tool_id tool_id sys.monitoring.clear_tool_id
58+
5759
.. versionadded:: 3.14
5860

5961
.. function:: free_tool_id(tool_id: int, /) -> None
6062

6163
Should be called once a tool no longer requires *tool_id*.
6264
Will call :func:`clear_tool_id` before releasing *tool_id*.
6365

66+
.. audit-event:: sys.monitoring.free_tool_id tool_id sys.monitoring.free_tool_id
67+
6468
.. versionchanged:: 3.14
6569
Now calls :func:`clear_tool_id` before releasing *tool_id*.
6670
Previously, it would not disable global or local events associated

‎Lib/test/audit-tests.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,19 @@ def hook(event, args):
648648
sys.monitoring.register_callback(1, 1, None)
649649

650650

651+
def test_sys_monitoring_clear_free_tool_id():
652+
import sys
653+
654+
def hook(event, args):
655+
if event.startswith("sys.monitoring"):
656+
print(event, args)
657+
658+
sys.addaudithook(hook)
659+
sys.monitoring.use_tool_id(1, "test")
660+
sys.monitoring.clear_tool_id(1)
661+
sys.monitoring.free_tool_id(1)
662+
663+
651664
def test_winapi_createnamedpipe(pipe_name):
652665
import _winapi
653666

‎Lib/test/test_audit.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,21 @@ def test_sys_monitoring_register_callback(self):
304304

305305
self.assertEqual(actual, expected)
306306

307+
def test_sys_monitoring_clear_free_tool_id(self):
308+
returncode, events, stderr = self.run_python("test_sys_monitoring_clear_free_tool_id")
309+
if returncode:
310+
self.fail(stderr)
311+
312+
if support.verbose:
313+
print(*events, sep='\n')
314+
actual = [(ev[0], ev[2]) for ev in events]
315+
expected = [
316+
("sys.monitoring.clear_tool_id", "(1,)"),
317+
("sys.monitoring.free_tool_id", "(1,)"),
318+
]
319+
320+
self.assertEqual(actual, expected)
321+
307322
def test_winapi_createnamedpipe(self):
308323
winapi = import_helper.import_module("_winapi")
309324

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Emit audit events when ``sys.monitoring`` tool IDs are cleared or freed.

‎Python/instrumentation.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,10 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id)
22262226
return NULL;
22272227
}
22282228

2229+
if (PySys_Audit("sys.monitoring.clear_tool_id", "i", tool_id) < 0) {
2230+
return NULL;
2231+
}
2232+
22292233
PyInterpreterState *interp = _PyInterpreterState_GET();
22302234

22312235
if (interp->monitoring_tool_names[tool_id] != NULL) {
@@ -2252,6 +2256,11 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id)
22522256
if (check_valid_tool(tool_id)) {
22532257
return NULL;
22542258
}
2259+
2260+
if (PySys_Audit("sys.monitoring.free_tool_id", "i", tool_id) < 0) {
2261+
return NULL;
2262+
}
2263+
22552264
PyInterpreterState *interp = _PyInterpreterState_GET();
22562265

22572266
if (interp->monitoring_tool_names[tool_id] != NULL) {

0 commit comments

Comments
 (0)