bpo-38410: Properly handle PySys_Audit() failures (GH-18658)

_PyEval_SetAsyncGenFinalizer() and _PyEval_SetAsyncGenFirstiter()
didn't include proper error handling for their PySys_Audit() calls.

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
Serhiy Storchaka 2020-03-02 09:58:48 +02:00 committed by GitHub
parent a7b8a969eb
commit 87a4cd5fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 32 deletions

View File

@ -0,0 +1,2 @@
Properly handle :func:`sys.audit` failures in
:func:`sys.set_asyncgen_hooks`. Based on patch by Zackery Spytz.

View File

@ -4742,19 +4742,6 @@ _PyEval_GetCoroutineOriginTrackingDepth(void)
return tstate->coroutine_origin_tracking_depth;
}
void
_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
{
PyThreadState *tstate = _PyThreadState_GET();
if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
return;
}
Py_XINCREF(firstiter);
Py_XSETREF(tstate->async_gen_firstiter, firstiter);
}
PyObject *
_PyEval_GetAsyncGenFirstiter(void)
{
@ -4762,19 +4749,6 @@ _PyEval_GetAsyncGenFirstiter(void)
return tstate->async_gen_firstiter;
}
void
_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
{
PyThreadState *tstate = _PyThreadState_GET();
if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
return;
}
Py_XINCREF(finalizer);
Py_XSETREF(tstate->async_gen_finalizer, finalizer);
}
PyObject *
_PyEval_GetAsyncGenFinalizer(void)
{

View File

@ -1178,6 +1178,51 @@ static PyStructSequence_Desc asyncgen_hooks_desc = {
2
};
static int
set_async_gen_firstiter(PyObject *firstiter)
{
PyThreadState *tstate = _PyThreadState_GET();
if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
return -1;
}
Py_XINCREF(firstiter);
Py_XSETREF(tstate->async_gen_firstiter, firstiter);
return 0;
}
void
_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
{
if (set_async_gen_firstiter(firstiter) < 0) {
PyErr_WriteUnraisable(NULL);
}
}
static int
set_async_gen_finalizer(PyObject *finalizer)
{
PyThreadState *tstate = _PyThreadState_GET();
if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
return -1;
}
Py_XINCREF(finalizer);
Py_XSETREF(tstate->async_gen_finalizer, finalizer);
return 0;
}
void
_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
{
if (set_async_gen_finalizer(finalizer) < 0) {
PyErr_WriteUnraisable(NULL);
}
}
static PyObject *
sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
{
@ -1198,10 +1243,12 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
Py_TYPE(finalizer)->tp_name);
return NULL;
}
_PyEval_SetAsyncGenFinalizer(finalizer);
if (set_async_gen_finalizer(finalizer) < 0) {
return NULL;
}
}
else if (finalizer == Py_None) {
_PyEval_SetAsyncGenFinalizer(NULL);
else if (finalizer == Py_None && set_async_gen_finalizer(NULL) < 0) {
return NULL;
}
if (firstiter && firstiter != Py_None) {
@ -1211,10 +1258,12 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw)
Py_TYPE(firstiter)->tp_name);
return NULL;
}
_PyEval_SetAsyncGenFirstiter(firstiter);
if (set_async_gen_firstiter(firstiter) < 0) {
return NULL;
}
}
else if (firstiter == Py_None) {
_PyEval_SetAsyncGenFirstiter(NULL);
else if (firstiter == Py_None && set_async_gen_firstiter(NULL) < 0) {
return NULL;
}
Py_RETURN_NONE;