1 | n/a | import contextlib |
---|
2 | n/a | import copy |
---|
3 | n/a | import inspect |
---|
4 | n/a | import pickle |
---|
5 | n/a | import sys |
---|
6 | n/a | import types |
---|
7 | n/a | import unittest |
---|
8 | n/a | import warnings |
---|
9 | n/a | from test import support |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | class AsyncYieldFrom: |
---|
13 | n/a | def __init__(self, obj): |
---|
14 | n/a | self.obj = obj |
---|
15 | n/a | |
---|
16 | n/a | def __await__(self): |
---|
17 | n/a | yield from self.obj |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | class AsyncYield: |
---|
21 | n/a | def __init__(self, value): |
---|
22 | n/a | self.value = value |
---|
23 | n/a | |
---|
24 | n/a | def __await__(self): |
---|
25 | n/a | yield self.value |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | def run_async(coro): |
---|
29 | n/a | assert coro.__class__ in {types.GeneratorType, types.CoroutineType} |
---|
30 | n/a | |
---|
31 | n/a | buffer = [] |
---|
32 | n/a | result = None |
---|
33 | n/a | while True: |
---|
34 | n/a | try: |
---|
35 | n/a | buffer.append(coro.send(None)) |
---|
36 | n/a | except StopIteration as ex: |
---|
37 | n/a | result = ex.args[0] if ex.args else None |
---|
38 | n/a | break |
---|
39 | n/a | return buffer, result |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | def run_async__await__(coro): |
---|
43 | n/a | assert coro.__class__ is types.CoroutineType |
---|
44 | n/a | aw = coro.__await__() |
---|
45 | n/a | buffer = [] |
---|
46 | n/a | result = None |
---|
47 | n/a | i = 0 |
---|
48 | n/a | while True: |
---|
49 | n/a | try: |
---|
50 | n/a | if i % 2: |
---|
51 | n/a | buffer.append(next(aw)) |
---|
52 | n/a | else: |
---|
53 | n/a | buffer.append(aw.send(None)) |
---|
54 | n/a | i += 1 |
---|
55 | n/a | except StopIteration as ex: |
---|
56 | n/a | result = ex.args[0] if ex.args else None |
---|
57 | n/a | break |
---|
58 | n/a | return buffer, result |
---|
59 | n/a | |
---|
60 | n/a | |
---|
61 | n/a | @contextlib.contextmanager |
---|
62 | n/a | def silence_coro_gc(): |
---|
63 | n/a | with warnings.catch_warnings(): |
---|
64 | n/a | warnings.simplefilter("ignore") |
---|
65 | n/a | yield |
---|
66 | n/a | support.gc_collect() |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | class AsyncBadSyntaxTest(unittest.TestCase): |
---|
70 | n/a | |
---|
71 | n/a | def test_badsyntax_1(self): |
---|
72 | n/a | samples = [ |
---|
73 | n/a | """def foo(): |
---|
74 | n/a | await something() |
---|
75 | n/a | """, |
---|
76 | n/a | |
---|
77 | n/a | """await something()""", |
---|
78 | n/a | |
---|
79 | n/a | """async def foo(): |
---|
80 | n/a | yield from [] |
---|
81 | n/a | """, |
---|
82 | n/a | |
---|
83 | n/a | """async def foo(): |
---|
84 | n/a | await await fut |
---|
85 | n/a | """, |
---|
86 | n/a | |
---|
87 | n/a | """async def foo(a=await something()): |
---|
88 | n/a | pass |
---|
89 | n/a | """, |
---|
90 | n/a | |
---|
91 | n/a | """async def foo(a:await something()): |
---|
92 | n/a | pass |
---|
93 | n/a | """, |
---|
94 | n/a | |
---|
95 | n/a | """async def foo(): |
---|
96 | n/a | def bar(): |
---|
97 | n/a | [i async for i in els] |
---|
98 | n/a | """, |
---|
99 | n/a | |
---|
100 | n/a | """async def foo(): |
---|
101 | n/a | def bar(): |
---|
102 | n/a | [await i for i in els] |
---|
103 | n/a | """, |
---|
104 | n/a | |
---|
105 | n/a | """async def foo(): |
---|
106 | n/a | def bar(): |
---|
107 | n/a | [i for i in els |
---|
108 | n/a | async for b in els] |
---|
109 | n/a | """, |
---|
110 | n/a | |
---|
111 | n/a | """async def foo(): |
---|
112 | n/a | def bar(): |
---|
113 | n/a | [i for i in els |
---|
114 | n/a | for c in b |
---|
115 | n/a | async for b in els] |
---|
116 | n/a | """, |
---|
117 | n/a | |
---|
118 | n/a | """async def foo(): |
---|
119 | n/a | def bar(): |
---|
120 | n/a | [i for i in els |
---|
121 | n/a | async for b in els |
---|
122 | n/a | for c in b] |
---|
123 | n/a | """, |
---|
124 | n/a | |
---|
125 | n/a | """async def foo(): |
---|
126 | n/a | def bar(): |
---|
127 | n/a | [i for i in els |
---|
128 | n/a | for b in await els] |
---|
129 | n/a | """, |
---|
130 | n/a | |
---|
131 | n/a | """async def foo(): |
---|
132 | n/a | def bar(): |
---|
133 | n/a | [i for i in els |
---|
134 | n/a | for b in els |
---|
135 | n/a | if await b] |
---|
136 | n/a | """, |
---|
137 | n/a | |
---|
138 | n/a | """async def foo(): |
---|
139 | n/a | def bar(): |
---|
140 | n/a | [i for i in await els] |
---|
141 | n/a | """, |
---|
142 | n/a | |
---|
143 | n/a | """async def foo(): |
---|
144 | n/a | def bar(): |
---|
145 | n/a | [i for i in els if await i] |
---|
146 | n/a | """, |
---|
147 | n/a | |
---|
148 | n/a | """def bar(): |
---|
149 | n/a | [i async for i in els] |
---|
150 | n/a | """, |
---|
151 | n/a | |
---|
152 | n/a | """def bar(): |
---|
153 | n/a | [await i for i in els] |
---|
154 | n/a | """, |
---|
155 | n/a | |
---|
156 | n/a | """def bar(): |
---|
157 | n/a | [i for i in els |
---|
158 | n/a | async for b in els] |
---|
159 | n/a | """, |
---|
160 | n/a | |
---|
161 | n/a | """def bar(): |
---|
162 | n/a | [i for i in els |
---|
163 | n/a | for c in b |
---|
164 | n/a | async for b in els] |
---|
165 | n/a | """, |
---|
166 | n/a | |
---|
167 | n/a | """def bar(): |
---|
168 | n/a | [i for i in els |
---|
169 | n/a | async for b in els |
---|
170 | n/a | for c in b] |
---|
171 | n/a | """, |
---|
172 | n/a | |
---|
173 | n/a | """def bar(): |
---|
174 | n/a | [i for i in els |
---|
175 | n/a | for b in await els] |
---|
176 | n/a | """, |
---|
177 | n/a | |
---|
178 | n/a | """def bar(): |
---|
179 | n/a | [i for i in els |
---|
180 | n/a | for b in els |
---|
181 | n/a | if await b] |
---|
182 | n/a | """, |
---|
183 | n/a | |
---|
184 | n/a | """def bar(): |
---|
185 | n/a | [i for i in await els] |
---|
186 | n/a | """, |
---|
187 | n/a | |
---|
188 | n/a | """def bar(): |
---|
189 | n/a | [i for i in els if await i] |
---|
190 | n/a | """, |
---|
191 | n/a | |
---|
192 | n/a | """async def foo(): |
---|
193 | n/a | await |
---|
194 | n/a | """, |
---|
195 | n/a | |
---|
196 | n/a | """async def foo(): |
---|
197 | n/a | def bar(): pass |
---|
198 | n/a | await = 1 |
---|
199 | n/a | """, |
---|
200 | n/a | |
---|
201 | n/a | """async def foo(): |
---|
202 | n/a | |
---|
203 | n/a | def bar(): pass |
---|
204 | n/a | await = 1 |
---|
205 | n/a | """, |
---|
206 | n/a | |
---|
207 | n/a | """async def foo(): |
---|
208 | n/a | def bar(): pass |
---|
209 | n/a | if 1: |
---|
210 | n/a | await = 1 |
---|
211 | n/a | """, |
---|
212 | n/a | |
---|
213 | n/a | """def foo(): |
---|
214 | n/a | async def bar(): pass |
---|
215 | n/a | if 1: |
---|
216 | n/a | await a |
---|
217 | n/a | """, |
---|
218 | n/a | |
---|
219 | n/a | """def foo(): |
---|
220 | n/a | async def bar(): pass |
---|
221 | n/a | await a |
---|
222 | n/a | """, |
---|
223 | n/a | |
---|
224 | n/a | """def foo(): |
---|
225 | n/a | def baz(): pass |
---|
226 | n/a | async def bar(): pass |
---|
227 | n/a | await a |
---|
228 | n/a | """, |
---|
229 | n/a | |
---|
230 | n/a | """def foo(): |
---|
231 | n/a | def baz(): pass |
---|
232 | n/a | # 456 |
---|
233 | n/a | async def bar(): pass |
---|
234 | n/a | # 123 |
---|
235 | n/a | await a |
---|
236 | n/a | """, |
---|
237 | n/a | |
---|
238 | n/a | """async def foo(): |
---|
239 | n/a | def baz(): pass |
---|
240 | n/a | # 456 |
---|
241 | n/a | async def bar(): pass |
---|
242 | n/a | # 123 |
---|
243 | n/a | await = 2 |
---|
244 | n/a | """, |
---|
245 | n/a | |
---|
246 | n/a | """def foo(): |
---|
247 | n/a | |
---|
248 | n/a | def baz(): pass |
---|
249 | n/a | |
---|
250 | n/a | async def bar(): pass |
---|
251 | n/a | |
---|
252 | n/a | await a |
---|
253 | n/a | """, |
---|
254 | n/a | |
---|
255 | n/a | """async def foo(): |
---|
256 | n/a | |
---|
257 | n/a | def baz(): pass |
---|
258 | n/a | |
---|
259 | n/a | async def bar(): pass |
---|
260 | n/a | |
---|
261 | n/a | await = 2 |
---|
262 | n/a | """, |
---|
263 | n/a | |
---|
264 | n/a | """async def foo(): |
---|
265 | n/a | def async(): pass |
---|
266 | n/a | """, |
---|
267 | n/a | |
---|
268 | n/a | """async def foo(): |
---|
269 | n/a | def await(): pass |
---|
270 | n/a | """, |
---|
271 | n/a | |
---|
272 | n/a | """async def foo(): |
---|
273 | n/a | def bar(): |
---|
274 | n/a | await |
---|
275 | n/a | """, |
---|
276 | n/a | |
---|
277 | n/a | """async def foo(): |
---|
278 | n/a | return lambda async: await |
---|
279 | n/a | """, |
---|
280 | n/a | |
---|
281 | n/a | """async def foo(): |
---|
282 | n/a | return lambda a: await |
---|
283 | n/a | """, |
---|
284 | n/a | |
---|
285 | n/a | """await a()""", |
---|
286 | n/a | |
---|
287 | n/a | """async def foo(a=await b): |
---|
288 | n/a | pass |
---|
289 | n/a | """, |
---|
290 | n/a | |
---|
291 | n/a | """async def foo(a:await b): |
---|
292 | n/a | pass |
---|
293 | n/a | """, |
---|
294 | n/a | |
---|
295 | n/a | """def baz(): |
---|
296 | n/a | async def foo(a=await b): |
---|
297 | n/a | pass |
---|
298 | n/a | """, |
---|
299 | n/a | |
---|
300 | n/a | """async def foo(async): |
---|
301 | n/a | pass |
---|
302 | n/a | """, |
---|
303 | n/a | |
---|
304 | n/a | """async def foo(): |
---|
305 | n/a | def bar(): |
---|
306 | n/a | def baz(): |
---|
307 | n/a | async = 1 |
---|
308 | n/a | """, |
---|
309 | n/a | |
---|
310 | n/a | """async def foo(): |
---|
311 | n/a | def bar(): |
---|
312 | n/a | def baz(): |
---|
313 | n/a | pass |
---|
314 | n/a | async = 1 |
---|
315 | n/a | """, |
---|
316 | n/a | |
---|
317 | n/a | """def foo(): |
---|
318 | n/a | async def bar(): |
---|
319 | n/a | |
---|
320 | n/a | async def baz(): |
---|
321 | n/a | pass |
---|
322 | n/a | |
---|
323 | n/a | def baz(): |
---|
324 | n/a | 42 |
---|
325 | n/a | |
---|
326 | n/a | async = 1 |
---|
327 | n/a | """, |
---|
328 | n/a | |
---|
329 | n/a | """async def foo(): |
---|
330 | n/a | def bar(): |
---|
331 | n/a | def baz(): |
---|
332 | n/a | pass\nawait foo() |
---|
333 | n/a | """, |
---|
334 | n/a | |
---|
335 | n/a | """def foo(): |
---|
336 | n/a | def bar(): |
---|
337 | n/a | async def baz(): |
---|
338 | n/a | pass\nawait foo() |
---|
339 | n/a | """, |
---|
340 | n/a | |
---|
341 | n/a | """async def foo(await): |
---|
342 | n/a | pass |
---|
343 | n/a | """, |
---|
344 | n/a | |
---|
345 | n/a | """def foo(): |
---|
346 | n/a | |
---|
347 | n/a | async def bar(): pass |
---|
348 | n/a | |
---|
349 | n/a | await a |
---|
350 | n/a | """, |
---|
351 | n/a | |
---|
352 | n/a | """def foo(): |
---|
353 | n/a | async def bar(): |
---|
354 | n/a | pass\nawait a |
---|
355 | n/a | """] |
---|
356 | n/a | |
---|
357 | n/a | for code in samples: |
---|
358 | n/a | with self.subTest(code=code), self.assertRaises(SyntaxError): |
---|
359 | n/a | compile(code, "<test>", "exec") |
---|
360 | n/a | |
---|
361 | n/a | def test_badsyntax_2(self): |
---|
362 | n/a | samples = [ |
---|
363 | n/a | """def foo(): |
---|
364 | n/a | await = 1 |
---|
365 | n/a | """, |
---|
366 | n/a | |
---|
367 | n/a | """class Bar: |
---|
368 | n/a | def async(): pass |
---|
369 | n/a | """, |
---|
370 | n/a | |
---|
371 | n/a | """class Bar: |
---|
372 | n/a | async = 1 |
---|
373 | n/a | """, |
---|
374 | n/a | |
---|
375 | n/a | """class async: |
---|
376 | n/a | pass |
---|
377 | n/a | """, |
---|
378 | n/a | |
---|
379 | n/a | """class await: |
---|
380 | n/a | pass |
---|
381 | n/a | """, |
---|
382 | n/a | |
---|
383 | n/a | """import math as await""", |
---|
384 | n/a | |
---|
385 | n/a | """def async(): |
---|
386 | n/a | pass""", |
---|
387 | n/a | |
---|
388 | n/a | """def foo(*, await=1): |
---|
389 | n/a | pass""" |
---|
390 | n/a | |
---|
391 | n/a | """async = 1""", |
---|
392 | n/a | |
---|
393 | n/a | """print(await=1)""" |
---|
394 | n/a | ] |
---|
395 | n/a | |
---|
396 | n/a | for code in samples: |
---|
397 | n/a | with self.subTest(code=code), self.assertWarnsRegex( |
---|
398 | n/a | DeprecationWarning, |
---|
399 | n/a | "'await' will become reserved keywords"): |
---|
400 | n/a | compile(code, "<test>", "exec") |
---|
401 | n/a | |
---|
402 | n/a | def test_badsyntax_3(self): |
---|
403 | n/a | with self.assertRaises(DeprecationWarning): |
---|
404 | n/a | with warnings.catch_warnings(): |
---|
405 | n/a | warnings.simplefilter("error") |
---|
406 | n/a | compile("async = 1", "<test>", "exec") |
---|
407 | n/a | |
---|
408 | n/a | def test_goodsyntax_1(self): |
---|
409 | n/a | # Tests for issue 24619 |
---|
410 | n/a | |
---|
411 | n/a | samples = [ |
---|
412 | n/a | '''def foo(await): |
---|
413 | n/a | async def foo(): pass |
---|
414 | n/a | async def foo(): |
---|
415 | n/a | pass |
---|
416 | n/a | return await + 1 |
---|
417 | n/a | ''', |
---|
418 | n/a | |
---|
419 | n/a | '''def foo(await): |
---|
420 | n/a | async def foo(): pass |
---|
421 | n/a | async def foo(): pass |
---|
422 | n/a | return await + 1 |
---|
423 | n/a | ''', |
---|
424 | n/a | |
---|
425 | n/a | '''def foo(await): |
---|
426 | n/a | |
---|
427 | n/a | async def foo(): pass |
---|
428 | n/a | |
---|
429 | n/a | async def foo(): pass |
---|
430 | n/a | |
---|
431 | n/a | return await + 1 |
---|
432 | n/a | ''', |
---|
433 | n/a | |
---|
434 | n/a | '''def foo(await): |
---|
435 | n/a | """spam""" |
---|
436 | n/a | async def foo(): \ |
---|
437 | n/a | pass |
---|
438 | n/a | # 123 |
---|
439 | n/a | async def foo(): pass |
---|
440 | n/a | # 456 |
---|
441 | n/a | return await + 1 |
---|
442 | n/a | ''', |
---|
443 | n/a | |
---|
444 | n/a | '''def foo(await): |
---|
445 | n/a | def foo(): pass |
---|
446 | n/a | def foo(): pass |
---|
447 | n/a | async def bar(): return await_ |
---|
448 | n/a | await_ = await |
---|
449 | n/a | try: |
---|
450 | n/a | bar().send(None) |
---|
451 | n/a | except StopIteration as ex: |
---|
452 | n/a | return ex.args[0] + 1 |
---|
453 | n/a | ''' |
---|
454 | n/a | ] |
---|
455 | n/a | |
---|
456 | n/a | for code in samples: |
---|
457 | n/a | with self.subTest(code=code): |
---|
458 | n/a | loc = {} |
---|
459 | n/a | |
---|
460 | n/a | with warnings.catch_warnings(): |
---|
461 | n/a | warnings.simplefilter("ignore") |
---|
462 | n/a | exec(code, loc, loc) |
---|
463 | n/a | |
---|
464 | n/a | self.assertEqual(loc['foo'](10), 11) |
---|
465 | n/a | |
---|
466 | n/a | |
---|
467 | n/a | class TokenizerRegrTest(unittest.TestCase): |
---|
468 | n/a | |
---|
469 | n/a | def test_oneline_defs(self): |
---|
470 | n/a | buf = [] |
---|
471 | n/a | for i in range(500): |
---|
472 | n/a | buf.append('def i{i}(): return {i}'.format(i=i)) |
---|
473 | n/a | buf = '\n'.join(buf) |
---|
474 | n/a | |
---|
475 | n/a | # Test that 500 consequent, one-line defs is OK |
---|
476 | n/a | ns = {} |
---|
477 | n/a | exec(buf, ns, ns) |
---|
478 | n/a | self.assertEqual(ns['i499'](), 499) |
---|
479 | n/a | |
---|
480 | n/a | # Test that 500 consequent, one-line defs *and* |
---|
481 | n/a | # one 'async def' following them is OK |
---|
482 | n/a | buf += '\nasync def foo():\n return' |
---|
483 | n/a | ns = {} |
---|
484 | n/a | exec(buf, ns, ns) |
---|
485 | n/a | self.assertEqual(ns['i499'](), 499) |
---|
486 | n/a | self.assertTrue(inspect.iscoroutinefunction(ns['foo'])) |
---|
487 | n/a | |
---|
488 | n/a | |
---|
489 | n/a | class CoroutineTest(unittest.TestCase): |
---|
490 | n/a | |
---|
491 | n/a | def test_gen_1(self): |
---|
492 | n/a | def gen(): yield |
---|
493 | n/a | self.assertFalse(hasattr(gen, '__await__')) |
---|
494 | n/a | |
---|
495 | n/a | def test_func_1(self): |
---|
496 | n/a | async def foo(): |
---|
497 | n/a | return 10 |
---|
498 | n/a | |
---|
499 | n/a | f = foo() |
---|
500 | n/a | self.assertIsInstance(f, types.CoroutineType) |
---|
501 | n/a | self.assertTrue(bool(foo.__code__.co_flags & inspect.CO_COROUTINE)) |
---|
502 | n/a | self.assertFalse(bool(foo.__code__.co_flags & inspect.CO_GENERATOR)) |
---|
503 | n/a | self.assertTrue(bool(f.cr_code.co_flags & inspect.CO_COROUTINE)) |
---|
504 | n/a | self.assertFalse(bool(f.cr_code.co_flags & inspect.CO_GENERATOR)) |
---|
505 | n/a | self.assertEqual(run_async(f), ([], 10)) |
---|
506 | n/a | |
---|
507 | n/a | self.assertEqual(run_async__await__(foo()), ([], 10)) |
---|
508 | n/a | |
---|
509 | n/a | def bar(): pass |
---|
510 | n/a | self.assertFalse(bool(bar.__code__.co_flags & inspect.CO_COROUTINE)) |
---|
511 | n/a | |
---|
512 | n/a | def test_func_2(self): |
---|
513 | n/a | async def foo(): |
---|
514 | n/a | raise StopIteration |
---|
515 | n/a | |
---|
516 | n/a | with self.assertRaisesRegex( |
---|
517 | n/a | RuntimeError, "coroutine raised StopIteration"): |
---|
518 | n/a | |
---|
519 | n/a | run_async(foo()) |
---|
520 | n/a | |
---|
521 | n/a | def test_func_3(self): |
---|
522 | n/a | async def foo(): |
---|
523 | n/a | raise StopIteration |
---|
524 | n/a | |
---|
525 | n/a | with silence_coro_gc(): |
---|
526 | n/a | self.assertRegex(repr(foo()), '^<coroutine object.* at 0x.*>$') |
---|
527 | n/a | |
---|
528 | n/a | def test_func_4(self): |
---|
529 | n/a | async def foo(): |
---|
530 | n/a | raise StopIteration |
---|
531 | n/a | |
---|
532 | n/a | check = lambda: self.assertRaisesRegex( |
---|
533 | n/a | TypeError, "'coroutine' object is not iterable") |
---|
534 | n/a | |
---|
535 | n/a | with check(): |
---|
536 | n/a | list(foo()) |
---|
537 | n/a | |
---|
538 | n/a | with check(): |
---|
539 | n/a | tuple(foo()) |
---|
540 | n/a | |
---|
541 | n/a | with check(): |
---|
542 | n/a | sum(foo()) |
---|
543 | n/a | |
---|
544 | n/a | with check(): |
---|
545 | n/a | iter(foo()) |
---|
546 | n/a | |
---|
547 | n/a | with silence_coro_gc(), check(): |
---|
548 | n/a | for i in foo(): |
---|
549 | n/a | pass |
---|
550 | n/a | |
---|
551 | n/a | with silence_coro_gc(), check(): |
---|
552 | n/a | [i for i in foo()] |
---|
553 | n/a | |
---|
554 | n/a | def test_func_5(self): |
---|
555 | n/a | @types.coroutine |
---|
556 | n/a | def bar(): |
---|
557 | n/a | yield 1 |
---|
558 | n/a | |
---|
559 | n/a | async def foo(): |
---|
560 | n/a | await bar() |
---|
561 | n/a | |
---|
562 | n/a | check = lambda: self.assertRaisesRegex( |
---|
563 | n/a | TypeError, "'coroutine' object is not iterable") |
---|
564 | n/a | |
---|
565 | n/a | with check(): |
---|
566 | n/a | for el in foo(): pass |
---|
567 | n/a | |
---|
568 | n/a | # the following should pass without an error |
---|
569 | n/a | for el in bar(): |
---|
570 | n/a | self.assertEqual(el, 1) |
---|
571 | n/a | self.assertEqual([el for el in bar()], [1]) |
---|
572 | n/a | self.assertEqual(tuple(bar()), (1,)) |
---|
573 | n/a | self.assertEqual(next(iter(bar())), 1) |
---|
574 | n/a | |
---|
575 | n/a | def test_func_6(self): |
---|
576 | n/a | @types.coroutine |
---|
577 | n/a | def bar(): |
---|
578 | n/a | yield 1 |
---|
579 | n/a | yield 2 |
---|
580 | n/a | |
---|
581 | n/a | async def foo(): |
---|
582 | n/a | await bar() |
---|
583 | n/a | |
---|
584 | n/a | f = foo() |
---|
585 | n/a | self.assertEqual(f.send(None), 1) |
---|
586 | n/a | self.assertEqual(f.send(None), 2) |
---|
587 | n/a | with self.assertRaises(StopIteration): |
---|
588 | n/a | f.send(None) |
---|
589 | n/a | |
---|
590 | n/a | def test_func_7(self): |
---|
591 | n/a | async def bar(): |
---|
592 | n/a | return 10 |
---|
593 | n/a | |
---|
594 | n/a | def foo(): |
---|
595 | n/a | yield from bar() |
---|
596 | n/a | |
---|
597 | n/a | with silence_coro_gc(), self.assertRaisesRegex( |
---|
598 | n/a | TypeError, |
---|
599 | n/a | "cannot 'yield from' a coroutine object in a non-coroutine generator"): |
---|
600 | n/a | |
---|
601 | n/a | list(foo()) |
---|
602 | n/a | |
---|
603 | n/a | def test_func_8(self): |
---|
604 | n/a | @types.coroutine |
---|
605 | n/a | def bar(): |
---|
606 | n/a | return (yield from foo()) |
---|
607 | n/a | |
---|
608 | n/a | async def foo(): |
---|
609 | n/a | return 'spam' |
---|
610 | n/a | |
---|
611 | n/a | self.assertEqual(run_async(bar()), ([], 'spam') ) |
---|
612 | n/a | |
---|
613 | n/a | def test_func_9(self): |
---|
614 | n/a | async def foo(): pass |
---|
615 | n/a | |
---|
616 | n/a | with self.assertWarnsRegex( |
---|
617 | n/a | RuntimeWarning, "coroutine '.*test_func_9.*foo' was never awaited"): |
---|
618 | n/a | |
---|
619 | n/a | foo() |
---|
620 | n/a | support.gc_collect() |
---|
621 | n/a | |
---|
622 | n/a | def test_func_10(self): |
---|
623 | n/a | N = 0 |
---|
624 | n/a | |
---|
625 | n/a | @types.coroutine |
---|
626 | n/a | def gen(): |
---|
627 | n/a | nonlocal N |
---|
628 | n/a | try: |
---|
629 | n/a | a = yield |
---|
630 | n/a | yield (a ** 2) |
---|
631 | n/a | except ZeroDivisionError: |
---|
632 | n/a | N += 100 |
---|
633 | n/a | raise |
---|
634 | n/a | finally: |
---|
635 | n/a | N += 1 |
---|
636 | n/a | |
---|
637 | n/a | async def foo(): |
---|
638 | n/a | await gen() |
---|
639 | n/a | |
---|
640 | n/a | coro = foo() |
---|
641 | n/a | aw = coro.__await__() |
---|
642 | n/a | self.assertIs(aw, iter(aw)) |
---|
643 | n/a | next(aw) |
---|
644 | n/a | self.assertEqual(aw.send(10), 100) |
---|
645 | n/a | |
---|
646 | n/a | self.assertEqual(N, 0) |
---|
647 | n/a | aw.close() |
---|
648 | n/a | self.assertEqual(N, 1) |
---|
649 | n/a | |
---|
650 | n/a | coro = foo() |
---|
651 | n/a | aw = coro.__await__() |
---|
652 | n/a | next(aw) |
---|
653 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
654 | n/a | aw.throw(ZeroDivisionError, None, None) |
---|
655 | n/a | self.assertEqual(N, 102) |
---|
656 | n/a | |
---|
657 | n/a | def test_func_11(self): |
---|
658 | n/a | async def func(): pass |
---|
659 | n/a | coro = func() |
---|
660 | n/a | # Test that PyCoro_Type and _PyCoroWrapper_Type types were properly |
---|
661 | n/a | # initialized |
---|
662 | n/a | self.assertIn('__await__', dir(coro)) |
---|
663 | n/a | self.assertIn('__iter__', dir(coro.__await__())) |
---|
664 | n/a | self.assertIn('coroutine_wrapper', repr(coro.__await__())) |
---|
665 | n/a | coro.close() # avoid RuntimeWarning |
---|
666 | n/a | |
---|
667 | n/a | def test_func_12(self): |
---|
668 | n/a | async def g(): |
---|
669 | n/a | i = me.send(None) |
---|
670 | n/a | await foo |
---|
671 | n/a | me = g() |
---|
672 | n/a | with self.assertRaisesRegex(ValueError, |
---|
673 | n/a | "coroutine already executing"): |
---|
674 | n/a | me.send(None) |
---|
675 | n/a | |
---|
676 | n/a | def test_func_13(self): |
---|
677 | n/a | async def g(): |
---|
678 | n/a | pass |
---|
679 | n/a | with self.assertRaisesRegex( |
---|
680 | n/a | TypeError, |
---|
681 | n/a | "can't send non-None value to a just-started coroutine"): |
---|
682 | n/a | |
---|
683 | n/a | g().send('spam') |
---|
684 | n/a | |
---|
685 | n/a | def test_func_14(self): |
---|
686 | n/a | @types.coroutine |
---|
687 | n/a | def gen(): |
---|
688 | n/a | yield |
---|
689 | n/a | async def coro(): |
---|
690 | n/a | try: |
---|
691 | n/a | await gen() |
---|
692 | n/a | except GeneratorExit: |
---|
693 | n/a | await gen() |
---|
694 | n/a | c = coro() |
---|
695 | n/a | c.send(None) |
---|
696 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
697 | n/a | "coroutine ignored GeneratorExit"): |
---|
698 | n/a | c.close() |
---|
699 | n/a | |
---|
700 | n/a | def test_func_15(self): |
---|
701 | n/a | # See http://bugs.python.org/issue25887 for details |
---|
702 | n/a | |
---|
703 | n/a | async def spammer(): |
---|
704 | n/a | return 'spam' |
---|
705 | n/a | async def reader(coro): |
---|
706 | n/a | return await coro |
---|
707 | n/a | |
---|
708 | n/a | spammer_coro = spammer() |
---|
709 | n/a | |
---|
710 | n/a | with self.assertRaisesRegex(StopIteration, 'spam'): |
---|
711 | n/a | reader(spammer_coro).send(None) |
---|
712 | n/a | |
---|
713 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
714 | n/a | 'cannot reuse already awaited coroutine'): |
---|
715 | n/a | reader(spammer_coro).send(None) |
---|
716 | n/a | |
---|
717 | n/a | def test_func_16(self): |
---|
718 | n/a | # See http://bugs.python.org/issue25887 for details |
---|
719 | n/a | |
---|
720 | n/a | @types.coroutine |
---|
721 | n/a | def nop(): |
---|
722 | n/a | yield |
---|
723 | n/a | async def send(): |
---|
724 | n/a | await nop() |
---|
725 | n/a | return 'spam' |
---|
726 | n/a | async def read(coro): |
---|
727 | n/a | await nop() |
---|
728 | n/a | return await coro |
---|
729 | n/a | |
---|
730 | n/a | spammer = send() |
---|
731 | n/a | |
---|
732 | n/a | reader = read(spammer) |
---|
733 | n/a | reader.send(None) |
---|
734 | n/a | reader.send(None) |
---|
735 | n/a | with self.assertRaisesRegex(Exception, 'ham'): |
---|
736 | n/a | reader.throw(Exception('ham')) |
---|
737 | n/a | |
---|
738 | n/a | reader = read(spammer) |
---|
739 | n/a | reader.send(None) |
---|
740 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
741 | n/a | 'cannot reuse already awaited coroutine'): |
---|
742 | n/a | reader.send(None) |
---|
743 | n/a | |
---|
744 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
745 | n/a | 'cannot reuse already awaited coroutine'): |
---|
746 | n/a | reader.throw(Exception('wat')) |
---|
747 | n/a | |
---|
748 | n/a | def test_func_17(self): |
---|
749 | n/a | # See http://bugs.python.org/issue25887 for details |
---|
750 | n/a | |
---|
751 | n/a | async def coroutine(): |
---|
752 | n/a | return 'spam' |
---|
753 | n/a | |
---|
754 | n/a | coro = coroutine() |
---|
755 | n/a | with self.assertRaisesRegex(StopIteration, 'spam'): |
---|
756 | n/a | coro.send(None) |
---|
757 | n/a | |
---|
758 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
759 | n/a | 'cannot reuse already awaited coroutine'): |
---|
760 | n/a | coro.send(None) |
---|
761 | n/a | |
---|
762 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
763 | n/a | 'cannot reuse already awaited coroutine'): |
---|
764 | n/a | coro.throw(Exception('wat')) |
---|
765 | n/a | |
---|
766 | n/a | # Closing a coroutine shouldn't raise any exception even if it's |
---|
767 | n/a | # already closed/exhausted (similar to generators) |
---|
768 | n/a | coro.close() |
---|
769 | n/a | coro.close() |
---|
770 | n/a | |
---|
771 | n/a | def test_func_18(self): |
---|
772 | n/a | # See http://bugs.python.org/issue25887 for details |
---|
773 | n/a | |
---|
774 | n/a | async def coroutine(): |
---|
775 | n/a | return 'spam' |
---|
776 | n/a | |
---|
777 | n/a | coro = coroutine() |
---|
778 | n/a | await_iter = coro.__await__() |
---|
779 | n/a | it = iter(await_iter) |
---|
780 | n/a | |
---|
781 | n/a | with self.assertRaisesRegex(StopIteration, 'spam'): |
---|
782 | n/a | it.send(None) |
---|
783 | n/a | |
---|
784 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
785 | n/a | 'cannot reuse already awaited coroutine'): |
---|
786 | n/a | it.send(None) |
---|
787 | n/a | |
---|
788 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
789 | n/a | 'cannot reuse already awaited coroutine'): |
---|
790 | n/a | # Although the iterator protocol requires iterators to |
---|
791 | n/a | # raise another StopIteration here, we don't want to do |
---|
792 | n/a | # that. In this particular case, the iterator will raise |
---|
793 | n/a | # a RuntimeError, so that 'yield from' and 'await' |
---|
794 | n/a | # expressions will trigger the error, instead of silently |
---|
795 | n/a | # ignoring the call. |
---|
796 | n/a | next(it) |
---|
797 | n/a | |
---|
798 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
799 | n/a | 'cannot reuse already awaited coroutine'): |
---|
800 | n/a | it.throw(Exception('wat')) |
---|
801 | n/a | |
---|
802 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
803 | n/a | 'cannot reuse already awaited coroutine'): |
---|
804 | n/a | it.throw(Exception('wat')) |
---|
805 | n/a | |
---|
806 | n/a | # Closing a coroutine shouldn't raise any exception even if it's |
---|
807 | n/a | # already closed/exhausted (similar to generators) |
---|
808 | n/a | it.close() |
---|
809 | n/a | it.close() |
---|
810 | n/a | |
---|
811 | n/a | def test_func_19(self): |
---|
812 | n/a | CHK = 0 |
---|
813 | n/a | |
---|
814 | n/a | @types.coroutine |
---|
815 | n/a | def foo(): |
---|
816 | n/a | nonlocal CHK |
---|
817 | n/a | yield |
---|
818 | n/a | try: |
---|
819 | n/a | yield |
---|
820 | n/a | except GeneratorExit: |
---|
821 | n/a | CHK += 1 |
---|
822 | n/a | |
---|
823 | n/a | async def coroutine(): |
---|
824 | n/a | await foo() |
---|
825 | n/a | |
---|
826 | n/a | coro = coroutine() |
---|
827 | n/a | |
---|
828 | n/a | coro.send(None) |
---|
829 | n/a | coro.send(None) |
---|
830 | n/a | |
---|
831 | n/a | self.assertEqual(CHK, 0) |
---|
832 | n/a | coro.close() |
---|
833 | n/a | self.assertEqual(CHK, 1) |
---|
834 | n/a | |
---|
835 | n/a | for _ in range(3): |
---|
836 | n/a | # Closing a coroutine shouldn't raise any exception even if it's |
---|
837 | n/a | # already closed/exhausted (similar to generators) |
---|
838 | n/a | coro.close() |
---|
839 | n/a | self.assertEqual(CHK, 1) |
---|
840 | n/a | |
---|
841 | n/a | def test_coro_wrapper_send_tuple(self): |
---|
842 | n/a | async def foo(): |
---|
843 | n/a | return (10,) |
---|
844 | n/a | |
---|
845 | n/a | result = run_async__await__(foo()) |
---|
846 | n/a | self.assertEqual(result, ([], (10,))) |
---|
847 | n/a | |
---|
848 | n/a | def test_coro_wrapper_send_stop_iterator(self): |
---|
849 | n/a | async def foo(): |
---|
850 | n/a | return StopIteration(10) |
---|
851 | n/a | |
---|
852 | n/a | result = run_async__await__(foo()) |
---|
853 | n/a | self.assertIsInstance(result[1], StopIteration) |
---|
854 | n/a | self.assertEqual(result[1].value, 10) |
---|
855 | n/a | |
---|
856 | n/a | def test_cr_await(self): |
---|
857 | n/a | @types.coroutine |
---|
858 | n/a | def a(): |
---|
859 | n/a | self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_RUNNING) |
---|
860 | n/a | self.assertIsNone(coro_b.cr_await) |
---|
861 | n/a | yield |
---|
862 | n/a | self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_RUNNING) |
---|
863 | n/a | self.assertIsNone(coro_b.cr_await) |
---|
864 | n/a | |
---|
865 | n/a | async def c(): |
---|
866 | n/a | await a() |
---|
867 | n/a | |
---|
868 | n/a | async def b(): |
---|
869 | n/a | self.assertIsNone(coro_b.cr_await) |
---|
870 | n/a | await c() |
---|
871 | n/a | self.assertIsNone(coro_b.cr_await) |
---|
872 | n/a | |
---|
873 | n/a | coro_b = b() |
---|
874 | n/a | self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_CREATED) |
---|
875 | n/a | self.assertIsNone(coro_b.cr_await) |
---|
876 | n/a | |
---|
877 | n/a | coro_b.send(None) |
---|
878 | n/a | self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_SUSPENDED) |
---|
879 | n/a | self.assertEqual(coro_b.cr_await.cr_await.gi_code.co_name, 'a') |
---|
880 | n/a | |
---|
881 | n/a | with self.assertRaises(StopIteration): |
---|
882 | n/a | coro_b.send(None) # complete coroutine |
---|
883 | n/a | self.assertEqual(inspect.getcoroutinestate(coro_b), inspect.CORO_CLOSED) |
---|
884 | n/a | self.assertIsNone(coro_b.cr_await) |
---|
885 | n/a | |
---|
886 | n/a | def test_corotype_1(self): |
---|
887 | n/a | ct = types.CoroutineType |
---|
888 | n/a | self.assertIn('into coroutine', ct.send.__doc__) |
---|
889 | n/a | self.assertIn('inside coroutine', ct.close.__doc__) |
---|
890 | n/a | self.assertIn('in coroutine', ct.throw.__doc__) |
---|
891 | n/a | self.assertIn('of the coroutine', ct.__dict__['__name__'].__doc__) |
---|
892 | n/a | self.assertIn('of the coroutine', ct.__dict__['__qualname__'].__doc__) |
---|
893 | n/a | self.assertEqual(ct.__name__, 'coroutine') |
---|
894 | n/a | |
---|
895 | n/a | async def f(): pass |
---|
896 | n/a | c = f() |
---|
897 | n/a | self.assertIn('coroutine object', repr(c)) |
---|
898 | n/a | c.close() |
---|
899 | n/a | |
---|
900 | n/a | def test_await_1(self): |
---|
901 | n/a | |
---|
902 | n/a | async def foo(): |
---|
903 | n/a | await 1 |
---|
904 | n/a | with self.assertRaisesRegex(TypeError, "object int can.t.*await"): |
---|
905 | n/a | run_async(foo()) |
---|
906 | n/a | |
---|
907 | n/a | def test_await_2(self): |
---|
908 | n/a | async def foo(): |
---|
909 | n/a | await [] |
---|
910 | n/a | with self.assertRaisesRegex(TypeError, "object list can.t.*await"): |
---|
911 | n/a | run_async(foo()) |
---|
912 | n/a | |
---|
913 | n/a | def test_await_3(self): |
---|
914 | n/a | async def foo(): |
---|
915 | n/a | await AsyncYieldFrom([1, 2, 3]) |
---|
916 | n/a | |
---|
917 | n/a | self.assertEqual(run_async(foo()), ([1, 2, 3], None)) |
---|
918 | n/a | self.assertEqual(run_async__await__(foo()), ([1, 2, 3], None)) |
---|
919 | n/a | |
---|
920 | n/a | def test_await_4(self): |
---|
921 | n/a | async def bar(): |
---|
922 | n/a | return 42 |
---|
923 | n/a | |
---|
924 | n/a | async def foo(): |
---|
925 | n/a | return await bar() |
---|
926 | n/a | |
---|
927 | n/a | self.assertEqual(run_async(foo()), ([], 42)) |
---|
928 | n/a | |
---|
929 | n/a | def test_await_5(self): |
---|
930 | n/a | class Awaitable: |
---|
931 | n/a | def __await__(self): |
---|
932 | n/a | return |
---|
933 | n/a | |
---|
934 | n/a | async def foo(): |
---|
935 | n/a | return (await Awaitable()) |
---|
936 | n/a | |
---|
937 | n/a | with self.assertRaisesRegex( |
---|
938 | n/a | TypeError, "__await__.*returned non-iterator of type"): |
---|
939 | n/a | |
---|
940 | n/a | run_async(foo()) |
---|
941 | n/a | |
---|
942 | n/a | def test_await_6(self): |
---|
943 | n/a | class Awaitable: |
---|
944 | n/a | def __await__(self): |
---|
945 | n/a | return iter([52]) |
---|
946 | n/a | |
---|
947 | n/a | async def foo(): |
---|
948 | n/a | return (await Awaitable()) |
---|
949 | n/a | |
---|
950 | n/a | self.assertEqual(run_async(foo()), ([52], None)) |
---|
951 | n/a | |
---|
952 | n/a | def test_await_7(self): |
---|
953 | n/a | class Awaitable: |
---|
954 | n/a | def __await__(self): |
---|
955 | n/a | yield 42 |
---|
956 | n/a | return 100 |
---|
957 | n/a | |
---|
958 | n/a | async def foo(): |
---|
959 | n/a | return (await Awaitable()) |
---|
960 | n/a | |
---|
961 | n/a | self.assertEqual(run_async(foo()), ([42], 100)) |
---|
962 | n/a | |
---|
963 | n/a | def test_await_8(self): |
---|
964 | n/a | class Awaitable: |
---|
965 | n/a | pass |
---|
966 | n/a | |
---|
967 | n/a | async def foo(): return await Awaitable() |
---|
968 | n/a | |
---|
969 | n/a | with self.assertRaisesRegex( |
---|
970 | n/a | TypeError, "object Awaitable can't be used in 'await' expression"): |
---|
971 | n/a | |
---|
972 | n/a | run_async(foo()) |
---|
973 | n/a | |
---|
974 | n/a | def test_await_9(self): |
---|
975 | n/a | def wrap(): |
---|
976 | n/a | return bar |
---|
977 | n/a | |
---|
978 | n/a | async def bar(): |
---|
979 | n/a | return 42 |
---|
980 | n/a | |
---|
981 | n/a | async def foo(): |
---|
982 | n/a | b = bar() |
---|
983 | n/a | |
---|
984 | n/a | db = {'b': lambda: wrap} |
---|
985 | n/a | |
---|
986 | n/a | class DB: |
---|
987 | n/a | b = wrap |
---|
988 | n/a | |
---|
989 | n/a | return (await bar() + await wrap()() + await db['b']()()() + |
---|
990 | n/a | await bar() * 1000 + await DB.b()()) |
---|
991 | n/a | |
---|
992 | n/a | async def foo2(): |
---|
993 | n/a | return -await bar() |
---|
994 | n/a | |
---|
995 | n/a | self.assertEqual(run_async(foo()), ([], 42168)) |
---|
996 | n/a | self.assertEqual(run_async(foo2()), ([], -42)) |
---|
997 | n/a | |
---|
998 | n/a | def test_await_10(self): |
---|
999 | n/a | async def baz(): |
---|
1000 | n/a | return 42 |
---|
1001 | n/a | |
---|
1002 | n/a | async def bar(): |
---|
1003 | n/a | return baz() |
---|
1004 | n/a | |
---|
1005 | n/a | async def foo(): |
---|
1006 | n/a | return await (await bar()) |
---|
1007 | n/a | |
---|
1008 | n/a | self.assertEqual(run_async(foo()), ([], 42)) |
---|
1009 | n/a | |
---|
1010 | n/a | def test_await_11(self): |
---|
1011 | n/a | def ident(val): |
---|
1012 | n/a | return val |
---|
1013 | n/a | |
---|
1014 | n/a | async def bar(): |
---|
1015 | n/a | return 'spam' |
---|
1016 | n/a | |
---|
1017 | n/a | async def foo(): |
---|
1018 | n/a | return ident(val=await bar()) |
---|
1019 | n/a | |
---|
1020 | n/a | async def foo2(): |
---|
1021 | n/a | return await bar(), 'ham' |
---|
1022 | n/a | |
---|
1023 | n/a | self.assertEqual(run_async(foo2()), ([], ('spam', 'ham'))) |
---|
1024 | n/a | |
---|
1025 | n/a | def test_await_12(self): |
---|
1026 | n/a | async def coro(): |
---|
1027 | n/a | return 'spam' |
---|
1028 | n/a | |
---|
1029 | n/a | class Awaitable: |
---|
1030 | n/a | def __await__(self): |
---|
1031 | n/a | return coro() |
---|
1032 | n/a | |
---|
1033 | n/a | async def foo(): |
---|
1034 | n/a | return await Awaitable() |
---|
1035 | n/a | |
---|
1036 | n/a | with self.assertRaisesRegex( |
---|
1037 | n/a | TypeError, r"__await__\(\) returned a coroutine"): |
---|
1038 | n/a | |
---|
1039 | n/a | run_async(foo()) |
---|
1040 | n/a | |
---|
1041 | n/a | def test_await_13(self): |
---|
1042 | n/a | class Awaitable: |
---|
1043 | n/a | def __await__(self): |
---|
1044 | n/a | return self |
---|
1045 | n/a | |
---|
1046 | n/a | async def foo(): |
---|
1047 | n/a | return await Awaitable() |
---|
1048 | n/a | |
---|
1049 | n/a | with self.assertRaisesRegex( |
---|
1050 | n/a | TypeError, "__await__.*returned non-iterator of type"): |
---|
1051 | n/a | |
---|
1052 | n/a | run_async(foo()) |
---|
1053 | n/a | |
---|
1054 | n/a | def test_await_14(self): |
---|
1055 | n/a | class Wrapper: |
---|
1056 | n/a | # Forces the interpreter to use CoroutineType.__await__ |
---|
1057 | n/a | def __init__(self, coro): |
---|
1058 | n/a | assert coro.__class__ is types.CoroutineType |
---|
1059 | n/a | self.coro = coro |
---|
1060 | n/a | def __await__(self): |
---|
1061 | n/a | return self.coro.__await__() |
---|
1062 | n/a | |
---|
1063 | n/a | class FutureLike: |
---|
1064 | n/a | def __await__(self): |
---|
1065 | n/a | return (yield) |
---|
1066 | n/a | |
---|
1067 | n/a | class Marker(Exception): |
---|
1068 | n/a | pass |
---|
1069 | n/a | |
---|
1070 | n/a | async def coro1(): |
---|
1071 | n/a | try: |
---|
1072 | n/a | return await FutureLike() |
---|
1073 | n/a | except ZeroDivisionError: |
---|
1074 | n/a | raise Marker |
---|
1075 | n/a | async def coro2(): |
---|
1076 | n/a | return await Wrapper(coro1()) |
---|
1077 | n/a | |
---|
1078 | n/a | c = coro2() |
---|
1079 | n/a | c.send(None) |
---|
1080 | n/a | with self.assertRaisesRegex(StopIteration, 'spam'): |
---|
1081 | n/a | c.send('spam') |
---|
1082 | n/a | |
---|
1083 | n/a | c = coro2() |
---|
1084 | n/a | c.send(None) |
---|
1085 | n/a | with self.assertRaises(Marker): |
---|
1086 | n/a | c.throw(ZeroDivisionError) |
---|
1087 | n/a | |
---|
1088 | n/a | def test_await_15(self): |
---|
1089 | n/a | @types.coroutine |
---|
1090 | n/a | def nop(): |
---|
1091 | n/a | yield |
---|
1092 | n/a | |
---|
1093 | n/a | async def coroutine(): |
---|
1094 | n/a | await nop() |
---|
1095 | n/a | |
---|
1096 | n/a | async def waiter(coro): |
---|
1097 | n/a | await coro |
---|
1098 | n/a | |
---|
1099 | n/a | coro = coroutine() |
---|
1100 | n/a | coro.send(None) |
---|
1101 | n/a | |
---|
1102 | n/a | with self.assertRaisesRegex(RuntimeError, |
---|
1103 | n/a | "coroutine is being awaited already"): |
---|
1104 | n/a | waiter(coro).send(None) |
---|
1105 | n/a | |
---|
1106 | n/a | def test_with_1(self): |
---|
1107 | n/a | class Manager: |
---|
1108 | n/a | def __init__(self, name): |
---|
1109 | n/a | self.name = name |
---|
1110 | n/a | |
---|
1111 | n/a | async def __aenter__(self): |
---|
1112 | n/a | await AsyncYieldFrom(['enter-1-' + self.name, |
---|
1113 | n/a | 'enter-2-' + self.name]) |
---|
1114 | n/a | return self |
---|
1115 | n/a | |
---|
1116 | n/a | async def __aexit__(self, *args): |
---|
1117 | n/a | await AsyncYieldFrom(['exit-1-' + self.name, |
---|
1118 | n/a | 'exit-2-' + self.name]) |
---|
1119 | n/a | |
---|
1120 | n/a | if self.name == 'B': |
---|
1121 | n/a | return True |
---|
1122 | n/a | |
---|
1123 | n/a | |
---|
1124 | n/a | async def foo(): |
---|
1125 | n/a | async with Manager("A") as a, Manager("B") as b: |
---|
1126 | n/a | await AsyncYieldFrom([('managers', a.name, b.name)]) |
---|
1127 | n/a | 1/0 |
---|
1128 | n/a | |
---|
1129 | n/a | f = foo() |
---|
1130 | n/a | result, _ = run_async(f) |
---|
1131 | n/a | |
---|
1132 | n/a | self.assertEqual( |
---|
1133 | n/a | result, ['enter-1-A', 'enter-2-A', 'enter-1-B', 'enter-2-B', |
---|
1134 | n/a | ('managers', 'A', 'B'), |
---|
1135 | n/a | 'exit-1-B', 'exit-2-B', 'exit-1-A', 'exit-2-A'] |
---|
1136 | n/a | ) |
---|
1137 | n/a | |
---|
1138 | n/a | async def foo(): |
---|
1139 | n/a | async with Manager("A") as a, Manager("C") as c: |
---|
1140 | n/a | await AsyncYieldFrom([('managers', a.name, c.name)]) |
---|
1141 | n/a | 1/0 |
---|
1142 | n/a | |
---|
1143 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
1144 | n/a | run_async(foo()) |
---|
1145 | n/a | |
---|
1146 | n/a | def test_with_2(self): |
---|
1147 | n/a | class CM: |
---|
1148 | n/a | def __aenter__(self): |
---|
1149 | n/a | pass |
---|
1150 | n/a | |
---|
1151 | n/a | async def foo(): |
---|
1152 | n/a | async with CM(): |
---|
1153 | n/a | pass |
---|
1154 | n/a | |
---|
1155 | n/a | with self.assertRaisesRegex(AttributeError, '__aexit__'): |
---|
1156 | n/a | run_async(foo()) |
---|
1157 | n/a | |
---|
1158 | n/a | def test_with_3(self): |
---|
1159 | n/a | class CM: |
---|
1160 | n/a | def __aexit__(self): |
---|
1161 | n/a | pass |
---|
1162 | n/a | |
---|
1163 | n/a | async def foo(): |
---|
1164 | n/a | async with CM(): |
---|
1165 | n/a | pass |
---|
1166 | n/a | |
---|
1167 | n/a | with self.assertRaisesRegex(AttributeError, '__aenter__'): |
---|
1168 | n/a | run_async(foo()) |
---|
1169 | n/a | |
---|
1170 | n/a | def test_with_4(self): |
---|
1171 | n/a | class CM: |
---|
1172 | n/a | def __enter__(self): |
---|
1173 | n/a | pass |
---|
1174 | n/a | |
---|
1175 | n/a | def __exit__(self): |
---|
1176 | n/a | pass |
---|
1177 | n/a | |
---|
1178 | n/a | async def foo(): |
---|
1179 | n/a | async with CM(): |
---|
1180 | n/a | pass |
---|
1181 | n/a | |
---|
1182 | n/a | with self.assertRaisesRegex(AttributeError, '__aexit__'): |
---|
1183 | n/a | run_async(foo()) |
---|
1184 | n/a | |
---|
1185 | n/a | def test_with_5(self): |
---|
1186 | n/a | # While this test doesn't make a lot of sense, |
---|
1187 | n/a | # it's a regression test for an early bug with opcodes |
---|
1188 | n/a | # generation |
---|
1189 | n/a | |
---|
1190 | n/a | class CM: |
---|
1191 | n/a | async def __aenter__(self): |
---|
1192 | n/a | return self |
---|
1193 | n/a | |
---|
1194 | n/a | async def __aexit__(self, *exc): |
---|
1195 | n/a | pass |
---|
1196 | n/a | |
---|
1197 | n/a | async def func(): |
---|
1198 | n/a | async with CM(): |
---|
1199 | n/a | assert (1, ) == 1 |
---|
1200 | n/a | |
---|
1201 | n/a | with self.assertRaises(AssertionError): |
---|
1202 | n/a | run_async(func()) |
---|
1203 | n/a | |
---|
1204 | n/a | def test_with_6(self): |
---|
1205 | n/a | class CM: |
---|
1206 | n/a | def __aenter__(self): |
---|
1207 | n/a | return 123 |
---|
1208 | n/a | |
---|
1209 | n/a | def __aexit__(self, *e): |
---|
1210 | n/a | return 456 |
---|
1211 | n/a | |
---|
1212 | n/a | async def foo(): |
---|
1213 | n/a | async with CM(): |
---|
1214 | n/a | pass |
---|
1215 | n/a | |
---|
1216 | n/a | with self.assertRaisesRegex( |
---|
1217 | n/a | TypeError, "object int can't be used in 'await' expression"): |
---|
1218 | n/a | # it's important that __aexit__ wasn't called |
---|
1219 | n/a | run_async(foo()) |
---|
1220 | n/a | |
---|
1221 | n/a | def test_with_7(self): |
---|
1222 | n/a | class CM: |
---|
1223 | n/a | async def __aenter__(self): |
---|
1224 | n/a | return self |
---|
1225 | n/a | |
---|
1226 | n/a | def __aexit__(self, *e): |
---|
1227 | n/a | return 444 |
---|
1228 | n/a | |
---|
1229 | n/a | async def foo(): |
---|
1230 | n/a | async with CM(): |
---|
1231 | n/a | 1/0 |
---|
1232 | n/a | |
---|
1233 | n/a | try: |
---|
1234 | n/a | run_async(foo()) |
---|
1235 | n/a | except TypeError as exc: |
---|
1236 | n/a | self.assertRegex( |
---|
1237 | n/a | exc.args[0], "object int can't be used in 'await' expression") |
---|
1238 | n/a | self.assertTrue(exc.__context__ is not None) |
---|
1239 | n/a | self.assertTrue(isinstance(exc.__context__, ZeroDivisionError)) |
---|
1240 | n/a | else: |
---|
1241 | n/a | self.fail('invalid asynchronous context manager did not fail') |
---|
1242 | n/a | |
---|
1243 | n/a | |
---|
1244 | n/a | def test_with_8(self): |
---|
1245 | n/a | CNT = 0 |
---|
1246 | n/a | |
---|
1247 | n/a | class CM: |
---|
1248 | n/a | async def __aenter__(self): |
---|
1249 | n/a | return self |
---|
1250 | n/a | |
---|
1251 | n/a | def __aexit__(self, *e): |
---|
1252 | n/a | return 456 |
---|
1253 | n/a | |
---|
1254 | n/a | async def foo(): |
---|
1255 | n/a | nonlocal CNT |
---|
1256 | n/a | async with CM(): |
---|
1257 | n/a | CNT += 1 |
---|
1258 | n/a | |
---|
1259 | n/a | |
---|
1260 | n/a | with self.assertRaisesRegex( |
---|
1261 | n/a | TypeError, "object int can't be used in 'await' expression"): |
---|
1262 | n/a | |
---|
1263 | n/a | run_async(foo()) |
---|
1264 | n/a | |
---|
1265 | n/a | self.assertEqual(CNT, 1) |
---|
1266 | n/a | |
---|
1267 | n/a | |
---|
1268 | n/a | def test_with_9(self): |
---|
1269 | n/a | CNT = 0 |
---|
1270 | n/a | |
---|
1271 | n/a | class CM: |
---|
1272 | n/a | async def __aenter__(self): |
---|
1273 | n/a | return self |
---|
1274 | n/a | |
---|
1275 | n/a | async def __aexit__(self, *e): |
---|
1276 | n/a | 1/0 |
---|
1277 | n/a | |
---|
1278 | n/a | async def foo(): |
---|
1279 | n/a | nonlocal CNT |
---|
1280 | n/a | async with CM(): |
---|
1281 | n/a | CNT += 1 |
---|
1282 | n/a | |
---|
1283 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
1284 | n/a | run_async(foo()) |
---|
1285 | n/a | |
---|
1286 | n/a | self.assertEqual(CNT, 1) |
---|
1287 | n/a | |
---|
1288 | n/a | def test_with_10(self): |
---|
1289 | n/a | CNT = 0 |
---|
1290 | n/a | |
---|
1291 | n/a | class CM: |
---|
1292 | n/a | async def __aenter__(self): |
---|
1293 | n/a | return self |
---|
1294 | n/a | |
---|
1295 | n/a | async def __aexit__(self, *e): |
---|
1296 | n/a | 1/0 |
---|
1297 | n/a | |
---|
1298 | n/a | async def foo(): |
---|
1299 | n/a | nonlocal CNT |
---|
1300 | n/a | async with CM(): |
---|
1301 | n/a | async with CM(): |
---|
1302 | n/a | raise RuntimeError |
---|
1303 | n/a | |
---|
1304 | n/a | try: |
---|
1305 | n/a | run_async(foo()) |
---|
1306 | n/a | except ZeroDivisionError as exc: |
---|
1307 | n/a | self.assertTrue(exc.__context__ is not None) |
---|
1308 | n/a | self.assertTrue(isinstance(exc.__context__, ZeroDivisionError)) |
---|
1309 | n/a | self.assertTrue(isinstance(exc.__context__.__context__, |
---|
1310 | n/a | RuntimeError)) |
---|
1311 | n/a | else: |
---|
1312 | n/a | self.fail('exception from __aexit__ did not propagate') |
---|
1313 | n/a | |
---|
1314 | n/a | def test_with_11(self): |
---|
1315 | n/a | CNT = 0 |
---|
1316 | n/a | |
---|
1317 | n/a | class CM: |
---|
1318 | n/a | async def __aenter__(self): |
---|
1319 | n/a | raise NotImplementedError |
---|
1320 | n/a | |
---|
1321 | n/a | async def __aexit__(self, *e): |
---|
1322 | n/a | 1/0 |
---|
1323 | n/a | |
---|
1324 | n/a | async def foo(): |
---|
1325 | n/a | nonlocal CNT |
---|
1326 | n/a | async with CM(): |
---|
1327 | n/a | raise RuntimeError |
---|
1328 | n/a | |
---|
1329 | n/a | try: |
---|
1330 | n/a | run_async(foo()) |
---|
1331 | n/a | except NotImplementedError as exc: |
---|
1332 | n/a | self.assertTrue(exc.__context__ is None) |
---|
1333 | n/a | else: |
---|
1334 | n/a | self.fail('exception from __aenter__ did not propagate') |
---|
1335 | n/a | |
---|
1336 | n/a | def test_with_12(self): |
---|
1337 | n/a | CNT = 0 |
---|
1338 | n/a | |
---|
1339 | n/a | class CM: |
---|
1340 | n/a | async def __aenter__(self): |
---|
1341 | n/a | return self |
---|
1342 | n/a | |
---|
1343 | n/a | async def __aexit__(self, *e): |
---|
1344 | n/a | return True |
---|
1345 | n/a | |
---|
1346 | n/a | async def foo(): |
---|
1347 | n/a | nonlocal CNT |
---|
1348 | n/a | async with CM() as cm: |
---|
1349 | n/a | self.assertIs(cm.__class__, CM) |
---|
1350 | n/a | raise RuntimeError |
---|
1351 | n/a | |
---|
1352 | n/a | run_async(foo()) |
---|
1353 | n/a | |
---|
1354 | n/a | def test_with_13(self): |
---|
1355 | n/a | CNT = 0 |
---|
1356 | n/a | |
---|
1357 | n/a | class CM: |
---|
1358 | n/a | async def __aenter__(self): |
---|
1359 | n/a | 1/0 |
---|
1360 | n/a | |
---|
1361 | n/a | async def __aexit__(self, *e): |
---|
1362 | n/a | return True |
---|
1363 | n/a | |
---|
1364 | n/a | async def foo(): |
---|
1365 | n/a | nonlocal CNT |
---|
1366 | n/a | CNT += 1 |
---|
1367 | n/a | async with CM(): |
---|
1368 | n/a | CNT += 1000 |
---|
1369 | n/a | CNT += 10000 |
---|
1370 | n/a | |
---|
1371 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
1372 | n/a | run_async(foo()) |
---|
1373 | n/a | self.assertEqual(CNT, 1) |
---|
1374 | n/a | |
---|
1375 | n/a | def test_for_1(self): |
---|
1376 | n/a | aiter_calls = 0 |
---|
1377 | n/a | |
---|
1378 | n/a | class AsyncIter: |
---|
1379 | n/a | def __init__(self): |
---|
1380 | n/a | self.i = 0 |
---|
1381 | n/a | |
---|
1382 | n/a | async def __aiter__(self): |
---|
1383 | n/a | nonlocal aiter_calls |
---|
1384 | n/a | aiter_calls += 1 |
---|
1385 | n/a | return self |
---|
1386 | n/a | |
---|
1387 | n/a | async def __anext__(self): |
---|
1388 | n/a | self.i += 1 |
---|
1389 | n/a | |
---|
1390 | n/a | if not (self.i % 10): |
---|
1391 | n/a | await AsyncYield(self.i * 10) |
---|
1392 | n/a | |
---|
1393 | n/a | if self.i > 100: |
---|
1394 | n/a | raise StopAsyncIteration |
---|
1395 | n/a | |
---|
1396 | n/a | return self.i, self.i |
---|
1397 | n/a | |
---|
1398 | n/a | |
---|
1399 | n/a | buffer = [] |
---|
1400 | n/a | async def test1(): |
---|
1401 | n/a | with self.assertWarnsRegex(DeprecationWarning, "legacy"): |
---|
1402 | n/a | async for i1, i2 in AsyncIter(): |
---|
1403 | n/a | buffer.append(i1 + i2) |
---|
1404 | n/a | |
---|
1405 | n/a | yielded, _ = run_async(test1()) |
---|
1406 | n/a | # Make sure that __aiter__ was called only once |
---|
1407 | n/a | self.assertEqual(aiter_calls, 1) |
---|
1408 | n/a | self.assertEqual(yielded, [i * 100 for i in range(1, 11)]) |
---|
1409 | n/a | self.assertEqual(buffer, [i*2 for i in range(1, 101)]) |
---|
1410 | n/a | |
---|
1411 | n/a | |
---|
1412 | n/a | buffer = [] |
---|
1413 | n/a | async def test2(): |
---|
1414 | n/a | nonlocal buffer |
---|
1415 | n/a | with self.assertWarnsRegex(DeprecationWarning, "legacy"): |
---|
1416 | n/a | async for i in AsyncIter(): |
---|
1417 | n/a | buffer.append(i[0]) |
---|
1418 | n/a | if i[0] == 20: |
---|
1419 | n/a | break |
---|
1420 | n/a | else: |
---|
1421 | n/a | buffer.append('what?') |
---|
1422 | n/a | buffer.append('end') |
---|
1423 | n/a | |
---|
1424 | n/a | yielded, _ = run_async(test2()) |
---|
1425 | n/a | # Make sure that __aiter__ was called only once |
---|
1426 | n/a | self.assertEqual(aiter_calls, 2) |
---|
1427 | n/a | self.assertEqual(yielded, [100, 200]) |
---|
1428 | n/a | self.assertEqual(buffer, [i for i in range(1, 21)] + ['end']) |
---|
1429 | n/a | |
---|
1430 | n/a | |
---|
1431 | n/a | buffer = [] |
---|
1432 | n/a | async def test3(): |
---|
1433 | n/a | nonlocal buffer |
---|
1434 | n/a | with self.assertWarnsRegex(DeprecationWarning, "legacy"): |
---|
1435 | n/a | async for i in AsyncIter(): |
---|
1436 | n/a | if i[0] > 20: |
---|
1437 | n/a | continue |
---|
1438 | n/a | buffer.append(i[0]) |
---|
1439 | n/a | else: |
---|
1440 | n/a | buffer.append('what?') |
---|
1441 | n/a | buffer.append('end') |
---|
1442 | n/a | |
---|
1443 | n/a | yielded, _ = run_async(test3()) |
---|
1444 | n/a | # Make sure that __aiter__ was called only once |
---|
1445 | n/a | self.assertEqual(aiter_calls, 3) |
---|
1446 | n/a | self.assertEqual(yielded, [i * 100 for i in range(1, 11)]) |
---|
1447 | n/a | self.assertEqual(buffer, [i for i in range(1, 21)] + |
---|
1448 | n/a | ['what?', 'end']) |
---|
1449 | n/a | |
---|
1450 | n/a | def test_for_2(self): |
---|
1451 | n/a | tup = (1, 2, 3) |
---|
1452 | n/a | refs_before = sys.getrefcount(tup) |
---|
1453 | n/a | |
---|
1454 | n/a | async def foo(): |
---|
1455 | n/a | async for i in tup: |
---|
1456 | n/a | print('never going to happen') |
---|
1457 | n/a | |
---|
1458 | n/a | with self.assertRaisesRegex( |
---|
1459 | n/a | TypeError, "async for' requires an object.*__aiter__.*tuple"): |
---|
1460 | n/a | |
---|
1461 | n/a | run_async(foo()) |
---|
1462 | n/a | |
---|
1463 | n/a | self.assertEqual(sys.getrefcount(tup), refs_before) |
---|
1464 | n/a | |
---|
1465 | n/a | def test_for_3(self): |
---|
1466 | n/a | class I: |
---|
1467 | n/a | def __aiter__(self): |
---|
1468 | n/a | return self |
---|
1469 | n/a | |
---|
1470 | n/a | aiter = I() |
---|
1471 | n/a | refs_before = sys.getrefcount(aiter) |
---|
1472 | n/a | |
---|
1473 | n/a | async def foo(): |
---|
1474 | n/a | async for i in aiter: |
---|
1475 | n/a | print('never going to happen') |
---|
1476 | n/a | |
---|
1477 | n/a | with self.assertRaisesRegex( |
---|
1478 | n/a | TypeError, |
---|
1479 | n/a | r"async for' received an invalid object.*__aiter.*\: I"): |
---|
1480 | n/a | |
---|
1481 | n/a | run_async(foo()) |
---|
1482 | n/a | |
---|
1483 | n/a | self.assertEqual(sys.getrefcount(aiter), refs_before) |
---|
1484 | n/a | |
---|
1485 | n/a | def test_for_4(self): |
---|
1486 | n/a | class I: |
---|
1487 | n/a | def __aiter__(self): |
---|
1488 | n/a | return self |
---|
1489 | n/a | |
---|
1490 | n/a | def __anext__(self): |
---|
1491 | n/a | return () |
---|
1492 | n/a | |
---|
1493 | n/a | aiter = I() |
---|
1494 | n/a | refs_before = sys.getrefcount(aiter) |
---|
1495 | n/a | |
---|
1496 | n/a | async def foo(): |
---|
1497 | n/a | async for i in aiter: |
---|
1498 | n/a | print('never going to happen') |
---|
1499 | n/a | |
---|
1500 | n/a | with self.assertRaisesRegex( |
---|
1501 | n/a | TypeError, |
---|
1502 | n/a | "async for' received an invalid object.*__anext__.*tuple"): |
---|
1503 | n/a | |
---|
1504 | n/a | run_async(foo()) |
---|
1505 | n/a | |
---|
1506 | n/a | self.assertEqual(sys.getrefcount(aiter), refs_before) |
---|
1507 | n/a | |
---|
1508 | n/a | def test_for_5(self): |
---|
1509 | n/a | class I: |
---|
1510 | n/a | async def __aiter__(self): |
---|
1511 | n/a | return self |
---|
1512 | n/a | |
---|
1513 | n/a | def __anext__(self): |
---|
1514 | n/a | return 123 |
---|
1515 | n/a | |
---|
1516 | n/a | async def foo(): |
---|
1517 | n/a | with self.assertWarnsRegex(DeprecationWarning, "legacy"): |
---|
1518 | n/a | async for i in I(): |
---|
1519 | n/a | print('never going to happen') |
---|
1520 | n/a | |
---|
1521 | n/a | with self.assertRaisesRegex( |
---|
1522 | n/a | TypeError, |
---|
1523 | n/a | "async for' received an invalid object.*__anext.*int"): |
---|
1524 | n/a | |
---|
1525 | n/a | run_async(foo()) |
---|
1526 | n/a | |
---|
1527 | n/a | def test_for_6(self): |
---|
1528 | n/a | I = 0 |
---|
1529 | n/a | |
---|
1530 | n/a | class Manager: |
---|
1531 | n/a | async def __aenter__(self): |
---|
1532 | n/a | nonlocal I |
---|
1533 | n/a | I += 10000 |
---|
1534 | n/a | |
---|
1535 | n/a | async def __aexit__(self, *args): |
---|
1536 | n/a | nonlocal I |
---|
1537 | n/a | I += 100000 |
---|
1538 | n/a | |
---|
1539 | n/a | class Iterable: |
---|
1540 | n/a | def __init__(self): |
---|
1541 | n/a | self.i = 0 |
---|
1542 | n/a | |
---|
1543 | n/a | def __aiter__(self): |
---|
1544 | n/a | return self |
---|
1545 | n/a | |
---|
1546 | n/a | async def __anext__(self): |
---|
1547 | n/a | if self.i > 10: |
---|
1548 | n/a | raise StopAsyncIteration |
---|
1549 | n/a | self.i += 1 |
---|
1550 | n/a | return self.i |
---|
1551 | n/a | |
---|
1552 | n/a | ############## |
---|
1553 | n/a | |
---|
1554 | n/a | manager = Manager() |
---|
1555 | n/a | iterable = Iterable() |
---|
1556 | n/a | mrefs_before = sys.getrefcount(manager) |
---|
1557 | n/a | irefs_before = sys.getrefcount(iterable) |
---|
1558 | n/a | |
---|
1559 | n/a | async def main(): |
---|
1560 | n/a | nonlocal I |
---|
1561 | n/a | |
---|
1562 | n/a | async with manager: |
---|
1563 | n/a | async for i in iterable: |
---|
1564 | n/a | I += 1 |
---|
1565 | n/a | I += 1000 |
---|
1566 | n/a | |
---|
1567 | n/a | with warnings.catch_warnings(): |
---|
1568 | n/a | warnings.simplefilter("error") |
---|
1569 | n/a | # Test that __aiter__ that returns an asynchronous iterator |
---|
1570 | n/a | # directly does not throw any warnings. |
---|
1571 | n/a | run_async(main()) |
---|
1572 | n/a | self.assertEqual(I, 111011) |
---|
1573 | n/a | |
---|
1574 | n/a | self.assertEqual(sys.getrefcount(manager), mrefs_before) |
---|
1575 | n/a | self.assertEqual(sys.getrefcount(iterable), irefs_before) |
---|
1576 | n/a | |
---|
1577 | n/a | ############## |
---|
1578 | n/a | |
---|
1579 | n/a | async def main(): |
---|
1580 | n/a | nonlocal I |
---|
1581 | n/a | |
---|
1582 | n/a | async with Manager(): |
---|
1583 | n/a | async for i in Iterable(): |
---|
1584 | n/a | I += 1 |
---|
1585 | n/a | I += 1000 |
---|
1586 | n/a | |
---|
1587 | n/a | async with Manager(): |
---|
1588 | n/a | async for i in Iterable(): |
---|
1589 | n/a | I += 1 |
---|
1590 | n/a | I += 1000 |
---|
1591 | n/a | |
---|
1592 | n/a | run_async(main()) |
---|
1593 | n/a | self.assertEqual(I, 333033) |
---|
1594 | n/a | |
---|
1595 | n/a | ############## |
---|
1596 | n/a | |
---|
1597 | n/a | async def main(): |
---|
1598 | n/a | nonlocal I |
---|
1599 | n/a | |
---|
1600 | n/a | async with Manager(): |
---|
1601 | n/a | I += 100 |
---|
1602 | n/a | async for i in Iterable(): |
---|
1603 | n/a | I += 1 |
---|
1604 | n/a | else: |
---|
1605 | n/a | I += 10000000 |
---|
1606 | n/a | I += 1000 |
---|
1607 | n/a | |
---|
1608 | n/a | async with Manager(): |
---|
1609 | n/a | I += 100 |
---|
1610 | n/a | async for i in Iterable(): |
---|
1611 | n/a | I += 1 |
---|
1612 | n/a | else: |
---|
1613 | n/a | I += 10000000 |
---|
1614 | n/a | I += 1000 |
---|
1615 | n/a | |
---|
1616 | n/a | run_async(main()) |
---|
1617 | n/a | self.assertEqual(I, 20555255) |
---|
1618 | n/a | |
---|
1619 | n/a | def test_for_7(self): |
---|
1620 | n/a | CNT = 0 |
---|
1621 | n/a | class AI: |
---|
1622 | n/a | async def __aiter__(self): |
---|
1623 | n/a | 1/0 |
---|
1624 | n/a | async def foo(): |
---|
1625 | n/a | nonlocal CNT |
---|
1626 | n/a | with self.assertWarnsRegex(DeprecationWarning, "legacy"): |
---|
1627 | n/a | async for i in AI(): |
---|
1628 | n/a | CNT += 1 |
---|
1629 | n/a | CNT += 10 |
---|
1630 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
1631 | n/a | run_async(foo()) |
---|
1632 | n/a | self.assertEqual(CNT, 0) |
---|
1633 | n/a | |
---|
1634 | n/a | def test_for_8(self): |
---|
1635 | n/a | CNT = 0 |
---|
1636 | n/a | class AI: |
---|
1637 | n/a | def __aiter__(self): |
---|
1638 | n/a | 1/0 |
---|
1639 | n/a | async def foo(): |
---|
1640 | n/a | nonlocal CNT |
---|
1641 | n/a | async for i in AI(): |
---|
1642 | n/a | CNT += 1 |
---|
1643 | n/a | CNT += 10 |
---|
1644 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
1645 | n/a | with warnings.catch_warnings(): |
---|
1646 | n/a | warnings.simplefilter("error") |
---|
1647 | n/a | # Test that if __aiter__ raises an exception it propagates |
---|
1648 | n/a | # without any kind of warning. |
---|
1649 | n/a | run_async(foo()) |
---|
1650 | n/a | self.assertEqual(CNT, 0) |
---|
1651 | n/a | |
---|
1652 | n/a | def test_for_9(self): |
---|
1653 | n/a | # Test that DeprecationWarning can safely be converted into |
---|
1654 | n/a | # an exception (__aiter__ should not have a chance to raise |
---|
1655 | n/a | # a ZeroDivisionError.) |
---|
1656 | n/a | class AI: |
---|
1657 | n/a | async def __aiter__(self): |
---|
1658 | n/a | 1/0 |
---|
1659 | n/a | async def foo(): |
---|
1660 | n/a | async for i in AI(): |
---|
1661 | n/a | pass |
---|
1662 | n/a | |
---|
1663 | n/a | with self.assertRaises(DeprecationWarning): |
---|
1664 | n/a | with warnings.catch_warnings(): |
---|
1665 | n/a | warnings.simplefilter("error") |
---|
1666 | n/a | run_async(foo()) |
---|
1667 | n/a | |
---|
1668 | n/a | def test_for_10(self): |
---|
1669 | n/a | # Test that DeprecationWarning can safely be converted into |
---|
1670 | n/a | # an exception. |
---|
1671 | n/a | class AI: |
---|
1672 | n/a | async def __aiter__(self): |
---|
1673 | n/a | pass |
---|
1674 | n/a | async def foo(): |
---|
1675 | n/a | async for i in AI(): |
---|
1676 | n/a | pass |
---|
1677 | n/a | |
---|
1678 | n/a | with self.assertRaises(DeprecationWarning): |
---|
1679 | n/a | with warnings.catch_warnings(): |
---|
1680 | n/a | warnings.simplefilter("error") |
---|
1681 | n/a | run_async(foo()) |
---|
1682 | n/a | |
---|
1683 | n/a | def test_for_tuple(self): |
---|
1684 | n/a | class Done(Exception): pass |
---|
1685 | n/a | |
---|
1686 | n/a | class AIter(tuple): |
---|
1687 | n/a | i = 0 |
---|
1688 | n/a | def __aiter__(self): |
---|
1689 | n/a | return self |
---|
1690 | n/a | async def __anext__(self): |
---|
1691 | n/a | if self.i >= len(self): |
---|
1692 | n/a | raise StopAsyncIteration |
---|
1693 | n/a | self.i += 1 |
---|
1694 | n/a | return self[self.i - 1] |
---|
1695 | n/a | |
---|
1696 | n/a | result = [] |
---|
1697 | n/a | async def foo(): |
---|
1698 | n/a | async for i in AIter([42]): |
---|
1699 | n/a | result.append(i) |
---|
1700 | n/a | raise Done |
---|
1701 | n/a | |
---|
1702 | n/a | with self.assertRaises(Done): |
---|
1703 | n/a | foo().send(None) |
---|
1704 | n/a | self.assertEqual(result, [42]) |
---|
1705 | n/a | |
---|
1706 | n/a | def test_for_stop_iteration(self): |
---|
1707 | n/a | class Done(Exception): pass |
---|
1708 | n/a | |
---|
1709 | n/a | class AIter(StopIteration): |
---|
1710 | n/a | i = 0 |
---|
1711 | n/a | def __aiter__(self): |
---|
1712 | n/a | return self |
---|
1713 | n/a | async def __anext__(self): |
---|
1714 | n/a | if self.i: |
---|
1715 | n/a | raise StopAsyncIteration |
---|
1716 | n/a | self.i += 1 |
---|
1717 | n/a | return self.value |
---|
1718 | n/a | |
---|
1719 | n/a | result = [] |
---|
1720 | n/a | async def foo(): |
---|
1721 | n/a | async for i in AIter(42): |
---|
1722 | n/a | result.append(i) |
---|
1723 | n/a | raise Done |
---|
1724 | n/a | |
---|
1725 | n/a | with self.assertRaises(Done): |
---|
1726 | n/a | foo().send(None) |
---|
1727 | n/a | self.assertEqual(result, [42]) |
---|
1728 | n/a | |
---|
1729 | n/a | def test_comp_1(self): |
---|
1730 | n/a | async def f(i): |
---|
1731 | n/a | return i |
---|
1732 | n/a | |
---|
1733 | n/a | async def run_list(): |
---|
1734 | n/a | return [await c for c in [f(1), f(41)]] |
---|
1735 | n/a | |
---|
1736 | n/a | async def run_set(): |
---|
1737 | n/a | return {await c for c in [f(1), f(41)]} |
---|
1738 | n/a | |
---|
1739 | n/a | async def run_dict1(): |
---|
1740 | n/a | return {await c: 'a' for c in [f(1), f(41)]} |
---|
1741 | n/a | |
---|
1742 | n/a | async def run_dict2(): |
---|
1743 | n/a | return {i: await c for i, c in enumerate([f(1), f(41)])} |
---|
1744 | n/a | |
---|
1745 | n/a | self.assertEqual(run_async(run_list()), ([], [1, 41])) |
---|
1746 | n/a | self.assertEqual(run_async(run_set()), ([], {1, 41})) |
---|
1747 | n/a | self.assertEqual(run_async(run_dict1()), ([], {1: 'a', 41: 'a'})) |
---|
1748 | n/a | self.assertEqual(run_async(run_dict2()), ([], {0: 1, 1: 41})) |
---|
1749 | n/a | |
---|
1750 | n/a | def test_comp_2(self): |
---|
1751 | n/a | async def f(i): |
---|
1752 | n/a | return i |
---|
1753 | n/a | |
---|
1754 | n/a | async def run_list(): |
---|
1755 | n/a | return [s for c in [f(''), f('abc'), f(''), f(['de', 'fg'])] |
---|
1756 | n/a | for s in await c] |
---|
1757 | n/a | |
---|
1758 | n/a | self.assertEqual( |
---|
1759 | n/a | run_async(run_list()), |
---|
1760 | n/a | ([], ['a', 'b', 'c', 'de', 'fg'])) |
---|
1761 | n/a | |
---|
1762 | n/a | async def run_set(): |
---|
1763 | n/a | return {d |
---|
1764 | n/a | for c in [f([f([10, 30]), |
---|
1765 | n/a | f([20])])] |
---|
1766 | n/a | for s in await c |
---|
1767 | n/a | for d in await s} |
---|
1768 | n/a | |
---|
1769 | n/a | self.assertEqual( |
---|
1770 | n/a | run_async(run_set()), |
---|
1771 | n/a | ([], {10, 20, 30})) |
---|
1772 | n/a | |
---|
1773 | n/a | async def run_set2(): |
---|
1774 | n/a | return {await s |
---|
1775 | n/a | for c in [f([f(10), f(20)])] |
---|
1776 | n/a | for s in await c} |
---|
1777 | n/a | |
---|
1778 | n/a | self.assertEqual( |
---|
1779 | n/a | run_async(run_set2()), |
---|
1780 | n/a | ([], {10, 20})) |
---|
1781 | n/a | |
---|
1782 | n/a | def test_comp_3(self): |
---|
1783 | n/a | async def f(it): |
---|
1784 | n/a | for i in it: |
---|
1785 | n/a | yield i |
---|
1786 | n/a | |
---|
1787 | n/a | async def run_list(): |
---|
1788 | n/a | return [i + 1 async for i in f([10, 20])] |
---|
1789 | n/a | self.assertEqual( |
---|
1790 | n/a | run_async(run_list()), |
---|
1791 | n/a | ([], [11, 21])) |
---|
1792 | n/a | |
---|
1793 | n/a | async def run_set(): |
---|
1794 | n/a | return {i + 1 async for i in f([10, 20])} |
---|
1795 | n/a | self.assertEqual( |
---|
1796 | n/a | run_async(run_set()), |
---|
1797 | n/a | ([], {11, 21})) |
---|
1798 | n/a | |
---|
1799 | n/a | async def run_dict(): |
---|
1800 | n/a | return {i + 1: i + 2 async for i in f([10, 20])} |
---|
1801 | n/a | self.assertEqual( |
---|
1802 | n/a | run_async(run_dict()), |
---|
1803 | n/a | ([], {11: 12, 21: 22})) |
---|
1804 | n/a | |
---|
1805 | n/a | async def run_gen(): |
---|
1806 | n/a | gen = (i + 1 async for i in f([10, 20])) |
---|
1807 | n/a | return [g + 100 async for g in gen] |
---|
1808 | n/a | self.assertEqual( |
---|
1809 | n/a | run_async(run_gen()), |
---|
1810 | n/a | ([], [111, 121])) |
---|
1811 | n/a | |
---|
1812 | n/a | def test_comp_4(self): |
---|
1813 | n/a | async def f(it): |
---|
1814 | n/a | for i in it: |
---|
1815 | n/a | yield i |
---|
1816 | n/a | |
---|
1817 | n/a | async def run_list(): |
---|
1818 | n/a | return [i + 1 async for i in f([10, 20]) if i > 10] |
---|
1819 | n/a | self.assertEqual( |
---|
1820 | n/a | run_async(run_list()), |
---|
1821 | n/a | ([], [21])) |
---|
1822 | n/a | |
---|
1823 | n/a | async def run_set(): |
---|
1824 | n/a | return {i + 1 async for i in f([10, 20]) if i > 10} |
---|
1825 | n/a | self.assertEqual( |
---|
1826 | n/a | run_async(run_set()), |
---|
1827 | n/a | ([], {21})) |
---|
1828 | n/a | |
---|
1829 | n/a | async def run_dict(): |
---|
1830 | n/a | return {i + 1: i + 2 async for i in f([10, 20]) if i > 10} |
---|
1831 | n/a | self.assertEqual( |
---|
1832 | n/a | run_async(run_dict()), |
---|
1833 | n/a | ([], {21: 22})) |
---|
1834 | n/a | |
---|
1835 | n/a | async def run_gen(): |
---|
1836 | n/a | gen = (i + 1 async for i in f([10, 20]) if i > 10) |
---|
1837 | n/a | return [g + 100 async for g in gen] |
---|
1838 | n/a | self.assertEqual( |
---|
1839 | n/a | run_async(run_gen()), |
---|
1840 | n/a | ([], [121])) |
---|
1841 | n/a | |
---|
1842 | n/a | def test_comp_5(self): |
---|
1843 | n/a | async def f(it): |
---|
1844 | n/a | for i in it: |
---|
1845 | n/a | yield i |
---|
1846 | n/a | |
---|
1847 | n/a | async def run_list(): |
---|
1848 | n/a | return [i + 1 for pair in ([10, 20], [30, 40]) if pair[0] > 10 |
---|
1849 | n/a | async for i in f(pair) if i > 30] |
---|
1850 | n/a | self.assertEqual( |
---|
1851 | n/a | run_async(run_list()), |
---|
1852 | n/a | ([], [41])) |
---|
1853 | n/a | |
---|
1854 | n/a | def test_comp_6(self): |
---|
1855 | n/a | async def f(it): |
---|
1856 | n/a | for i in it: |
---|
1857 | n/a | yield i |
---|
1858 | n/a | |
---|
1859 | n/a | async def run_list(): |
---|
1860 | n/a | return [i + 1 async for seq in f([(10, 20), (30,)]) |
---|
1861 | n/a | for i in seq] |
---|
1862 | n/a | |
---|
1863 | n/a | self.assertEqual( |
---|
1864 | n/a | run_async(run_list()), |
---|
1865 | n/a | ([], [11, 21, 31])) |
---|
1866 | n/a | |
---|
1867 | n/a | def test_comp_7(self): |
---|
1868 | n/a | async def f(): |
---|
1869 | n/a | yield 1 |
---|
1870 | n/a | yield 2 |
---|
1871 | n/a | raise Exception('aaa') |
---|
1872 | n/a | |
---|
1873 | n/a | async def run_list(): |
---|
1874 | n/a | return [i async for i in f()] |
---|
1875 | n/a | |
---|
1876 | n/a | with self.assertRaisesRegex(Exception, 'aaa'): |
---|
1877 | n/a | run_async(run_list()) |
---|
1878 | n/a | |
---|
1879 | n/a | def test_comp_8(self): |
---|
1880 | n/a | async def f(): |
---|
1881 | n/a | return [i for i in [1, 2, 3]] |
---|
1882 | n/a | |
---|
1883 | n/a | self.assertEqual( |
---|
1884 | n/a | run_async(f()), |
---|
1885 | n/a | ([], [1, 2, 3])) |
---|
1886 | n/a | |
---|
1887 | n/a | def test_comp_9(self): |
---|
1888 | n/a | async def gen(): |
---|
1889 | n/a | yield 1 |
---|
1890 | n/a | yield 2 |
---|
1891 | n/a | async def f(): |
---|
1892 | n/a | l = [i async for i in gen()] |
---|
1893 | n/a | return [i for i in l] |
---|
1894 | n/a | |
---|
1895 | n/a | self.assertEqual( |
---|
1896 | n/a | run_async(f()), |
---|
1897 | n/a | ([], [1, 2])) |
---|
1898 | n/a | |
---|
1899 | n/a | def test_comp_10(self): |
---|
1900 | n/a | async def f(): |
---|
1901 | n/a | xx = {i for i in [1, 2, 3]} |
---|
1902 | n/a | return {x: x for x in xx} |
---|
1903 | n/a | |
---|
1904 | n/a | self.assertEqual( |
---|
1905 | n/a | run_async(f()), |
---|
1906 | n/a | ([], {1: 1, 2: 2, 3: 3})) |
---|
1907 | n/a | |
---|
1908 | n/a | def test_copy(self): |
---|
1909 | n/a | async def func(): pass |
---|
1910 | n/a | coro = func() |
---|
1911 | n/a | with self.assertRaises(TypeError): |
---|
1912 | n/a | copy.copy(coro) |
---|
1913 | n/a | |
---|
1914 | n/a | aw = coro.__await__() |
---|
1915 | n/a | try: |
---|
1916 | n/a | with self.assertRaises(TypeError): |
---|
1917 | n/a | copy.copy(aw) |
---|
1918 | n/a | finally: |
---|
1919 | n/a | aw.close() |
---|
1920 | n/a | |
---|
1921 | n/a | def test_pickle(self): |
---|
1922 | n/a | async def func(): pass |
---|
1923 | n/a | coro = func() |
---|
1924 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1925 | n/a | with self.assertRaises((TypeError, pickle.PicklingError)): |
---|
1926 | n/a | pickle.dumps(coro, proto) |
---|
1927 | n/a | |
---|
1928 | n/a | aw = coro.__await__() |
---|
1929 | n/a | try: |
---|
1930 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
1931 | n/a | with self.assertRaises((TypeError, pickle.PicklingError)): |
---|
1932 | n/a | pickle.dumps(aw, proto) |
---|
1933 | n/a | finally: |
---|
1934 | n/a | aw.close() |
---|
1935 | n/a | |
---|
1936 | n/a | def test_fatal_coro_warning(self): |
---|
1937 | n/a | # Issue 27811 |
---|
1938 | n/a | async def func(): pass |
---|
1939 | n/a | with warnings.catch_warnings(), support.captured_stderr() as stderr: |
---|
1940 | n/a | warnings.filterwarnings("error") |
---|
1941 | n/a | func() |
---|
1942 | n/a | support.gc_collect() |
---|
1943 | n/a | self.assertIn("was never awaited", stderr.getvalue()) |
---|
1944 | n/a | |
---|
1945 | n/a | |
---|
1946 | n/a | class CoroAsyncIOCompatTest(unittest.TestCase): |
---|
1947 | n/a | |
---|
1948 | n/a | def test_asyncio_1(self): |
---|
1949 | n/a | # asyncio cannot be imported when Python is compiled without thread |
---|
1950 | n/a | # support |
---|
1951 | n/a | asyncio = support.import_module('asyncio') |
---|
1952 | n/a | |
---|
1953 | n/a | class MyException(Exception): |
---|
1954 | n/a | pass |
---|
1955 | n/a | |
---|
1956 | n/a | buffer = [] |
---|
1957 | n/a | |
---|
1958 | n/a | class CM: |
---|
1959 | n/a | async def __aenter__(self): |
---|
1960 | n/a | buffer.append(1) |
---|
1961 | n/a | await asyncio.sleep(0.01) |
---|
1962 | n/a | buffer.append(2) |
---|
1963 | n/a | return self |
---|
1964 | n/a | |
---|
1965 | n/a | async def __aexit__(self, exc_type, exc_val, exc_tb): |
---|
1966 | n/a | await asyncio.sleep(0.01) |
---|
1967 | n/a | buffer.append(exc_type.__name__) |
---|
1968 | n/a | |
---|
1969 | n/a | async def f(): |
---|
1970 | n/a | async with CM() as c: |
---|
1971 | n/a | await asyncio.sleep(0.01) |
---|
1972 | n/a | raise MyException |
---|
1973 | n/a | buffer.append('unreachable') |
---|
1974 | n/a | |
---|
1975 | n/a | loop = asyncio.new_event_loop() |
---|
1976 | n/a | asyncio.set_event_loop(loop) |
---|
1977 | n/a | try: |
---|
1978 | n/a | loop.run_until_complete(f()) |
---|
1979 | n/a | except MyException: |
---|
1980 | n/a | pass |
---|
1981 | n/a | finally: |
---|
1982 | n/a | loop.close() |
---|
1983 | n/a | asyncio.set_event_loop(None) |
---|
1984 | n/a | |
---|
1985 | n/a | self.assertEqual(buffer, [1, 2, 'MyException']) |
---|
1986 | n/a | |
---|
1987 | n/a | |
---|
1988 | n/a | class SysSetCoroWrapperTest(unittest.TestCase): |
---|
1989 | n/a | |
---|
1990 | n/a | def test_set_wrapper_1(self): |
---|
1991 | n/a | async def foo(): |
---|
1992 | n/a | return 'spam' |
---|
1993 | n/a | |
---|
1994 | n/a | wrapped = None |
---|
1995 | n/a | def wrap(gen): |
---|
1996 | n/a | nonlocal wrapped |
---|
1997 | n/a | wrapped = gen |
---|
1998 | n/a | return gen |
---|
1999 | n/a | |
---|
2000 | n/a | self.assertIsNone(sys.get_coroutine_wrapper()) |
---|
2001 | n/a | |
---|
2002 | n/a | sys.set_coroutine_wrapper(wrap) |
---|
2003 | n/a | self.assertIs(sys.get_coroutine_wrapper(), wrap) |
---|
2004 | n/a | try: |
---|
2005 | n/a | f = foo() |
---|
2006 | n/a | self.assertTrue(wrapped) |
---|
2007 | n/a | |
---|
2008 | n/a | self.assertEqual(run_async(f), ([], 'spam')) |
---|
2009 | n/a | finally: |
---|
2010 | n/a | sys.set_coroutine_wrapper(None) |
---|
2011 | n/a | |
---|
2012 | n/a | self.assertIsNone(sys.get_coroutine_wrapper()) |
---|
2013 | n/a | |
---|
2014 | n/a | wrapped = None |
---|
2015 | n/a | with silence_coro_gc(): |
---|
2016 | n/a | foo() |
---|
2017 | n/a | self.assertFalse(wrapped) |
---|
2018 | n/a | |
---|
2019 | n/a | def test_set_wrapper_2(self): |
---|
2020 | n/a | self.assertIsNone(sys.get_coroutine_wrapper()) |
---|
2021 | n/a | with self.assertRaisesRegex(TypeError, "callable expected, got int"): |
---|
2022 | n/a | sys.set_coroutine_wrapper(1) |
---|
2023 | n/a | self.assertIsNone(sys.get_coroutine_wrapper()) |
---|
2024 | n/a | |
---|
2025 | n/a | def test_set_wrapper_3(self): |
---|
2026 | n/a | async def foo(): |
---|
2027 | n/a | return 'spam' |
---|
2028 | n/a | |
---|
2029 | n/a | def wrapper(coro): |
---|
2030 | n/a | async def wrap(coro): |
---|
2031 | n/a | return await coro |
---|
2032 | n/a | return wrap(coro) |
---|
2033 | n/a | |
---|
2034 | n/a | sys.set_coroutine_wrapper(wrapper) |
---|
2035 | n/a | try: |
---|
2036 | n/a | with silence_coro_gc(), self.assertRaisesRegex( |
---|
2037 | n/a | RuntimeError, |
---|
2038 | n/a | r"coroutine wrapper.*\.wrapper at 0x.*attempted to " |
---|
2039 | n/a | r"recursively wrap .* wrap .*"): |
---|
2040 | n/a | |
---|
2041 | n/a | foo() |
---|
2042 | n/a | finally: |
---|
2043 | n/a | sys.set_coroutine_wrapper(None) |
---|
2044 | n/a | |
---|
2045 | n/a | def test_set_wrapper_4(self): |
---|
2046 | n/a | @types.coroutine |
---|
2047 | n/a | def foo(): |
---|
2048 | n/a | return 'spam' |
---|
2049 | n/a | |
---|
2050 | n/a | wrapped = None |
---|
2051 | n/a | def wrap(gen): |
---|
2052 | n/a | nonlocal wrapped |
---|
2053 | n/a | wrapped = gen |
---|
2054 | n/a | return gen |
---|
2055 | n/a | |
---|
2056 | n/a | sys.set_coroutine_wrapper(wrap) |
---|
2057 | n/a | try: |
---|
2058 | n/a | foo() |
---|
2059 | n/a | self.assertIs( |
---|
2060 | n/a | wrapped, None, |
---|
2061 | n/a | "generator-based coroutine was wrapped via " |
---|
2062 | n/a | "sys.set_coroutine_wrapper") |
---|
2063 | n/a | finally: |
---|
2064 | n/a | sys.set_coroutine_wrapper(None) |
---|
2065 | n/a | |
---|
2066 | n/a | |
---|
2067 | n/a | class CAPITest(unittest.TestCase): |
---|
2068 | n/a | |
---|
2069 | n/a | def test_tp_await_1(self): |
---|
2070 | n/a | from _testcapi import awaitType as at |
---|
2071 | n/a | |
---|
2072 | n/a | async def foo(): |
---|
2073 | n/a | future = at(iter([1])) |
---|
2074 | n/a | return (await future) |
---|
2075 | n/a | |
---|
2076 | n/a | self.assertEqual(foo().send(None), 1) |
---|
2077 | n/a | |
---|
2078 | n/a | def test_tp_await_2(self): |
---|
2079 | n/a | # Test tp_await to __await__ mapping |
---|
2080 | n/a | from _testcapi import awaitType as at |
---|
2081 | n/a | future = at(iter([1])) |
---|
2082 | n/a | self.assertEqual(next(future.__await__()), 1) |
---|
2083 | n/a | |
---|
2084 | n/a | def test_tp_await_3(self): |
---|
2085 | n/a | from _testcapi import awaitType as at |
---|
2086 | n/a | |
---|
2087 | n/a | async def foo(): |
---|
2088 | n/a | future = at(1) |
---|
2089 | n/a | return (await future) |
---|
2090 | n/a | |
---|
2091 | n/a | with self.assertRaisesRegex( |
---|
2092 | n/a | TypeError, "__await__.*returned non-iterator of type 'int'"): |
---|
2093 | n/a | self.assertEqual(foo().send(None), 1) |
---|
2094 | n/a | |
---|
2095 | n/a | |
---|
2096 | n/a | if __name__=="__main__": |
---|
2097 | n/a | unittest.main() |
---|