Skip to content

Commit

Permalink
PySys_AuditTuple() now just calls PySys_Audit()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Sep 18, 2023
1 parent a1db71f commit 01acad0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
5 changes: 2 additions & 3 deletions Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ accessible to C code. They all work with the current interpreter thread's
.. c:function:: int PySys_AuditTuple(const char *event, PyObject *args)
Similar to :c:func:`PySys_Audit`, but event pass arguments as a Python
:class:`tuple` object. *args* can be *NULL* to pass no arguments: it is
treated the same as passing an empty tuple.
Similar to :c:func:`PySys_Audit`, but pass arguments as a Python object. To
pass no arguments, *args* can be *NULL*.
.. versionadded:: 3.13
Expand Down
24 changes: 12 additions & 12 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,30 +1330,30 @@ static int test_audit_tuple(void)

assert(!PyErr_Occurred());

// pass tuple
// pass Python tuple object
PyObject *tuple = Py_BuildValue("(i)", 444);
if (tuple == NULL) {
goto error;
}
assert(PySys_AuditTuple("_testembed.set", tuple) == 0);
assert(!PyErr_Occurred());
Py_DECREF(tuple);
assert(sawSet == 444);
Py_DECREF(tuple);

// pass Python int object
PyObject *int_arg = PyLong_FromLong(555);
if (int_arg == NULL) {
goto error;
}
assert(PySys_AuditTuple("_testembed.set", int_arg) == 0);
assert(!PyErr_Occurred());
assert(sawSet == 555);
Py_DECREF(int_arg);

// NULL is accepted and means "no arguments"
assert(PySys_AuditTuple("_testembed.test_audit_tuple", NULL) == 0);
assert(!PyErr_Occurred());

// wrong argument type
PyObject *not_tuple = PyLong_FromLong(123);
if (not_tuple == NULL) {
goto error;
}
assert(PySys_AuditTuple("_testembed.test_audit_tuple", not_tuple) == -1);
assert(PyErr_ExceptionMatches(PyExc_TypeError));
PyErr_Clear();
Py_DECREF(not_tuple);

Py_Finalize();
return 0;

Expand Down
28 changes: 4 additions & 24 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,32 +378,12 @@ PySys_Audit(const char *event, const char *format, ...)
int
PySys_AuditTuple(const char *event, PyObject *args)
{
PyThreadState *tstate = _PyThreadState_GET();
// tstate can be NULL if the function is called before Py_Initialize()
if (!should_audit_tstate(tstate)) {
return 0;
}

int delete_args = 0;
if (args == NULL) {
delete_args = 1;
args = PyTuple_New(0);
if (args == NULL) {
return -1;
}
if (args) {
return PySys_Audit(event, "O", args);
}
else if (!PyTuple_Check(args)) {
_PyErr_Format(tstate, PyExc_TypeError,
"expected tuple, got %s", Py_TYPE(args)->tp_name);
return -1;
}

int res = sys_audit_tuple(tstate, event, args);

if (delete_args) {
Py_DECREF(args);
else {
return PySys_Audit(event, NULL);
}
return res;
}

/* We expose this function primarily for our own cleanup during
Expand Down

0 comments on commit 01acad0

Please sign in to comment.