Skip to content

Commit fc37ff8

Browse files
committed
add command get await
1 parent dd34e57 commit fc37ff8

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

yapypy/extended_python/emit_impl/control.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -559,17 +559,18 @@ def py_emit(node: ast.AsyncWith, ctx: Context):
559559
"""
560560
title: async with.
561561
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
572572
>>> class Manager:
573+
>>> name: str
573574
>>> def __init__(self, name):
574575
>>> self.name = name
575576
>>>
@@ -584,14 +585,22 @@ def py_emit(node: ast.AsyncWith, ctx: Context):
584585
>>>
585586
>>> if self.name == 'B':
586587
>>> 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
587596
test:
588597
>>> 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')
593601
>>> f = foo()
594-
>>> result = get_event_loop().run_until_complete(f)
602+
>>> result, _ = run_async(f)
603+
>>> print('fuck')
595604
>>> assert result == ['enter-1-A', 'enter-2-A', 'enter-1-B', 'enter-2-B',
596605
>>> ('managers', 'A', 'B'),
597606
>>> 'exit-1-B', 'exit-2-B', 'exit-1-A', 'exit-2-A']
@@ -673,6 +682,7 @@ def emit_async_with_pop_iter():
673682

674683
byte_code.extend([
675684
WITH_CLEANUP_START(),
685+
GET_AWAITABLE(),
676686
LOAD_CONST(None),
677687
YIELD_FROM(),
678688
WITH_CLEANUP_FINISH(),

0 commit comments

Comments
 (0)