summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-03-02 07:58:48 (GMT)
committerGitHub <noreply@github.com>2020-03-02 07:58:48 (GMT)
commit87a4cd5fbebdd0e6166b421d2c3706bc2f2e5a11 (patch)
treee6b4edd79f7cacf27ea9bc0c13ded2b5b34ce641 /Python
parenta7b8a969eb3daacb1fcb029a8c5fecb5d09c964b (diff)
downloadcpython-87a4cd5fbebdd0e6166b421d2c3706bc2f2e5a11.zip
cpython-87a4cd5fbebdd0e6166b421d2c3706bc2f2e5a11.tar.gz
cpython-87a4cd5fbebdd0e6166b421d2c3706bc2f2e5a11.tar.bz2
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>
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c26
-rw-r--r--Python/sysmodule.c61
2 files changed, 55 insertions, 32 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 3306fb9..5d78d5a 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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)
{
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index da4b6e1..b544f2b 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -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;