mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
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:
parent
2ab5b092e5
commit
fdbeb2b4b6
10 changed files with 92 additions and 39 deletions
|
|
@ -81,22 +81,7 @@ class Hashable(metaclass=ABCMeta):
|
|||
return NotImplemented
|
||||
|
||||
|
||||
class _AwaitableMeta(ABCMeta):
|
||||
|
||||
def __instancecheck__(cls, instance):
|
||||
# This hook is needed because we can't add
|
||||
# '__await__' method to generator objects, and
|
||||
# we can't register GeneratorType on Awaitable.
|
||||
# NB: 0x100 = CO_ITERABLE_COROUTINE
|
||||
# (We don't want to import 'inspect' module, as
|
||||
# a dependency for 'collections.abc')
|
||||
if (instance.__class__ is generator and
|
||||
instance.gi_code.co_flags & 0x100):
|
||||
return True
|
||||
return super().__instancecheck__(instance)
|
||||
|
||||
|
||||
class Awaitable(metaclass=_AwaitableMeta):
|
||||
class Awaitable(metaclass=ABCMeta):
|
||||
|
||||
__slots__ = ()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue