Skip to content

Commit 8ab272f

Browse files
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 (cherry picked from commit bb2f3ff) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent 10665ac commit 8ab272f

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
@@ -4708,6 +4708,9 @@ environment. Code objects are returned by the built-in :func:`compile` function
47084708
and can be extracted from function objects through their :attr:`__code__`
47094709
attribute. See also the :mod:`code` module.
47104710

4711+
Accessing ``__code__`` raises an :ref:`auditing event <auditing>`
4712+
``object.__getattr__`` with arguments ``obj`` and ``"__code__"``.
4713+
47114714
.. index::
47124715
builtin: exec
47134716
builtin: eval

Doc/reference/datamodel.rst

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

1003+
Accessing ``f_code`` raises an :ref:`auditing event <auditing>`
1004+
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
1005+
10031006
.. index::
10041007
single: f_trace (frame attribute)
10051008
single: f_trace_lines (frame attribute)
@@ -1084,6 +1087,9 @@ Internal types
10841087
:keyword:`try` statement with no matching except clause or with a
10851088
finally clause.
10861089

1090+
Accessing ``tb_frame`` raises an :ref:`auditing event <auditing>`
1091+
``object.__getattr__`` with arguments ``obj`` and ``"tb_frame"``.
1092+
10871093
.. index::
10881094
single: tb_next (traceback attribute)
10891095

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ Kevan Heydon
696696
Wouter van Heyst
697697
Kelsey Hightower
698698
Jason Hildebrand
699+
Ryan Hileman
699700
Aaron Hill
700701
Joel Hillacre
701702
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
@@ -708,9 +708,9 @@ static PyGetSetDef gen_getsetlist[] = {
708708
};
709709

710710
static PyMemberDef gen_memberlist[] = {
711-
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
711+
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY|READ_RESTRICTED},
712712
{"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
713-
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
713+
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY|READ_RESTRICTED},
714714
{NULL} /* Sentinel */
715715
};
716716

@@ -944,9 +944,9 @@ static PyGetSetDef coro_getsetlist[] = {
944944
};
945945

946946
static PyMemberDef coro_memberlist[] = {
947-
{"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
947+
{"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY|READ_RESTRICTED},
948948
{"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
949-
{"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
949+
{"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY|READ_RESTRICTED},
950950
{"cr_origin", T_OBJECT, offsetof(PyCoroObject, cr_origin), READONLY},
951951
{NULL} /* Sentinel */
952952
};
@@ -1341,10 +1341,12 @@ static PyGetSetDef async_gen_getsetlist[] = {
13411341
};
13421342

13431343
static PyMemberDef async_gen_memberlist[] = {
1344-
{"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY},
1344+
{"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame),
1345+
READONLY|READ_RESTRICTED},
13451346
{"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running_async),
13461347
READONLY},
1347-
{"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY},
1348+
{"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code),
1349+
READONLY|READ_RESTRICTED},
13481350
{NULL} /* Sentinel */
13491351
};
13501352

Python/traceback.c

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

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

0 commit comments

Comments
 (0)