Stefan Behnel added the comment:

I just noticed that I hadn't used the real "types.coroutine" in my Py3.5 tests 
when reporting back in issue 24017. When I pass a Cython generator through it, 
I get

"""
Traceback (most recent call last):
  File "tests/run/test_coroutines_pep492.pyx", line 245, in 
test_coroutines_pep492.CoroutineTest.test_func_5 
(test_coroutines_pep492.c:13445)
    for el in bar():
  File "/opt/python3.5/lib/python3.5/types.py", line 197, in wrapped
    'non-coroutine: {!r}'.format(coro))
TypeError: callable wrapped with types.coroutine() returned non-coroutine: 
<generator object at 0x7f178c458898>
"""

This is actually obvious, given that the sole purpose of the decorator is to 
turn something that is a Generator and *not* a Coroutine into something that is 
a Coroutine, as a means for the user to say "but I know better". So checking 
for the return value being a Coroutine is wrong. Instead, it should check that 
it's a Generator and if it's not an Awaitable, wrap it as a self-returning 
Awaitable. That's more or less what my proposed implementation in issue 24017 
did:

  class types_coroutine(object):
    def __init__(self, gen):
        self._gen = gen

    class as_coroutine(object):
        def __init__(self, gen):
            self._gen = gen
            self.send = gen.send
            self.throw = gen.throw
            self.close = gen.close

        def __await__(self):
            return self._gen

    def __call__(self, *args, **kwargs):
        return self.as_coroutine(self._gen(*args, **kwargs))

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24316>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to