Issue #24400: Resurrect inspect.isawaitable()

collections.abc.Awaitable and collections.abc.Coroutine no longer
use __instancecheck__ hook to detect generator-based coroutines.

inspect.isawaitable() can be used to detect generator-based coroutines
and to distinguish them from regular generator objects.
This commit is contained in:
Yury Selivanov 2015-07-03 13:11:35 -04:00
parent 2ab5b092e5
commit fdbeb2b4b6
10 changed files with 92 additions and 39 deletions

View file

@ -241,12 +241,12 @@ def coroutine(func):
@_functools.wraps(func)
def wrapped(*args, **kwargs):
coro = func(*args, **kwargs)
if coro.__class__ is CoroutineType:
# 'coro' is a native coroutine object.
if (coro.__class__ is CoroutineType or
coro.__class__ is GeneratorType and coro.gi_code.co_flags & 0x100):
# 'coro' is a native coroutine object or an iterable coroutine
return coro
if (coro.__class__ is GeneratorType or
(isinstance(coro, _collections_abc.Generator) and
not isinstance(coro, _collections_abc.Coroutine))):
if (isinstance(coro, _collections_abc.Generator) and
not isinstance(coro, _collections_abc.Coroutine)):
# 'coro' is either a pure Python generator iterator, or it
# implements collections.abc.Generator (and does not implement
# collections.abc.Coroutine).