Skip to content

bpo-42800: add audit hooks for f_code and tb_frame #24182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2021

Conversation

lunixbochs
Copy link
Contributor

@lunixbochs lunixbochs commented Jan 10, 2021

  • 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
  • Document audit hooks for tb_frame, f_code, and __code__

  • Add an AUDIT_READ attribute flag aliased to READ_RESTRICTED.

  • Update obsolete RESTRICTED flag documentation.

https://bugs.python.org/issue42800

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@lunixbochs

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@lunixbochs
Copy link
Contributor Author

I've amended this PR to also cover the code attributes of generator objects (generator, coroutine, and async generator). I'll comment on this in the BPO.

* 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

* Document audit hooks for tb_frame, f_code, and __code__
* Add an AUDIT_READ attribute flag aliased to READ_RESTRICTED.
* Update obsolete RESTRICTED flag documentation.
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 23, 2021
@@ -5206,6 +5206,9 @@ environment. Code objects are returned by the built-in :func:`compile` function
and can be extracted from function objects through their :attr:`__code__`
attribute. See also the :mod:`code` module.

Accessing ``__code__`` raises an :ref:`auditing event <auditing>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to add a regular ..audit_event item (or whatever the name is) and override the text by putting an indented paragraph below it. That will make sure the entry gets into the auto-generated table. Otherwise, it all looks good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, and IMO it makes the audit event table look worse due to the way it only references these docs via unlabeled footnotes in the object.__getattr__ section, and it scrambles the order of the footnotes so the first object.__getattr__ footnote no longer points at object, it points at one of the specific attributes.

My longer reasoning is here: https://bugs.python.org/msg384749

I think a better answer would be for someone who understands the sphinx table generator to add a directive that inserts labeled cross-references for each attribute below the object.__getattr__ row.

Right now when you add cross-references for all of the attributes the table row looks something like this:

object.__getattr__ | description | [1] [2] [3] [4] [5]

But would be actually useful with a structure like this that actually labels the references:

event description references
object.__getattr__ description [1]
attr traceback.tb_frame description [1]
attr code.f_code description [1]

@zooba
Copy link
Member

zooba commented Feb 23, 2021

This should be backported to 3.8 and 3.9, but it'll have to be modified slightly to leave the documentation in for the removed constants.

@lunixbochs
Copy link
Contributor Author

Note that even though the RESTRICTED constants appeared on a couple of attributes up to 3.8, nothing seems to actually use them (as of 3.0 when restricted mode was removed?) until they were used for read audit in 3.8, so the documentation (which said, e.g. "Not readable in restricted mode") was already incorrect.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Feb 24, 2021
@zooba zooba merged commit 9a2c2a9 into python:master Apr 29, 2021
zooba added a commit that referenced this pull request May 3, 2021
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
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 3, 2021
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>
miss-islington added a commit that referenced this pull request May 3, 2021
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants