当前位置: 首页>>代码示例>>Python>>正文


Python types.CoroutineType方法代码示例

本文整理汇总了Python中types.CoroutineType方法的典型用法代码示例。如果您正苦于以下问题:Python types.CoroutineType方法的具体用法?Python types.CoroutineType怎么用?Python types.CoroutineType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在types的用法示例。


在下文中一共展示了types.CoroutineType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run_async__await__

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def run_async__await__(coro):
    assert coro.__class__ is types.CoroutineType
    aw = coro.__await__()
    buffer = []
    result = None
    i = 0
    while True:
        try:
            if i % 2:
                buffer.append(next(aw))
            else:
                buffer.append(aw.send(None))
            i += 1
        except StopIteration as ex:
            result = ex.args[0] if ex.args else None
            break
    return buffer, result 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:19,代码来源:test_coroutines.py

示例2: test_func_1

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [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)) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:18,代码来源:test_coroutines.py

示例3: is_internal_attribute

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def is_internal_attribute(obj, attr):
    """Test if the attribute given is an internal python attribute.  For
    example this function returns `True` for the `func_code` attribute of
    python objects.  This is useful if the environment method
    :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.

    >>> from jinja2.sandbox import is_internal_attribute
    >>> is_internal_attribute(str, "mro")
    True
    >>> is_internal_attribute(str, "upper")
    False
    """
    if isinstance(obj, types.FunctionType):
        if attr in UNSAFE_FUNCTION_ATTRIBUTES:
            return True
    elif isinstance(obj, types.MethodType):
        if attr in UNSAFE_FUNCTION_ATTRIBUTES or \
           attr in UNSAFE_METHOD_ATTRIBUTES:
            return True
    elif isinstance(obj, type):
        if attr == 'mro':
            return True
    elif isinstance(obj, (types.CodeType, types.TracebackType, types.FrameType)):
        return True
    elif isinstance(obj, types.GeneratorType):
        if attr in UNSAFE_GENERATOR_ATTRIBUTES:
            return True
    elif hasattr(types, 'CoroutineType') and isinstance(obj, types.CoroutineType):
        if attr in UNSAFE_COROUTINE_ATTRIBUTES:
            return True
    elif hasattr(types, 'AsyncGeneratorType') and isinstance(obj, types.AsyncGeneratorType):
        if attr in UNSAFE_ASYNC_GENERATOR_ATTRIBUTES:
            return True
    return attr.startswith('__') 
开发者ID:remg427,项目名称:misp42splunk,代码行数:36,代码来源:sandbox.py

示例4: is_internal_attribute

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def is_internal_attribute(obj, attr):
    """Test if the attribute given is an internal python attribute.  For
    example this function returns `True` for the `func_code` attribute of
    python objects.  This is useful if the environment method
    :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden.

    >>> from jinja2.sandbox import is_internal_attribute
    >>> is_internal_attribute(str, "mro")
    True
    >>> is_internal_attribute(str, "upper")
    False
    """
    if isinstance(obj, types.FunctionType):
        if attr in UNSAFE_FUNCTION_ATTRIBUTES:
            return True
    elif isinstance(obj, types.MethodType):
        if attr in UNSAFE_FUNCTION_ATTRIBUTES or attr in UNSAFE_METHOD_ATTRIBUTES:
            return True
    elif isinstance(obj, type):
        if attr == "mro":
            return True
    elif isinstance(obj, (types.CodeType, types.TracebackType, types.FrameType)):
        return True
    elif isinstance(obj, types.GeneratorType):
        if attr in UNSAFE_GENERATOR_ATTRIBUTES:
            return True
    elif hasattr(types, "CoroutineType") and isinstance(obj, types.CoroutineType):
        if attr in UNSAFE_COROUTINE_ATTRIBUTES:
            return True
    elif hasattr(types, "AsyncGeneratorType") and isinstance(
        obj, types.AsyncGeneratorType
    ):
        if attr in UNSAFE_ASYNC_GENERATOR_ATTRIBUTES:
            return True
    return attr.startswith("__") 
开发者ID:pypa,项目名称:pipenv,代码行数:37,代码来源:sandbox.py

示例5: iscoroutine

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def iscoroutine(object):
    """Return true if the object is a coroutine."""
    return isinstance(object, types.CoroutineType) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:5,代码来源:inspect.py

示例6: isawaitable

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
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)) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:8,代码来源:inspect.py

示例7: run_async

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def run_async(coro):
    assert coro.__class__ in {types.GeneratorType, types.CoroutineType}

    buffer = []
    result = None
    while True:
        try:
            buffer.append(coro.send(None))
        except StopIteration as ex:
            result = ex.args[0] if ex.args else None
            break
    return buffer, result 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:14,代码来源:test_coroutines.py

示例8: test_await_14

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def test_await_14(self):
        class Wrapper:
            # Forces the interpreter to use CoroutineType.__await__
            def __init__(self, coro):
                assert coro.__class__ is types.CoroutineType
                self.coro = coro
            def __await__(self):
                return self.coro.__await__()

        class FutureLike:
            def __await__(self):
                return (yield)

        class Marker(Exception):
            pass

        async def coro1():
            try:
                return await FutureLike()
            except ZeroDivisionError:
                raise Marker
        async def coro2():
            return await Wrapper(coro1())

        c = coro2()
        c.send(None)
        with self.assertRaisesRegex(StopIteration, 'spam'):
            c.send('spam')

        c = coro2()
        c.send(None)
        with self.assertRaises(Marker):
            c.throw(ZeroDivisionError) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:35,代码来源:test_coroutines.py

示例9: isawaitable

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def isawaitable(object):
    """Return true if object can be passed to an ``await`` expression."""
    return (isinstance(object, types.CoroutineType) or
            isinstance(object, types.GeneratorType) and
                bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
            isinstance(object, collections.abc.Awaitable)) 
开发者ID:CedricGuillemet,项目名称:Imogen,代码行数:8,代码来源:inspect.py

示例10: test_async_call_generates_coro

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def test_async_call_generates_coro(self):
        method = self.client.my_method_async()
        self.assertIsInstance(method, types.CoroutineType) 
开发者ID:kanboard,项目名称:python-api-client,代码行数:5,代码来源:test_kanboard.py

示例11: run

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [as 别名]
def run(self, coro):
        """Schedule a coroutine."""
        assert isinstance(coro, types.CoroutineType)
        task = Task(self.clock, coro)
        return task 
开发者ID:lordmauve,项目名称:wasabi2d,代码行数:7,代码来源:clock.py

示例12: is_awaitable

# 需要导入模块: import types [as 别名]
# 或者: from types import CoroutineType [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__")
    ) 
开发者ID:graphql-python,项目名称:graphql-core,代码行数:17,代码来源:is_awaitable.py


注:本文中的types.CoroutineType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。