micropython/tests/basics/gen_yield_from.py
Damien George 22ade2f5c4 py/vm: Fix case of handling raised StopIteration within yield from.
This patch concerns the handling of an NLR-raised StopIteration, raised
during a call to mp_resume() which is handling the yield from opcode.

Previously, commit 6738c1dded introduced code
to handle this case, along with a test.  It seems that it was lucky that
the test worked because the code did not correctly handle the stack pointer
(sp).

Furthermore, commit 79d996a57b improved the
way mp_resume() propagated certain exceptions: it changed raising an NLR
value to returning MP_VM_RETURN_EXCEPTION.  This change meant that the
test introduced in gen_yield_from_ducktype.py was no longer hitting the
code introduced in 6738c1dded.

The patch here does two things:

1. Fixes the handling of sp in the VM for the case that yield from is
   interrupted by a StopIteration raised via NLR.

2. Introduces a new test to check this handling of sp and re-covers the
   code in the VM.
2018-02-27 15:39:31 +11:00

56 lines
949 B
Python

# 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))
# StopIteration from within a Python function, within a native iterator (map), within a yield from
def gen7(x):
if x < 3:
return x
else:
raise StopIteration(444)
def gen8():
print((yield from map(gen7, range(100))))
g = gen8()
print(list(g))