From: Yury Selivanov <yselivanov@sprymix.com> Date: Sun, 31 May 2015 01:02:12 +0000 (-0400) Subject: Issue 24004: Support Awaitables (pep 492) in @asyncio.coroutine decorator X-Git-Tag: v3.5.0b3~134^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a316085192382e56c8322da395231e24c6dbdc5b;p=python Issue 24004: Support Awaitables (pep 492) in @asyncio.coroutine decorator --- diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py index 1e0a70497f..4933cf83d2 100644 --- a/Lib/asyncio/coroutines.py +++ b/Lib/asyncio/coroutines.py @@ -54,9 +54,10 @@ else: inspect.CO_COROUTINE) try: - from collections.abc import Coroutine as CoroutineABC + from collections.abc import Coroutine as CoroutineABC, \ + Awaitable as AwaitableABC except ImportError: - CoroutineABC = None + CoroutineABC = AwaitableABC = None # Check for CPython issue #21209 @@ -192,6 +193,16 @@ def coroutine(func): res = func(*args, **kw) if isinstance(res, futures.Future) or inspect.isgenerator(res): res = yield from res + elif AwaitableABC is not None: + # If 'func' returns an Awaitable (new in 3.5) we + # want to run it. + try: + await_meth = res.__await__ + except AttributeError: + pass + else: + if isinstance(res, AwaitableABC): + res = yield from await_meth() return res if not _DEBUG: