[New-bugs-announce] [issue24342] coroutine wrapper recursion

Yury Selivanov report at bugs.python.org
Mon Jun 1 04:11:34 CEST 2015


New submission from Yury Selivanov:

Consider following piece of code:

    async def foo():
        return 'spam'

    def wrapper(coro):
        async def wrap(coro):
            print('before')
            try:
                return await coro
            finally:
                print('after')
        return wrap(coro)

    import sys
    sys.set_coroutine_wrapper(wrapper)
    print(foo().send(None))


Current python will crash with a "RuntimeError: maximum recursion depth exceeded", because  "wrap" is itself a coroutine, so ceval will call "wrapper" recursively.

There are three options here:

1. Leave things as is;

2. Add a flag in tstate that coroutine_wrapper is executing, and raise a RuntimeError if it's reentering;

3. Add a flag in tstate (see 2) and skip wrapping when reentering (i.e. return what was passed to the wrapper).

The attached patch implements (2).  It also makes PyEval*CoroWrapper methods private.

I, myself, vote for option 2.

----------
assignee: yselivanov
components: Interpreter Core
files: coro_wrapper.patch
keywords: patch
messages: 244569
nosy: gvanrossum, haypo, ncoghlan, yselivanov
priority: normal
severity: normal
status: open
title: coroutine wrapper recursion
versions: Python 3.5, Python 3.6
Added file: http://bugs.python.org/file39578/coro_wrapper.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24342>
_______________________________________


More information about the New-bugs-announce mailing list