@@ -146,8 +146,6 @@ static void format_exc_unbound(PyCodeObject *co, int oparg);
146146static PyObject * unicode_concatenate (PyObject * , PyObject * ,
147147 PyFrameObject * , unsigned char * );
148148static PyObject * special_lookup (PyObject * , _Py_Identifier * );
149- static PyObject * apply_coroutine_wrapper (PyObject * );
150-
151149
152150#define NAME_ERROR_MSG \
153151 "name '%.200s' is not defined"
@@ -3923,6 +3921,18 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
39233921
39243922 if (co -> co_flags & CO_GENERATOR ) {
39253923 PyObject * gen ;
3924+ PyObject * coro_wrapper = tstate -> coroutine_wrapper ;
3925+ int is_coro = co -> co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE );
3926+
3927+ if (is_coro && tstate -> in_coroutine_wrapper ) {
3928+ assert (coro_wrapper != NULL );
3929+ PyErr_Format (PyExc_RuntimeError ,
3930+ "coroutine wrapper %.200R attempted "
3931+ "to recursively wrap %.200R" ,
3932+ coro_wrapper ,
3933+ co );
3934+ goto fail ;
3935+ }
39263936
39273937 /* Don't need to keep the reference to f_back, it will be set
39283938 * when the generator is resumed. */
@@ -3936,8 +3946,13 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
39363946 if (gen == NULL )
39373947 return NULL ;
39383948
3939- if (co -> co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE ))
3940- return apply_coroutine_wrapper (gen );
3949+ if (is_coro && coro_wrapper != NULL ) {
3950+ PyObject * wrapped ;
3951+ tstate -> in_coroutine_wrapper = 1 ;
3952+ wrapped = PyObject_CallFunction (coro_wrapper , "N" , gen );
3953+ tstate -> in_coroutine_wrapper = 0 ;
3954+ return wrapped ;
3955+ }
39413956
39423957 return gen ;
39433958 }
@@ -5232,33 +5247,6 @@ unicode_concatenate(PyObject *v, PyObject *w,
52325247 return res ;
52335248}
52345249
5235- static PyObject *
5236- apply_coroutine_wrapper (PyObject * gen )
5237- {
5238- PyObject * wrapped ;
5239- PyThreadState * tstate = PyThreadState_GET ();
5240- PyObject * wrapper = tstate -> coroutine_wrapper ;
5241-
5242- if (tstate -> in_coroutine_wrapper ) {
5243- assert (wrapper != NULL );
5244- PyErr_Format (PyExc_RuntimeError ,
5245- "coroutine wrapper %.200R attempted "
5246- "to recursively wrap %.200R" ,
5247- wrapper ,
5248- gen );
5249- return NULL ;
5250- }
5251-
5252- if (wrapper == NULL ) {
5253- return gen ;
5254- }
5255-
5256- tstate -> in_coroutine_wrapper = 1 ;
5257- wrapped = PyObject_CallFunction (wrapper , "N" , gen );
5258- tstate -> in_coroutine_wrapper = 0 ;
5259- return wrapped ;
5260- }
5261-
52625250#ifdef DYNAMIC_EXECUTION_PROFILE
52635251
52645252static PyObject *
0 commit comments