File: py35_pep492_interop.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (156 lines) | stat: -rw-r--r-- 3,878 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# cython: language_level=3, binding=True
# mode: run
# tag: pep492, asyncfor, await


def run_async(coro, ignore_type=False):
    if not ignore_type:
        #assert coro.__class__ is types.GeneratorType
        assert coro.__class__.__name__ in ('coroutine', 'GeneratorWrapper'), coro.__class__.__name__

    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


def run_async__await__(coro):
    assert coro.__class__.__name__ in ('coroutine', 'GeneratorWrapper'), coro.__class__.__name__
    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


async def await_pyobject(awaitable):
    """
    >>> async def simple():
    ...     return 10

    >>> buffer, result = run_async(await_pyobject(simple()))
    >>> result
    10

    >>> async def awaiting(awaitable):
    ...     return await awaitable

    >>> buffer, result = run_async(await_pyobject(awaiting(simple())))
    >>> result
    10
    """
    return await awaitable


def await_cyobject():
    """
    >>> async def run_await(awaitable):
    ...     return await awaitable

    >>> simple, awaiting = await_cyobject()

    >>> buffer, result = run_async(run_await(simple()))
    >>> result
    10

    >>> buffer, result = run_async(run_await(awaiting(simple())))
    >>> result
    10

    >>> buffer, result = run_async(run_await(awaiting(awaiting(simple()))))
    >>> result
    10

    >>> buffer, result = run_async(run_await(awaiting(run_await(awaiting(simple())))))
    >>> result
    10
    """

    async def simple():
        return 10

    async def awaiting(awaitable):
        return await awaitable

    return simple, awaiting


cimport cython

def yield_from_cyobject():
    """
    >>> async def py_simple_nonit():
    ...     return 10

    >>> async def run_await(awaitable):
    ...     return await awaitable

    >>> def run_yield_from(it):
    ...     return (yield from it)

    >>> simple_nonit, simple_it, awaiting, yield_from = yield_from_cyobject()

    >>> buffer, result = run_async(run_await(simple_it()))
    >>> result
    10
    >>> buffer, result = run_async(run_await(awaiting(simple_it())))
    >>> result
    10
    >>> buffer, result = run_async(awaiting(run_await(simple_it())), ignore_type=True)
    >>> result
    10
    >>> buffer, result = run_async(run_await(py_simple_nonit()))
    >>> result
    10

    >>> buffer, result = run_async(run_yield_from(awaiting(run_await(simple_it()))), ignore_type=True)
    >>> result
    10

    >>> buffer, result = run_async(run_yield_from(simple_it()), ignore_type=True)
    >>> result
    10
    >>> buffer, result = run_async(yield_from(simple_it()), ignore_type=True)
    >>> result
    10

    >>> next(run_yield_from(simple_nonit()))  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    TypeError: ...
    >>> next(run_yield_from(py_simple_nonit()))  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    TypeError: ...
    >>> next(yield_from(py_simple_nonit()))
    Traceback (most recent call last):
    TypeError: 'coroutine' object is not iterable
    """
    async def simple_nonit():
        return 10

    @cython.iterable_coroutine
    async def simple_it():
        return 10

    @cython.iterable_coroutine
    async def awaiting(awaitable):
        return await awaitable

    def yield_from(it):
        return (yield from it)

    return simple_nonit, simple_it, awaiting, yield_from