Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-85283: Add PySys_Audit() to the limited C API#108571

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 1 commit intopython:mainfromvstinner:limited_sys_audit
Oct 17, 2023

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedAug 28, 2023
edited by github-actionsbot
Loading

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.


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

@encukou
Copy link
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 alsocapi-workgroup/problems#35

@vstinner
Copy link
MemberAuthor

IMO this function is a poor fit for the limited API: it uses varargs, making it difficult to use from non-C languages. See alsocapi-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 thesys.audit() function from thesys module but uses directly the C implementation. Overridingsys.audit() has no effect on PySys_Audit().

@encukou
Copy link
Member

The C API has multiple functions using varargs

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

There's no such alternative toPySys_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, soPySys_Audit would still be preferred if you can use varargs).

@vstinner
Copy link
MemberAuthor

@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 inPySys_Audit(), you would like that all users of the limited C API have to go through aslower implementation, by always creating the tuple?

These programming languages can already build a tuple and callsys.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 usePyObject* 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, byprintf() for example.

For me, the advantage of addingPySys_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 theaudit() function (check for errors), and clear temporary objects.

Note:PySys_Audit() is implemented by callingPy_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 callsys.audit() via the "Python" API (create a tuple, get the sys module, call theaudit() 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.

@encukou
Copy link
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 usePySys_Audit should still use that. I'm asking toalso 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 incapi-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.

@vstinner
Copy link
MemberAuthor

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.

@encukou
Copy link
Member

encukou commentedSep 7, 2023
edited
Loading

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

@vstinner
Copy link
MemberAuthor

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

Sorry, you want to wait for what?

@encukou
Copy link
Member

I would prefer to wait until bothPySys_Audit andPySys_AuditTuple can be added to the stable ABI, together.

This function was added in Python 3.8 by the PEP 578 "Python RuntimeAudit 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 toInclude/cpython/sysmodule.h.
@vstinner
Copy link
MemberAuthor

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).

@vstinnervstinner merged commit2324652 intopython:mainOct 17, 2023
@vstinnervstinner deleted the limited_sys_audit branchOctober 17, 2023 14:02
@vstinner
Copy link
MemberAuthor

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

aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 11, 2024
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 addedto Python 3.13.Move non-limited "PerfMap" C API from Include/sysmodule.h toInclude/cpython/sysmodule.h.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
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 addedto Python 3.13.Move non-limited "PerfMap" C API from Include/sysmodule.h toInclude/cpython/sysmodule.h.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@encukouencukouAwaiting requested review from encukouencukou is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@vstinner@encukou@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp