k00shi · GitHub

Bug description:

sys.addaudithook() suppresses all Exception subclasses when an existing audit hook raises. The documentation and the C API (PySys_AddAuditHook) both say only RuntimeError should be suppressed.

The Python-level sys.addaudithook(hook) at Python/sysmodule.c:530 fires the sys.addaudithook event to existing hooks. If a hook raises, the exception matcher checks against PyExc_Exception instead of PyExc_RuntimeError. Any Exception subclass prevents the new hook from being installed and returns None.

Actual result: A hook that raises ValueError (or any Exception subclass) during sys.addaudithook is silently suppressed, and the new hook is not installed.

Expected result: Only RuntimeError should be suppressed. Non-RuntimeError exceptions should propagate through sys.addaudithook().

The fix is in Python/sysmodule.c:530, changing PyExc_Exception to PyExc_RuntimeError.

import sys
def blocking_hook(*args):
    raise ValueError("blocked")
sys.addaudithook(blocking_hook)
def my_hook(*args):
    print("This hook should have been installed")
sys.addaudithook(my_hook)
sys.audit("test")

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Linux, Windows

Linked PRs

Read the original on github.com ↗