gh-85283: Add PySys_AuditTuple() function (#108965)

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().
This commit is contained in:
Victor Stinner 2023-10-05 23:59:35 +02:00 committed by GitHub
parent aaf297c048
commit bb057b3370
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 109 additions and 9 deletions

View file

@ -191,9 +191,7 @@ static int
sys_audit_tstate(PyThreadState *ts, const char *event,
const char *argFormat, va_list vargs)
{
/* N format is inappropriate, because you do not know
whether the reference is consumed by the call.
Assert rather than exception for perf reasons */
assert(event != NULL);
assert(!argFormat || !strchr(argFormat, 'N'));
if (!ts) {
@ -338,6 +336,21 @@ PySys_Audit(const char *event, const char *argFormat, ...)
return res;
}
int
PySys_AuditTuple(const char *event, PyObject *args)
{
if (args == NULL) {
return PySys_Audit(event, NULL);
}
if (!PyTuple_Check(args)) {
PyErr_Format(PyExc_TypeError, "args must be tuple, got %s",
Py_TYPE(args)->tp_name);
return -1;
}
return PySys_Audit(event, "O", args);
}
/* We expose this function primarily for our own cleanup during
* finalization. In general, it should not need to be called,
* and as such the function is not exported.
@ -509,6 +522,9 @@ sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
return NULL;
}
assert(args[0] != NULL);
assert(PyUnicode_Check(args[0]));
if (!should_audit(tstate->interp)) {
Py_RETURN_NONE;
}