本文整理匯總了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__")
)