本文整理汇总了Python中inspect.CO_ITERABLE_COROUTINE属性的典型用法代码示例。如果您正苦于以下问题:Python inspect.CO_ITERABLE_COROUTINE属性的具体用法?Python inspect.CO_ITERABLE_COROUTINE怎么用?Python inspect.CO_ITERABLE_COROUTINE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类inspect
的用法示例。
在下文中一共展示了inspect.CO_ITERABLE_COROUTINE属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _from_coroutine
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_ITERABLE_COROUTINE [as 别名]
def _from_coroutine() -> bool:
"""Check if called from the coroutine function.
Determine whether the current function is called from a
coroutine function (native coroutine, generator-based coroutine,
or asynchronous generator function).
NOTE: That it's only recommended to use for debugging, not as
part of your production code's functionality.
"""
try:
frame = inspect.currentframe()
if frame is None:
return False
coroutine_function_flags = (
inspect.CO_COROUTINE # pylint: disable=no-member
| inspect.CO_ASYNC_GENERATOR # pylint: disable=no-member
| inspect.CO_ITERABLE_COROUTINE # pylint: disable=no-member
)
if (
frame is not None
and frame.f_back is not None
and frame.f_back.f_back is not None
):
return bool(
frame.f_back.f_back.f_code.co_flags & coroutine_function_flags
)
return False
finally:
del frame
示例2: test_genfunc
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_ITERABLE_COROUTINE [as 别名]
def test_genfunc(self):
def gen(): yield
self.assertIs(types.coroutine(gen), gen)
self.assertIs(types.coroutine(types.coroutine(gen)), gen)
self.assertTrue(gen.__code__.co_flags & inspect.CO_ITERABLE_COROUTINE)
self.assertFalse(gen.__code__.co_flags & inspect.CO_COROUTINE)
g = gen()
self.assertTrue(g.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE)
self.assertFalse(g.gi_code.co_flags & inspect.CO_COROUTINE)
self.assertIs(types.coroutine(gen), gen)
示例3: __init__
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_ITERABLE_COROUTINE [as 别名]
def __init__(self, iterator):
self.iterator = iterator
code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = inspect.CO_ITERABLE_COROUTINE
self.__dict__['__code__'] = code_mock
示例4: is_awaitable
# 需要导入模块: import inspect [as 别名]
# 或者: from inspect import CO_ITERABLE_COROUTINE [as 别名]
def is_awaitable(value: Any) -> bool:
"""Return true if object can be passed to an ``await`` expression.
Instead of testing if the object is an instance of abc.Awaitable, it checks
the existence of an `__await__` attribute. This is much faster.
"""
return (
# check for coroutine objects
isinstance(value, CoroutineType)
# check for old-style generator based coroutine objects
or isinstance(value, GeneratorType)
and bool(value.gi_code.co_flags & CO_ITERABLE_COROUTINE)
# check for other awaitables (e.g. futures)
or hasattr(value, "__await__")
)