@@ -559,17 +559,18 @@ def py_emit(node: ast.AsyncWith, ctx: Context):
559
559
"""
560
560
title: async with.
561
561
prepare:
562
- >>> import unittest
563
- >>> self: unittest.TestCase
564
- >>> from asyncio import sleep, Task, get_event_loop
565
- >>> class AsyncYieldFrom :
566
- >>> def __init__(self, obj) :
567
- >>> self.obj = obj
568
- >>>
569
- >>> def __await__(self):
570
- >>> yield from self.obj
571
- >>>
562
+ >>> def run_async(coro):
563
+ >>> buffer = []
564
+ >>> result = None
565
+ >>> while True :
566
+ >>> try :
567
+ >>> buffer.append(coro.send(None))
568
+ >>> except StopIteration as ex:
569
+ >>> result = ex.args[0] if ex.args else None
570
+ >>> break
571
+ >>> return buffer, result
572
572
>>> class Manager:
573
+ >>> name: str
573
574
>>> def __init__(self, name):
574
575
>>> self.name = name
575
576
>>>
@@ -584,14 +585,22 @@ def py_emit(node: ast.AsyncWith, ctx: Context):
584
585
>>>
585
586
>>> if self.name == 'B':
586
587
>>> return True
588
+ >>>
589
+ >>> class AsyncYieldFrom:
590
+ >>> obj = None
591
+ >>> def __init__(self, obj):
592
+ >>> self.obj = obj
593
+ >>>
594
+ >>> def __await__(self):
595
+ >>> yield from self.obj
587
596
test:
588
597
>>> async def foo():
589
- >>> async with Manager("A") as a, Manager("B") as b:
590
- >>> await AsyncYieldFrom([('managers', a.name, b.name)])
591
- >>> 1/0
592
- >>>
598
+ >>> async with Manager("A") as a, Manager("B") as b:
599
+ >>> await AsyncYieldFrom([('managers', a.name, b.name)])
600
+ >>> print('fuck')
593
601
>>> f = foo()
594
- >>> result = get_event_loop().run_until_complete(f)
602
+ >>> result, _ = run_async(f)
603
+ >>> print('fuck')
595
604
>>> assert result == ['enter-1-A', 'enter-2-A', 'enter-1-B', 'enter-2-B',
596
605
>>> ('managers', 'A', 'B'),
597
606
>>> 'exit-1-B', 'exit-2-B', 'exit-1-A', 'exit-2-A']
@@ -673,6 +682,7 @@ def emit_async_with_pop_iter():
673
682
674
683
byte_code .extend ([
675
684
WITH_CLEANUP_START (),
685
+ GET_AWAITABLE (),
676
686
LOAD_CONST (None ),
677
687
YIELD_FROM (),
678
688
WITH_CLEANUP_FINISH (),
0 commit comments