Skip to content

Commit b948d13

Browse files
akuchlingmerwok
andauthored
[3.9] bpo-41192: Clarify the sys module's description of the auditing feature (GH-22821)
Co-authored-by: Éric Araujo <[email protected]> (cherry picked from commit 0c37269)
1 parent 4aed781 commit b948d13

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Doc/library/sys.rst

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,22 @@ always available.
3131
When an auditing event is raised through the :func:`sys.audit` function, each
3232
hook will be called in the order it was added with the event name and the
3333
tuple of arguments. Native hooks added by :c:func:`PySys_AddAuditHook` are
34-
called first, followed by hooks added in the current interpreter.
34+
called first, followed by hooks added in the current interpreter. Hooks
35+
can then log the event, raise an exception to abort the operation,
36+
or terminate the process entirely.
3537

3638
.. audit-event:: sys.addaudithook "" sys.addaudithook
3739

38-
Raise an auditing event ``sys.addaudithook`` with no arguments. If any
40+
Calling :func:`sys.addaudithook` will itself raise an auditing event
41+
named ``sys.addaudithook`` with no arguments. If any
3942
existing hooks raise an exception derived from :class:`RuntimeError`, the
4043
new hook will not be added and the exception suppressed. As a result,
4144
callers cannot assume that their hook has been added unless they control
4245
all existing hooks.
4346

47+
See the :ref:`audit events table <audit-events>` for all events raised by
48+
CPython, and :pep:`578` for the original design discussion.
49+
4450
.. versionadded:: 3.8
4551

4652
.. versionchanged:: 3.8.1
@@ -79,14 +85,23 @@ always available.
7985

8086
.. index:: single: auditing
8187

82-
Raise an auditing event with any active hooks. The event name is a string
83-
identifying the event and its associated schema, which is the number and
84-
types of arguments. The schema for a given event is considered public and
85-
stable API and should not be modified between releases.
86-
87-
This function will raise the first exception raised by any hook. In general,
88-
these errors should not be handled and should terminate the process as
89-
quickly as possible.
88+
Raise an auditing event and trigger any active auditing hooks.
89+
*event* is a string identifying the event, and *args* may contain
90+
optional arguments with more information about the event. The
91+
number and types of arguments for a given event are considered a
92+
public and stable API and should not be modified between releases.
93+
94+
For example, one auditing event is named ``os.chdir``. This event has
95+
one argument called *path* that will contain the requested new
96+
working directory.
97+
98+
:func:`sys.audit` will call the existing auditing hooks, passing
99+
the event name and arguments, and will re-raise the first exception
100+
from any hook. In general, if an exception is raised, it should not
101+
be handled and the process should be terminated as quickly as
102+
possible. This allows hook implementations to decide how to respond
103+
to particular events: they can merely log the event or abort the
104+
operation by raising an exception.
90105

91106
Hooks are added using the :func:`sys.addaudithook` or
92107
:c:func:`PySys_AddAuditHook` functions.

0 commit comments

Comments
 (0)