diff options
author | Jeong, YunWon <69878+youknowone@users.noreply.github.com> | 2024-05-07 00:02:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-07 00:02:52 (GMT) |
commit | 8d8275b0cf43f0e20c72a9641cbddc5044cdae04 (patch) | |
tree | ea6cd6e2f4928a26d1e14c1b9f0e888b17a8966d /Python/sysmodule.c | |
parent | e272195b3eff3a78e334a601a637d198b8de2319 (diff) | |
download | cpython-8d8275b0cf43f0e20c72a9641cbddc5044cdae04.zip cpython-8d8275b0cf43f0e20c72a9641cbddc5044cdae04.tar.gz cpython-8d8275b0cf43f0e20c72a9641cbddc5044cdae04.tar.bz2 |
gh-118473: Fix set_asyncgen_hooks not to be partially set when arguments are invalid (#118474)
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 417040c..2851247 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1399,12 +1399,6 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw) Py_TYPE(finalizer)->tp_name); return NULL; } - if (_PyEval_SetAsyncGenFinalizer(finalizer) < 0) { - return NULL; - } - } - else if (finalizer == Py_None && _PyEval_SetAsyncGenFinalizer(NULL) < 0) { - return NULL; } if (firstiter && firstiter != Py_None) { @@ -1414,15 +1408,33 @@ sys_set_asyncgen_hooks(PyObject *self, PyObject *args, PyObject *kw) Py_TYPE(firstiter)->tp_name); return NULL; } - if (_PyEval_SetAsyncGenFirstiter(firstiter) < 0) { + } + + PyObject *cur_finalizer = _PyEval_GetAsyncGenFinalizer(); + + if (finalizer && finalizer != Py_None) { + if (_PyEval_SetAsyncGenFinalizer(finalizer) < 0) { return NULL; } } - else if (firstiter == Py_None && _PyEval_SetAsyncGenFirstiter(NULL) < 0) { + else if (finalizer == Py_None && _PyEval_SetAsyncGenFinalizer(NULL) < 0) { return NULL; } + if (firstiter && firstiter != Py_None) { + if (_PyEval_SetAsyncGenFirstiter(firstiter) < 0) { + goto error; + } + } + else if (firstiter == Py_None && _PyEval_SetAsyncGenFirstiter(NULL) < 0) { + goto error; + } + Py_RETURN_NONE; + +error: + _PyEval_SetAsyncGenFinalizer(cur_finalizer); + return NULL; } PyDoc_STRVAR(set_asyncgen_hooks_doc, |