Skip to content

Commit

Permalink
pythongh-126579: Convert sys.audit() to Argument Clinic
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 8, 2024
1 parent 061e50f commit 14825e1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 42 deletions.
51 changes: 50 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 13 additions & 41 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,56 +499,28 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
Py_RETURN_NONE;
}

PyDoc_STRVAR(audit_doc,
"audit($module, event, /, *args)\n\
--\n\
\n\
Passes the event to any audit hooks that are attached.");
/*[clinic input]
sys.audit
event: str
/
*args: tuple
Passes the event to any audit hooks that are attached.
[clinic start generated code]*/

static PyObject *
sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
sys_audit_impl(PyObject *module, const char *event, PyObject *args)
/*[clinic end generated code: output=1d0fc82da768f49d input=ec3b688527945109]*/
{
PyThreadState *tstate = _PyThreadState_GET();
_Py_EnsureTstateNotNULL(tstate);

if (argc == 0) {
_PyErr_SetString(tstate, PyExc_TypeError,
"audit() missing 1 required positional argument: "
"'event'");
return NULL;
}

assert(args[0] != NULL);

if (!should_audit(tstate->interp)) {
Py_RETURN_NONE;
}

PyObject *auditEvent = args[0];
if (!auditEvent) {
_PyErr_SetString(tstate, PyExc_TypeError,
"expected str for argument 'event'");
return NULL;
}
if (!PyUnicode_Check(auditEvent)) {
_PyErr_Format(tstate, PyExc_TypeError,
"expected str for argument 'event', not %.200s",
Py_TYPE(auditEvent)->tp_name);
return NULL;
}
const char *event = PyUnicode_AsUTF8(auditEvent);
if (!event) {
return NULL;
}

PyObject *auditArgs = _PyTuple_FromArray(args + 1, argc - 1);
if (!auditArgs) {
return NULL;
}

int res = _PySys_Audit(tstate, event, "O", auditArgs);
Py_DECREF(auditArgs);

int res = _PySys_Audit(tstate, event, "O", args);
if (res < 0) {
return NULL;
}
Expand Down Expand Up @@ -2564,7 +2536,7 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
SYS_ADDAUDITHOOK_METHODDEF
{"audit", _PyCFunction_CAST(sys_audit), METH_FASTCALL, audit_doc },
SYS_AUDIT_METHODDEF
{"breakpointhook", _PyCFunction_CAST(sys_breakpointhook),
METH_FASTCALL | METH_KEYWORDS, breakpointhook_doc},
SYS__CLEAR_INTERNAL_CACHES_METHODDEF
Expand Down

0 comments on commit 14825e1

Please sign in to comment.