kopia lustrzana https://github.com/micropython/micropython
tests: Add testcases for yield from.
rodzic
55234f4617
commit
3c2b2acd8c
|
@ -0,0 +1,87 @@
|
|||
def gen():
|
||||
yield 1
|
||||
yield 2
|
||||
yield 3
|
||||
yield 4
|
||||
|
||||
def gen2():
|
||||
yield -1
|
||||
print((yield from gen()))
|
||||
yield 10
|
||||
yield 11
|
||||
|
||||
g = gen2()
|
||||
print(next(g))
|
||||
print(next(g))
|
||||
g.close()
|
||||
try:
|
||||
print(next(g))
|
||||
except StopIteration:
|
||||
print("StopIteration")
|
||||
|
||||
|
||||
# Now variation of same test, but with leaf generator
|
||||
# swallowing GeneratorExit exception - its upstream gen
|
||||
# generator should still receive one.
|
||||
def gen3():
|
||||
yield 1
|
||||
try:
|
||||
yield 2
|
||||
except GeneratorExit:
|
||||
print("leaf caught GeneratorExit and swallowed it")
|
||||
return
|
||||
yield 3
|
||||
yield 4
|
||||
|
||||
def gen4():
|
||||
yield -1
|
||||
try:
|
||||
print((yield from gen3()))
|
||||
except GeneratorExit:
|
||||
print("delegating caught GeneratorExit")
|
||||
raise
|
||||
yield 10
|
||||
yield 11
|
||||
|
||||
g = gen4()
|
||||
print(next(g))
|
||||
print(next(g))
|
||||
print(next(g))
|
||||
g.close()
|
||||
try:
|
||||
print(next(g))
|
||||
except StopIteration:
|
||||
print("StopIteration")
|
||||
|
||||
|
||||
# Yet another variation - leaf generator gets GeneratorExit,
|
||||
# but raises StopIteration instead. This still should close chain properly.
|
||||
def gen5():
|
||||
yield 1
|
||||
try:
|
||||
yield 2
|
||||
except GeneratorExit:
|
||||
print("leaf caught GeneratorExit and raised StopIteration instead")
|
||||
raise StopIteration(123)
|
||||
yield 3
|
||||
yield 4
|
||||
|
||||
def gen6():
|
||||
yield -1
|
||||
try:
|
||||
print((yield from gen5()))
|
||||
except GeneratorExit:
|
||||
print("delegating caught GeneratorExit")
|
||||
raise
|
||||
yield 10
|
||||
yield 11
|
||||
|
||||
g = gen6()
|
||||
print(next(g))
|
||||
print(next(g))
|
||||
print(next(g))
|
||||
g.close()
|
||||
try:
|
||||
print(next(g))
|
||||
except StopIteration:
|
||||
print("StopIteration")
|
|
@ -0,0 +1,13 @@
|
|||
def gen():
|
||||
yield 1
|
||||
yield 2
|
||||
raise ValueError
|
||||
|
||||
def gen2():
|
||||
try:
|
||||
print((yield from gen()))
|
||||
except ValueError:
|
||||
print("caught ValueError from downstream")
|
||||
|
||||
g = gen2()
|
||||
print(list(g))
|
|
@ -0,0 +1,14 @@
|
|||
def gen():
|
||||
print("sent:", (yield 1))
|
||||
yield 2
|
||||
|
||||
def gen2():
|
||||
print((yield from gen()))
|
||||
|
||||
g = gen2()
|
||||
next(g)
|
||||
print("yielded:", g.send("val"))
|
||||
try:
|
||||
next(g)
|
||||
except StopIteration:
|
||||
print("StopIteration")
|
|
@ -0,0 +1,19 @@
|
|||
def gen():
|
||||
try:
|
||||
yield 1
|
||||
except ValueError:
|
||||
print("got ValueError from upstream!")
|
||||
yield "str1"
|
||||
raise TypeError
|
||||
|
||||
def gen2():
|
||||
print((yield from gen()))
|
||||
|
||||
g = gen2()
|
||||
print(next(g))
|
||||
print(g.throw(ValueError))
|
||||
try:
|
||||
print(next(g))
|
||||
except TypeError:
|
||||
print("got TypeError from downstream!")
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
# Case of terminating subgen using return with value
|
||||
def gen():
|
||||
yield 1
|
||||
yield 2
|
||||
return 3
|
||||
|
||||
def gen2():
|
||||
print("here1")
|
||||
print((yield from gen()))
|
||||
print("here2")
|
||||
|
||||
g = gen2()
|
||||
print(list(g))
|
||||
|
||||
|
||||
# Like above, but terminate subgen using StopIteration
|
||||
def gen3():
|
||||
yield 1
|
||||
yield 2
|
||||
raise StopIteration
|
||||
|
||||
def gen4():
|
||||
print("here1")
|
||||
print((yield from gen3()))
|
||||
print("here2")
|
||||
|
||||
g = gen4()
|
||||
print(list(g))
|
||||
|
||||
# Like above, but terminate subgen using StopIteration with value
|
||||
def gen5():
|
||||
yield 1
|
||||
yield 2
|
||||
raise StopIteration(123)
|
||||
|
||||
def gen6():
|
||||
print("here1")
|
||||
print((yield from gen5()))
|
||||
print("here2")
|
||||
|
||||
g = gen6()
|
||||
print(list(g))
|
Ładowanie…
Reference in New Issue