Skip to content

Commit bb2f3ff

Browse files
authored
bpo-42800: Add audit events for f_code and tb_frame (pythonGH-24182)
Accessing the following attributes will now fire PEP 578 style audit hooks as (object.__getattr__, obj, name): * PyTracebackObject: tb_frame * PyFrameObject: f_code * PyGenObject: gi_code, gi_frame * PyCoroObject: cr_code, cr_frame * PyAsyncGenObject: ag_code, ag_frame
1 parent 1536342 commit bb2f3ff

File tree

8 files changed

+22
-9
lines changed

8 files changed

+22
-9
lines changed

Doc/library/audit_events.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Audit events table
77

88
This table contains all events raised by :func:`sys.audit` or
99
:c:func:`PySys_Audit` calls throughout the CPython runtime and the
10-
standard library. These calls were added in 3.8.0 or later.
10+
standard library. These calls were added in 3.8.0 or later (see :pep:`578`).
1111

1212
See :func:`sys.addaudithook` and :c:func:`PySys_AddAuditHook` for
1313
information on handling these events.

Doc/library/stdtypes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5044,6 +5044,9 @@ environment. Code objects are returned by the built-in :func:`compile` function
50445044
and can be extracted from function objects through their :attr:`__code__`
50455045
attribute. See also the :mod:`code` module.
50465046

5047+
Accessing ``__code__`` raises an :ref:`auditing event <auditing>`
5048+
``object.__getattr__`` with arguments ``obj`` and ``"__code__"``.
5049+
50475050
.. index::
50485051
builtin: exec
50495052
builtin: eval

Doc/reference/datamodel.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,9 @@ Internal types
10051005
:attr:`f_lasti` gives the precise instruction (this is an index into the
10061006
bytecode string of the code object).
10071007

1008+
Accessing ``f_code`` raises an :ref:`auditing event <auditing>`
1009+
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
1010+
10081011
.. index::
10091012
single: f_trace (frame attribute)
10101013
single: f_trace_lines (frame attribute)
@@ -1089,6 +1092,9 @@ Internal types
10891092
:keyword:`try` statement with no matching except clause or with a
10901093
finally clause.
10911094

1095+
Accessing ``tb_frame`` raises an :ref:`auditing event <auditing>`
1096+
``object.__getattr__`` with arguments ``obj`` and ``"tb_frame"``.
1097+
10921098
.. index::
10931099
single: tb_next (traceback attribute)
10941100

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ Kevan Heydon
716716
Wouter van Heyst
717717
Kelsey Hightower
718718
Jason Hildebrand
719+
Ryan Hileman
719720
Aaron Hill
720721
Joel Hillacre
721722
Richie Hindle
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Audit hooks are now fired for frame.f_code, traceback.tb_frame, and generator code/frame attribute access.

Objects/frameobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
static PyMemberDef frame_memberlist[] = {
1515
{"f_back", T_OBJECT, OFF(f_back), READONLY},
16-
{"f_code", T_OBJECT, OFF(f_code), READONLY},
16+
{"f_code", T_OBJECT, OFF(f_code), READONLY|READ_RESTRICTED},
1717
{"f_builtins", T_OBJECT, OFF(f_builtins), READONLY},
1818
{"f_globals", T_OBJECT, OFF(f_globals), READONLY},
1919
{"f_lasti", T_INT, OFF(f_lasti), READONLY},

Objects/genobject.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,9 @@ static PyGetSetDef gen_getsetlist[] = {
711711
};
712712

713713
static PyMemberDef gen_memberlist[] = {
714-
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
714+
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY|READ_RESTRICTED},
715715
{"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
716-
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
716+
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY|READ_RESTRICTED},
717717
{NULL} /* Sentinel */
718718
};
719719

@@ -931,9 +931,9 @@ static PyGetSetDef coro_getsetlist[] = {
931931
};
932932

933933
static PyMemberDef coro_memberlist[] = {
934-
{"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
934+
{"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY|READ_RESTRICTED},
935935
{"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
936-
{"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
936+
{"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY|READ_RESTRICTED},
937937
{"cr_origin", T_OBJECT, offsetof(PyCoroObject, cr_origin), READONLY},
938938
{NULL} /* Sentinel */
939939
};
@@ -1328,10 +1328,12 @@ static PyGetSetDef async_gen_getsetlist[] = {
13281328
};
13291329

13301330
static PyMemberDef async_gen_memberlist[] = {
1331-
{"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY},
1331+
{"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame),
1332+
READONLY|READ_RESTRICTED},
13321333
{"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running_async),
13331334
READONLY},
1334-
{"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY},
1335+
{"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code),
1336+
READONLY|READ_RESTRICTED},
13351337
{NULL} /* Sentinel */
13361338
};
13371339

Python/traceback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static PyMethodDef tb_methods[] = {
147147
};
148148

149149
static PyMemberDef tb_memberlist[] = {
150-
{"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
150+
{"tb_frame", T_OBJECT, OFF(tb_frame), READONLY|READ_RESTRICTED},
151151
{"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
152152
{"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
153153
{NULL} /* Sentinel */

0 commit comments

Comments
 (0)