Skip to content

Commit 92bdc70

Browse files
committed
gh-152912: Fix audit hook exception check in sys.addaudithook()
sys.addaudithook() checks the exception from existing hooks against PyExc_Exception instead of PyExc_RuntimeError. Any Exception subclass is silently swallowed, blocking new hook installations without propagating the error. The C API and documentation specify RuntimeError only.
1 parent 31864bd commit 92bdc70

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/audit-tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ def test_block_add_hook_baseexception():
109109
pass
110110

111111

112+
def test_block_add_hook_valueerror():
113+
# Non-RuntimeError exceptions (like ValueError) should propagate out
114+
with assertRaises(ValueError):
115+
with TestHook(
116+
raise_on_events="sys.addaudithook", exc_type=ValueError
117+
) as hook1:
118+
with TestHook() as hook2:
119+
pass
120+
121+
112122
def test_marshal():
113123
import marshal
114124
o = ("a", "b", "c", 1, 2, 3)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
527527

528528
/* Invoke existing audit hooks to allow them an opportunity to abort. */
529529
if (_PySys_Audit(tstate, "sys.addaudithook", NULL) < 0) {
530-
if (_PyErr_ExceptionMatches(tstate, PyExc_Exception)) {
530+
if (_PyErr_ExceptionMatches(tstate, PyExc_RuntimeError)) {
531531
/* We do not report errors derived from Exception */
532532
_PyErr_Clear(tstate);
533533
Py_RETURN_NONE;

0 commit comments

Comments
 (0)