Issue 24004: Support Awaitables (pep 492) in @asyncio.coroutine decorator
This commit is contained in:
parent
0339568753
commit
a316085192
|
@ -54,9 +54,10 @@ else:
|
||||||
inspect.CO_COROUTINE)
|
inspect.CO_COROUTINE)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from collections.abc import Coroutine as CoroutineABC
|
from collections.abc import Coroutine as CoroutineABC, \
|
||||||
|
Awaitable as AwaitableABC
|
||||||
except ImportError:
|
except ImportError:
|
||||||
CoroutineABC = None
|
CoroutineABC = AwaitableABC = None
|
||||||
|
|
||||||
|
|
||||||
# Check for CPython issue #21209
|
# Check for CPython issue #21209
|
||||||
|
@ -192,6 +193,16 @@ def coroutine(func):
|
||||||
res = func(*args, **kw)
|
res = func(*args, **kw)
|
||||||
if isinstance(res, futures.Future) or inspect.isgenerator(res):
|
if isinstance(res, futures.Future) or inspect.isgenerator(res):
|
||||||
res = yield from 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
|
return res
|
||||||
|
|
||||||
if not _DEBUG:
|
if not _DEBUG:
|
||||||
|
|
Loading…
Reference in New Issue