Skip to content

Commit

Permalink
Address Serhiy's review
Browse files Browse the repository at this point in the history
Raise SystemError if event is NULL or if "N" format is used.
  • Loading branch information
vstinner committed Oct 5, 2023
1 parent 47ef8f0 commit 1324111
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
10 changes: 6 additions & 4 deletions Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,18 @@ accessible to C code. They all work with the current interpreter thread's
as used in :c:func:`Py_BuildValue` are available. If the built value is not
a tuple, it will be added into a single-element tuple.
:exc:`ValueError` is raised if the ``N`` format is used in *format*. The
``N`` format option consumes a reference, but since there is no way to know
whether arguments to this function will be consumed, using it may cause
reference leaks.
The ``N`` format option must not be used. It consumes a reference, but since
there is no way to know whether arguments to this function will be consumed,
using it may cause reference leaks.
Note that ``#`` format characters should always be treated as
:c:type:`Py_ssize_t`, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.
:func:`sys.audit` performs the same function from Python code.
Raise :exc:`SystemError` and return *NULL* if *event* is *NULL* or if the
``N`` format option is used.
See also :c:func:`PySys_AuditTuple`.
.. versionadded:: 3.8
Expand Down
22 changes: 10 additions & 12 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
}

if (event == NULL) {
_PyErr_SetString(ts, PyExc_ValueError,
_PyErr_SetString(ts, PyExc_SystemError,
"event argument must not be NULL");
return -1;
}

/* N format is inappropriate, because you do not know
whether the reference is consumed by the call. */
if (argFormat != NULL && strchr(argFormat, 'N')) {
_PyErr_SetString(ts, PyExc_ValueError,
_PyErr_SetString(ts, PyExc_SystemError,
"N format must not be used in the format argument");
return -1;
}
Expand Down Expand Up @@ -357,18 +357,16 @@ PySys_Audit(const char *event, const char *argFormat, ...)
int
PySys_AuditTuple(const char *event, PyObject *args)
{
if (args) {
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);
}
else {
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
Expand Down

0 comments on commit 1324111

Please sign in to comment.