summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2015-07-03 17:11:35 (GMT)
committerYury Selivanov <yselivanov@sprymix.com>2015-07-03 17:11:35 (GMT)
commitfdbeb2b4b67e1e44c96127a06cf1bdf878f4f7ca (patch)
tree249f7190feeef1e18f5c88b5987f6e632193df33 /Lib/inspect.py
parent2ab5b092e5a82390c236708b7c163a32dfc928a1 (diff)
downloadcpython-fdbeb2b4b67e1e44c96127a06cf1bdf878f4f7ca.zip
cpython-fdbeb2b4b67e1e44c96127a06cf1bdf878f4f7ca.tar.gz
cpython-fdbeb2b4b67e1e44c96127a06cf1bdf878f4f7ca.tar.bz2
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.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index f48769e..45679cf 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -207,6 +207,13 @@ def iscoroutine(object):
"""Return true if the object is a coroutine."""
return isinstance(object, types.CoroutineType)
+def isawaitable(object):
+ """Return true is object can be passed to an ``await`` expression."""
+ return (isinstance(object, types.CoroutineType) or
+ isinstance(object, types.GeneratorType) and
+ object.gi_code.co_flags & CO_ITERABLE_COROUTINE or
+ isinstance(object, collections.abc.Awaitable))
+
def istraceback(object):
"""Return true if the object is a traceback.