本文整理匯總了Python中inspect.CO_COROUTINE屬性的典型用法代碼示例。如果您正苦於以下問題:Python inspect.CO_COROUTINE屬性的具體用法?Python inspect.CO_COROUTINE怎麽用?Python inspect.CO_COROUTINE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類inspect
的用法示例。
在下文中一共展示了inspect.CO_COROUTINE屬性的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_func_1
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_COROUTINE [as 別名]
def test_func_1(self):
async def foo():
return 10
f = foo()
self.assertIsInstance(f, types.CoroutineType)
self.assertTrue(bool(foo.__code__.co_flags & inspect.CO_COROUTINE))
self.assertFalse(bool(foo.__code__.co_flags & inspect.CO_GENERATOR))
self.assertTrue(bool(f.cr_code.co_flags & inspect.CO_COROUTINE))
self.assertFalse(bool(f.cr_code.co_flags & inspect.CO_GENERATOR))
self.assertEqual(run_async(f), ([], 10))
self.assertEqual(run_async__await__(foo()), ([], 10))
def bar(): pass
self.assertFalse(bool(bar.__code__.co_flags & inspect.CO_COROUTINE))
示例2: test_async_await
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_COROUTINE [as 別名]
def test_async_await(self):
async def test():
def sum():
pass
if 1:
await someobj()
self.assertEqual(test.__name__, 'test')
self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
def decorator(func):
setattr(func, '_marked', True)
return func
@decorator
async def test2():
return 22
self.assertTrue(test2._marked)
self.assertEqual(test2.__name__, 'test2')
self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
示例3: is_coroutine_function
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_COROUTINE [as 別名]
def is_coroutine_function(func: Any) -> bool:
# Python < 3.8 does not correctly determine partially wrapped
# coroutine functions are coroutine functions, hence the need for
# this to exist. Code taken from CPython.
if sys.version_info >= (3, 8):
return asyncio.iscoroutinefunction(func)
else:
# Note that there is something special about the CoroutineMock
# such that it isn't determined as a coroutine function
# without an explicit check.
try:
from asynctest.mock import CoroutineMock
if isinstance(func, CoroutineMock):
return True
except ImportError:
# Not testing, no asynctest to import
pass
while inspect.ismethod(func):
func = func.__func__
while isinstance(func, functools.partial):
func = func.func
if not inspect.isfunction(func):
return False
result = bool(func.__code__.co_flags & inspect.CO_COROUTINE)
return result or getattr(func, "_is_coroutine", None) is asyncio.coroutines._is_coroutine
示例4: _from_coroutine
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_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
示例5: test_genfunc
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_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)
示例6: iscoroutinefunction
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_COROUTINE [as 別名]
def iscoroutinefunction(obj):
"""Return true if the object is a coroutine function.
Coroutine functions are defined with "async def" syntax.
"""
return (
_has_code_flag(obj, CO_COROUTINE) or
getattr(obj, '_is_coroutine', None) is _is_coroutine
)
示例7: __init__
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import CO_COROUTINE [as 別名]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# iscoroutinefunction() checks _is_coroutine property to say if an
# object is a coroutine. Without this check it looks to see if it is a
# function/method, which in this case it is not (since it is an
# AsyncMock).
# It is set through __dict__ because when spec_set is True, this
# attribute is likely undefined.
self.__dict__['_is_coroutine'] = asyncio.coroutines._is_coroutine
self.__dict__['_mock_await_count'] = 0
self.__dict__['_mock_await_args'] = None
self.__dict__['_mock_await_args_list'] = _CallList()
code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = inspect.CO_COROUTINE
self.__dict__['__code__'] = code_mock