GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-85283: Add PySys_AuditTuple() function by vstinner · Pull Request #108965 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Sep 5, 2023 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PySys_Audit() now raises an exception if the event argument is NULL or if the "N" format is used in the format argument.

Add tests on PySys_AuditTuple() and on new PySys_Audit() error code paths.

  • Issue: [C API] Convert a few stdlib extensions to the limited C API (PEP 384) #85283

📚 Documentation preview 📚: https://cpython-previews--108965.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added the awaiting core review label Sep 5, 2023
@bedevere-bot bedevere-bot mentioned this pull request Sep 5, 2023
[C API] Convert a few stdlib extensions to the limited C API (PEP 384) #85283
Closed
@vstinner

vstinner commented Sep 5, 2023

Copy link
Copy Markdown
Member Author

For the rationale of adding PySys_AuditTuple() in addition to the existing PySys_Audit(), see the discussion on my PR #108571 "Add PySys_Audit() to the limited C API" and this discussion: capi-workgroup/problems#35 (comment)

In short, it's possible to use the Python C API in Go, but Go doesn't support ... variadic arguments (<stdarg.h>). I would like to add PySys_Audit() argument to the limited C API, but @encukou asked me to add a different flavor which accepts a tuple. So here you have.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner mentioned this pull request Sep 5, 2023
gh-85283: Add PySys_Audit() to the limited C API #108571
Merged
@vstinner

vstinner commented Sep 6, 2023

Copy link
Copy Markdown
Member Author

cc @encukou

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 6, 2023

Copy link
Copy Markdown
Member Author

cc @zooba

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner force-pushed the sys_audit_tuple branch from 2386731 to 528ac80 Compare September 6, 2023 15:12
@zooba

zooba commented Sep 6, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Please don't make every PySys_Audit call convert the arguments to a tuple eagerly. It deliberately defers it until it knows there's at least one hook that's going to be called.

Please don't slow down the regular API for the sake of the limited API. If anything, make PySys_AuditTuple just pass its tuple to PySys_Audit, which will hide the ... from callers but otherwise not change any other code.

encukou and serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 6, 2023

Copy link
Copy Markdown
Member Author

Please don't make every PySys_Audit call convert the arguments to a tuple eagerly. It deliberately defers it until it knows there's at least one hook that's going to be called.

Oh! Nicely spotted, I knew that, I recall that I paid attention to that when you implemented audit hooks :-) Oops, it's now fixed. I made sure that "should audit" is checked as soon as possible. In exchange, arguments are only checked if a audit hook is added (if "should audit" is true).

Please don't slow down the regular API for the sake of the limited API. If anything, make PySys_AuditTuple just pass its tuple to PySys_Audit, which will hide the ... from callers but otherwise not change any other code.

I fixed my PR, existing APIs are still lazy as before, and I made PySys_AuditTuple() a litlte bit more lazy (exit earlier, before checking arguments).

@zooba: Would you mind to review the updated PR?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 6, 2023

Copy link
Copy Markdown
Member Author

Please don't slow down the regular API for the sake of the limited API.

Oh by the way, as I wrote earlier: I would prefer to not add PySys_AuditTuple() to the limited C API.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zooba

zooba commented Sep 6, 2023

Copy link
Copy Markdown
Member

I can't do a thorough review of that much code right now.

Why isn't the new function just this?

int PySys_AuditTuple(const char *event, PyObject *tuple) {
    return PySys_Audit(event, "O", tuple);
}

If a single object is passed in and it's a tuple, it's meant to be left alone (i.e. splatted). If you want a single argument that happens to be a tuple, you need PySys_Audit(event, "(O)", tuple).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 6, 2023

Copy link
Copy Markdown
Member Author

If a single object is passed in and it's a tuple, it's meant to be left alone (i.e. splatted).

Hum, I dislike this kind of magic "unpack or not" depending on the number of items. What if an event takes a single argument which a tuple? What if you want to unpack the tuple since the event has multiple arguments?

I would prefer a regular API: always pass a tuple.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zooba

zooba commented Sep 6, 2023

Copy link
Copy Markdown
Member

The magic already exists, and I showed in my message how to handle the case where you pass a tuple. It's also in the docs. If you want to make a massively complicated change instead of using the already existing API, that's up to you, but it's going to make it much harder to get a review and will cause maintenance headaches for all of us later on.

serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Sep 18, 2023

Copy link
Copy Markdown
Member

I concur with @zooba.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner force-pushed the sys_audit_tuple branch from cb25901 to 01acad0 Compare September 18, 2023 17:11
@vstinner

vstinner commented Sep 18, 2023

Copy link
Copy Markdown
Member Author

Ok, I updated my PR so PySys_AuditTuple() simply calls PySys_Audit(). I updated the doc and the added tests.

@zooba @serhiy-storchaka: Would you mind to review the updated PR?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Sep 19, 2023
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Are all these changes necessary? It looks to me that they were needed if you implement PySys_AuditTuple() on lower level. But if you make it a simple wrapper around PySys_Audit(), you can write it in 10-15 lines of code.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner force-pushed the sys_audit_tuple branch from 01acad0 to 579731b Compare September 19, 2023 14:57
@vstinner

vstinner commented Sep 19, 2023

Copy link
Copy Markdown
Member Author

Ok, I updated my PR so PySys_AuditTuple() simply calls PySys_Audit().

Oh, I removed too many changes and new tests failed. I added again checks on event and format to fix tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Sep 19, 2023
View reviewed changes
Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 19, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Ok, I updated my PR to add assertions to check arguments when should_audit() is false, and I added a PyTuple_Check() to reject types other than tuple.

@serhiy-storchaka: Would you mind to review the updated PR?

Note: sys_audit() is implemented by calling _PySys_Audit(tstate, event, "O", auditArgs).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zooba
zooba reviewed Sep 20, 2023
View reviewed changes
Comment thread Programs/_testembed.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

zooba
zooba requested changes Sep 20, 2023
View reviewed changes

@zooba zooba left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Overall the change is good, but I don't like having sometimes assertions and sometimes exceptions for invalid values in debug builds.

Also you should benchmark the extra runtime checks you're doing now. Previously I benchmarked and found that I needed to use assertions to maintain performance when adding these functions in the first place, so if you're going to regress them, it should be its own discussion and not slipped into an unrelated change.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/c-api/sys.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner force-pushed the sys_audit_tuple branch from 8f6f566 to d12654a Compare September 29, 2023 22:26
@vstinner

vstinner commented Sep 29, 2023

Copy link
Copy Markdown
Member Author

@zooba @serhiy-storchaka: I updated my PR to address latest @zooba's review, would you mind to review it?

  • Assertions are now also tested in all cases, not only if should_audit() is false. Well, never caled these functions with invalid arguments!
  • Fix the doc.
  • Replace assert() with printf+return in test_audit_tuple.

I rebased my PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Sep 30, 2023
View reviewed changes
Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 30, 2023

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: I updated my PR to take your review in account.

SystemError is now raised if events is NULL or if the "N" format option is used. I documented the error, as requested by @zooba.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner force-pushed the sys_audit_tuple branch from ddbd851 to 1324111 Compare October 5, 2023 21:15
@vstinner
vstinner enabled auto-merge (squash) October 5, 2023 21:22
@vstinner
vstinner disabled auto-merge October 5, 2023 21:28
@vstinner
pythongh-85283: Add PySys_AuditTuple() function
0ca0240
sys.audit() now has assertions to check that the event argument is
not NULL and that the format argument does not use the "N" format.

Add tests on PySys_AuditTuple().
@vstinner
vstinner force-pushed the sys_audit_tuple branch from 1324111 to 0ca0240 Compare October 5, 2023 21:35
@vstinner

vstinner commented Oct 5, 2023

Copy link
Copy Markdown
Member Author

@zooba:

Overall the change is good, but I don't like having sometimes assertions and sometimes exceptions for invalid values in debug builds.
Also you should benchmark the extra runtime checks you're doing now. Previously I benchmarked and found that I needed to use assertions to maintain performance when adding these functions in the first place, so if you're going to regress them, it should be its own discussion and not slipped into an unrelated change.

Ok. I removed the two checks which raised exceptions to leave PySys_Audit() and sys.audit() unchanged. I only added assertions to sys.audit() to check arguments even if there is no hook. They can be added again separately later if needed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner enabled auto-merge (squash) October 5, 2023 21:40
@vstinner
vstinner merged commit bb057b3 into python:main Oct 5, 2023
@vstinner
vstinner deleted the sys_audit_tuple branch October 5, 2023 21:59
@bedevere-app bedevere-app Bot removed the awaiting core review label Oct 5, 2023
@vstinner

vstinner commented Oct 5, 2023

Copy link
Copy Markdown
Member Author

Ok, I addressed all reviews. Thanks a lot for all your reviews :-) I merged my PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 6, 2023

Copy link
Copy Markdown
Member

Thank you Victor.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@vstinner @Glyphack
pythongh-85283: Add PySys_AuditTuple() function (python#108965)
76f9909
sys.audit() now has assertions to check that the event argument is
not NULL and that the format argument does not use the "N" format.

Add tests on PySys_AuditTuple().
@devdanzin devdanzin mentioned this pull request Oct 27, 2024
sys.audit(0) aborts due to an assertion in debug build #126018
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou left review comments
@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@zooba zooba zooba requested changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@vstinner @zooba @serhiy-storchaka @encukou @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.