Issue 24342: Let wrapper set by sys.set_coroutine_wrapper fail gracefully

This commit is contained in:
Yury Selivanov 2015-06-02 18:43:51 -04:00
parent 231d90609b
commit aab3c4a211
6 changed files with 68 additions and 10 deletions

View file

@ -3921,7 +3921,6 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
if (co->co_flags & CO_GENERATOR) {
PyObject *gen;
PyObject *coroutine_wrapper;
/* Don't need to keep the reference to f_back, it will be set
* when the generator is resumed. */
@ -3935,14 +3934,9 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
if (gen == NULL)
return NULL;
if (co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE)) {
coroutine_wrapper = _PyEval_GetCoroutineWrapper();
if (coroutine_wrapper != NULL) {
PyObject *wrapped =
PyObject_CallFunction(coroutine_wrapper, "N", gen);
gen = wrapped;
}
}
if (co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))
return _PyEval_ApplyCoroutineWrapper(gen);
return gen;
}
@ -4407,6 +4401,33 @@ _PyEval_GetCoroutineWrapper(void)
return tstate->coroutine_wrapper;
}
PyObject *
_PyEval_ApplyCoroutineWrapper(PyObject *gen)
{
PyObject *wrapped;
PyThreadState *tstate = PyThreadState_GET();
PyObject *wrapper = tstate->coroutine_wrapper;
if (tstate->in_coroutine_wrapper) {
assert(wrapper != NULL);
PyErr_Format(PyExc_RuntimeError,
"coroutine wrapper %.150R attempted "
"to recursively wrap %.150R",
wrapper,
gen);
return NULL;
}
if (wrapper == NULL) {
return gen;
}
tstate->in_coroutine_wrapper = 1;
wrapped = PyObject_CallFunction(wrapper, "N", gen);
tstate->in_coroutine_wrapper = 0;
return wrapped;
}
PyObject *
PyEval_GetBuiltins(void)
{