diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-12 02:57:16 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-12 02:57:16 (GMT) |
commit | 7544508f0245173bff5866aa1598c8f6cce1fc5f (patch) | |
tree | bf80850d9cd46fc811f04b8c2484fb50775c697d /Lib/inspect.py | |
parent | 4e6bf4b3da03b132b0698f30ee931a350585b117 (diff) | |
download | cpython-7544508f0245173bff5866aa1598c8f6cce1fc5f.zip cpython-7544508f0245173bff5866aa1598c8f6cce1fc5f.tar.gz cpython-7544508f0245173bff5866aa1598c8f6cce1fc5f.tar.bz2 |
PEP 0492 -- Coroutines with async and await syntax. Issue #24017.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 60890f2..e52d86e 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -33,6 +33,7 @@ __author__ = ('Ka-Ping Yee <ping@lfw.org>', import ast import dis +import collections.abc import enum import importlib.machinery import itertools @@ -174,7 +175,22 @@ def isgeneratorfunction(object): See help(isfunction) for attributes listing.""" return bool((isfunction(object) or ismethod(object)) and - object.__code__.co_flags & CO_GENERATOR) + object.__code__.co_flags & CO_GENERATOR and + not object.__code__.co_flags & CO_COROUTINE) + +def iscoroutinefunction(object): + """Return true if the object is a coroutine function. + + Coroutine functions are defined with "async def" syntax, + or generators decorated with "types.coroutine". + """ + return bool((isfunction(object) or ismethod(object)) and + object.__code__.co_flags & (CO_ITERABLE_COROUTINE | + CO_COROUTINE)) + +def isawaitable(object): + """Return true if the object can be used in "await" expression.""" + return isinstance(object, collections.abc.Awaitable) def isgenerator(object): """Return true if the object is a generator. @@ -191,7 +207,13 @@ def isgenerator(object): send resumes the generator and "sends" a value that becomes the result of the current yield-expression throw used to raise an exception inside the generator""" - return isinstance(object, types.GeneratorType) + return (isinstance(object, types.GeneratorType) and + not object.gi_code.co_flags & CO_COROUTINE) + +def iscoroutine(object): + """Return true if the object is a coroutine.""" + return (isinstance(object, types.GeneratorType) and + object.gi_code.co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE)) def istraceback(object): """Return true if the object is a traceback. |