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_Audit() to the limited C API by vstinner · Pull Request #108571 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Aug 28, 2023 •
edited by github-actions Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This function was added in Python 3.8 by the PEP 578 "Python Runtime Audit Hooks". It is needed to convert some stdlib extensions to the limited C API, like fcntl, resource and syslog.

Move also non-limited "PerfMap" C API from Include/sysmodule.h to Include/cpython/sysmodule.h.

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

📚 Documentation preview 📚: https://cpython-previews--108571.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 mentioned this pull request Aug 28, 2023
[C API] Convert a few stdlib extensions to the limited C API (PEP 384) #85283
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Aug 28, 2023
@vstinner
vstinner force-pushed the limited_sys_audit branch from cd4e215 to ee520a1 Compare August 28, 2023 15:48
@vstinner
vstinner requested review from a team and encukou as code owners August 28, 2023 15:48
@encukou

encukou commented Aug 31, 2023

Copy link
Copy Markdown
Member

IMO this function is a poor fit for the limited API: it uses varargs, making it difficult to use from non-C languages. See also capi-workgroup/problems#35

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Aug 31, 2023

Copy link
Copy Markdown
Member Author

IMO this function is a poor fit for the limited API: it uses varargs, making it difficult to use from non-C languages. See also capi-workgroup/problems#35

The C API has multiple functions using varargs:

  • PyErr_Format()
  • PyUnicode_FromFormat()

Right, they cannot be used in programming languages other than C.

But well, C remains widely used, and using this function is relevant in C, no? Which alternative do you propose?

PySys_Audit() is related to security and it doesn't get the sys.audit() function from the sys module but uses directly the C implementation. Overriding sys.audit() has no effect on PySys_Audit().

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Sep 4, 2023

Copy link
Copy Markdown
Member

The C API has multiple functions using varargs

Yup, but they all have alternatives that don't use varargs -- you can use PyErr_SetString or PyUnicode_FromStringAndSize, after you format the string using some other mechanism.

There's no such alternative to PySys_Audit. IMO, one should be added before this is added to stable ABI. Perhaps a function that takes the tuple of arguments (which is slower as the tuple would always be built, so PySys_Audit would still be preferred if you can use varargs).

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Sep 4, 2023

Copy link
Copy Markdown
Member Author

@encukou:

Yup, but they all have alternatives that don't use varargs -- you can use PyErr_SetString or PyUnicode_FromStringAndSize, after you format the string using some other mechanism.
There's no such alternative to PySys_Audit. IMO, one should be added before this is added to stable ABI. Perhaps a function that takes the tuple of arguments (which is slower as the tuple would always be built, so PySys_Audit would still be preferred if you can use varargs).

I'm not sure that I can follow your rationale. Because there are programming languages which cannot use a C API with variadic arguments, the ... arguments in PySys_Audit(), you would like that all users of the limited C API have to go through a slower implementation, by always creating the tuple?

These programming languages can already build a tuple and call sys.audit(). There is no need to add anything here. For me, the most convenient is PyTuple_Pack(), but it also uses variadic arguments: PyObject* PyTuple_Pack(Py_ssize_t, ...). Or you can use PyObject* Py_BuildValue(const char *, ...), oh wait, it also uses variadic arguments.

Are you sure that there are programming languages which are used to write Python extensions and cannot use variadic arguments? In C, variadic arguments are very commonly used, by printf() for example.

For me, the advantage of adding PySys_Audit() is that its API is easier to use than having to build a tuple manually (for check errors), get the sys module (check for errors), call the audit() function (check for errors), and clear temporary objects.

Note: PySys_Audit() is implemented by calling Py_VaBuildValue(argFormat, vargs) to make a Python tuple object.


If there are programming languages which are used to write Python extensions and cannot use variadic arguments, why not letting them to call sys.audit() via the "Python" API (create a tuple, get the sys module, call the audit() function)? Why would other programming languages have to suffer from that?

I think that C and C++ are still commonly used these days to write C extensions for Python.

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Sep 4, 2023

Copy link
Copy Markdown
Member

I'm not sure that I can follow your rationale. Because there are programming languages which cannot use a C API with variadic arguments, the ... arguments in PySys_Audit(), you would like that all users of the limited C API have to go through a slower implementation, by always creating the tuple?

No. Those that can use PySys_Audit should still use that. I'm asking to also add an alternative.

Are you sure that there are programming languages which are used to write Python extensions and cannot use variadic arguments?

It was mentioned by @steve-s in capi-workgroup/problems#35 (comment). Should we dig for the rationale?

In C, variadic arguments are very commonly used, by printf() for example.

Yup, C uses them a lot, but e.g. Rust is doing just fine without vararg functions.

These programming languages can already build a tuple and call sys.audit().

As you mentioned, sys.audit can be reassigned by the user. It's not a proper replacement.

All reactions

Sorry, something went wrong.

Uh oh!

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

This was referenced Sep 4, 2023
Use of features specific to the C language capi-workgroup/problems#35
Open
gh-85283: Add PySys_AuditTuple() function #108965
Merged
@vstinner

vstinner commented Sep 5, 2023

Copy link
Copy Markdown
Member Author

I wrote PR #108965 to add PySys_AuditTuple() to the non-limited C API.

Since it's a new API, I would prefer to start by adding it the non-limited C API first, and wait one version to move it the limited C API. Just in case if something goes wrong.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
vstinner force-pushed the limited_sys_audit branch from f1da73b to 8b5bf44 Compare September 5, 2023 22:28
@encukou

encukou commented Sep 7, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Thank you!
OK, but let's wait with PySys_Audit too, so both are added at the same time.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Sep 13, 2023

Copy link
Copy Markdown
Member Author

OK, but let's wait with PySys_Audit too, so both are added at the same time.

Sorry, you want to wait for what?

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Sep 18, 2023

Copy link
Copy Markdown
Member

I would prefer to wait until both PySys_Audit and PySys_AuditTuple can be added to the stable ABI, together.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
pythongh-85283: Add PySys_Audit() to the limited C API
cf09536
This function was added in Python 3.8 by the PEP 578 "Python Runtime
Audit Hooks". Add also PySys_AuditTuple() to the limited C API,
function added to Python 3.13.

Move also non-limited "PerfMap" C API from Include/sysmodule.h to
Include/cpython/sysmodule.h.
@vstinner
vstinner force-pushed the limited_sys_audit branch from 8b5bf44 to cf09536 Compare October 17, 2023 08:46
@vstinner

vstinner commented Oct 17, 2023

Copy link
Copy Markdown
Member Author

I would prefer to wait until both PySys_Audit and PySys_AuditTuple can be added to the stable ABI, together.

Right. I updated my PR to add PySys_Audit() and PySys_AuditTuple() to the limited C API version 3.13 (and so to the stable ABI).

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
vstinner merged commit 2324652 into python:main Oct 17, 2023
@vstinner
vstinner deleted the limited_sys_audit branch October 17, 2023 14:02
@bedevere-app bedevere-app Bot removed the awaiting core review label Oct 17, 2023
@vstinner

vstinner commented Oct 17, 2023

Copy link
Copy Markdown
Member Author

Merged. Thanks @encukou for telling me about variadic arguments, the additional of the PySys_AuditTuple() function solves this issue.

All reactions

Sorry, something went wrong.

Uh oh!

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

aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@vstinner @aisk
pythongh-85283: Add PySys_Audit() to the limited C API (python#108571)
89ad127
The PySys_Audit() function was added in Python 3.8 by the PEP 578
"Python Runtime Audit Hooks".

Add also PySys_AuditTuple() to the limited C API, function added
to Python 3.13.

Move non-limited "PerfMap" C API from Include/sysmodule.h to
Include/cpython/sysmodule.h.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@vstinner @Glyphack
pythongh-85283: Add PySys_Audit() to the limited C API (python#108571)
3ec26a2
The PySys_Audit() function was added in Python 3.8 by the PEP 578
"Python Runtime Audit Hooks".

Add also PySys_AuditTuple() to the limited C API, function added
to Python 3.13.

Move non-limited "PerfMap" C API from Include/sysmodule.h to
Include/cpython/sysmodule.h.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou Awaiting requested review from encukou encukou is a code owner

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.

3 participants

@vstinner @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.