Merge pull request #1534 from RustPython/coolreader18/yieldfrom-fix

Fix the `yield from` expression
This commit is contained in:
Windel Bouwman
2019-10-17 07:15:44 +02:00
committed by GitHub
6 changed files with 162 additions and 52 deletions

View File

@@ -88,3 +88,46 @@ l = list(g)
# print(l)
assert l == [99]
assert r == ['a', 66, None]
def binary(n):
if n <= 1:
return 1
l = yield from binary(n - 1)
r = yield from binary(n - 1)
return l + 1 + r
with assert_raises(StopIteration):
try:
next(binary(5))
except StopIteration as stopiter:
# TODO: StopIteration.value
assert stopiter.args[0] == 31
raise
class SpamException(Exception):
pass
l = []
def writer():
while True:
try:
w = (yield)
except SpamException:
l.append('***')
else:
l.append(f'>> {w}')
def wrapper(coro):
yield from coro
w = writer()
wrap = wrapper(w)
wrap.send(None) # "prime" the coroutine
for i in [0, 1, 2, 'spam', 4]:
if i == 'spam':
wrap.throw(SpamException)
else:
wrap.send(i)
assert l == ['>> 0', '>> 1', '>> 2', '***', '>> 4']