forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_matrix.py
120 lines (100 loc) · 2.89 KB
/
async_matrix.py
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
#!/usr/bin/env python3
"""Test various combinations of generators/coroutines.
This was used to cross-check the errors in the test case
testFullCoroutineMatrix in test-data/unit/check-async-await.test.
"""
import sys
from types import coroutine
from typing import Any, Awaitable, Generator, Iterator
# The various things you might try to use in `await` or `yield from`.
def plain_generator() -> Generator[str, None, int]:
yield 'a'
return 1
async def plain_coroutine() -> int:
return 1
@coroutine
def decorated_generator() -> Generator[str, None, int]:
yield 'a'
return 1
@coroutine
async def decorated_coroutine() -> int:
return 1
class It(Iterator[str]):
stop = False
def __iter__(self) -> 'It':
return self
def __next__(self) -> str:
if self.stop:
raise StopIteration('end')
else:
self.stop = True
return 'a'
def other_iterator() -> It:
return It()
class Aw(Awaitable[int]):
def __await__(self) -> Generator[str, Any, int]:
yield 'a'
return 1
def other_coroutine() -> Aw:
return Aw()
# The various contexts in which `await` or `yield from` might occur.
def plain_host_generator(func) -> Generator[str, None, None]:
yield 'a'
x = 0
f = func()
try:
x = yield from f
finally:
try:
f.close()
except AttributeError:
pass
async def plain_host_coroutine(func) -> None:
x = 0
x = await func()
@coroutine
def decorated_host_generator(func) -> Generator[str, None, None]:
yield 'a'
x = 0
f = func()
try:
x = yield from f
finally:
try:
f.close()
except AttributeError:
pass
@coroutine
async def decorated_host_coroutine(func) -> None:
x = 0
x = await func()
# Main driver.
def main():
verbose = ('-v' in sys.argv)
for host in [plain_host_generator, plain_host_coroutine,
decorated_host_generator, decorated_host_coroutine]:
print()
print("==== Host:", host.__name__)
for func in [plain_generator, plain_coroutine,
decorated_generator, decorated_coroutine,
other_iterator, other_coroutine]:
print(" ---- Func:", func.__name__)
try:
f = host(func)
for i in range(10):
try:
x = f.send(None)
if verbose:
print(" yield:", x)
except StopIteration as e:
if verbose:
print(" stop:", e.value)
break
else:
if verbose:
print(" ???? still going")
except Exception as e:
print(" error:", repr(e))
# Run main().
if __name__ == '__main__':
main()