Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 494e3d4

Browse files
authored
GH-107774: Add missing audit event for PEP 669 (GH-107775)
1 parent 39ef93e commit 494e3d4

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Lib/test/audit-tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,17 @@ def test_not_in_gc():
514514
assert hook not in o
515515

516516

517+
def test_sys_monitoring_register_callback():
518+
import sys
519+
520+
def hook(event, args):
521+
if event.startswith("sys.monitoring"):
522+
print(event, args)
523+
524+
sys.addaudithook(hook)
525+
sys.monitoring.register_callback(1, 1, None)
526+
527+
517528
if __name__ == "__main__":
518529
from test.support import suppress_msvcrt_asserts
519530

Lib/test/test_audit.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,18 @@ def test_not_in_gc(self):
257257
self.fail(stderr)
258258

259259

260+
def test_sys_monitoring_register_callback(self):
261+
returncode, events, stderr = self.run_python("test_sys_monitoring_register_callback")
262+
if returncode:
263+
self.fail(stderr)
264+
265+
if support.verbose:
266+
print(*events, sep='\n')
267+
actual = [(ev[0], ev[2]) for ev in events]
268+
expected = [("sys.monitoring.register_callback", "(None,)")]
269+
270+
self.assertEqual(actual, expected)
271+
272+
260273
if __name__ == "__main__":
261274
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PEP 669 specifies that ``sys.monitoring.register_callback`` will generate an
2+
audit event. Pre-releases of Python 3.12 did not generate the audit event.
3+
This is now fixed.

Python/instrumentation.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,9 @@ monitoring_register_callback_impl(PyObject *module, int tool_id, int event,
18511851
PyErr_Format(PyExc_ValueError, "invalid event %d", event);
18521852
return NULL;
18531853
}
1854+
if (PySys_Audit("sys.monitoring.register_callback", "O", func) < 0) {
1855+
return NULL;
1856+
}
18541857
if (func == Py_None) {
18551858
func = NULL;
18561859
}

0 commit comments

Comments
 (0)