Skip to content

Commit 0c37269

Browse files
akuchlingmerwok
andauthored
bpo-41192: Clarify the sys module's description of the auditing feature (GH-22768)
Co-authored-by: Éric Araujo <[email protected]>
1 parent ec42789 commit 0c37269

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
@@ -81,14 +87,23 @@ always available.
8187

8288
.. index:: single: auditing
8389

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

93108
Hooks are added using the :func:`sys.addaudithook` or
94109
:c:func:`PySys_AddAuditHook` functions.

0 commit comments

Comments
 (0)