Skip to content

sys.addaudithook() suppresses all Exception subclasses instead of only RuntimeError #152912

Closed

Description

@k00shi

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

Activity

  1. added 2 commits that reference this issue on Jul 2, 2026
  2. added a commit that references this issue on Jul 3, 2026
  3. added 2 commits that reference this issue on Jul 6, 2026
  4. added a commit that references this issue on Jul 27, 2026
  5. added a commit that references this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions