Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ def test_block_add_hook_baseexception():
pass


def test_block_add_hook_valueerror():
# Non-RuntimeError exceptions (like ValueError) should propagate out
with assertRaises(ValueError):
with TestHook(
raise_on_events="sys.addaudithook", exc_type=ValueError
) as hook1:
with TestHook() as hook2:
pass


def test_marshal():
import marshal
o = ("a", "b", "c", 1, 2, 3)
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def test_block_add_hook(self):
def test_block_add_hook_baseexception(self):
self.do_test("test_block_add_hook_baseexception")

def test_block_add_hook_valueerror(self):
self.do_test("test_block_add_hook_valueerror")

def test_marshal(self):
import_helper.import_module("marshal")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``sys.addaudithook()`` now correctly suppresses only :exc:`RuntimeError` instead of all :exc:`Exception` subclasses when an existing audit hook raises during hook registration. Patch by Yeongu Kim.
4 changes: 2 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)

/* Invoke existing audit hooks to allow them an opportunity to abort. */
if (_PySys_Audit(tstate, "sys.addaudithook", NULL) < 0) {
if (_PyErr_ExceptionMatches(tstate, PyExc_Exception)) {
/* We do not report errors derived from Exception */
if (_PyErr_ExceptionMatches(tstate, PyExc_RuntimeError)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been like this since the initial implementation in 2019, and isn't specified in PEP 578. While the sys.addaudithook documentation specifies RuntimeError, the equivalent C-API (PySys_AddAuditHook) correctly specifies Exception. I think it we should update the documentation here.

/* We do not report errors derived from RuntimeError */
_PyErr_Clear(tstate);
Py_RETURN_NONE;
}
Expand Down
Loading