circuitpython/tests/basics/generator_pep479.py
Damien George 3f6ffe059f py/objgenerator: Implement PEP479, StopIteration convs to RuntimeError.
This commit implements PEP479 which disallows raising StopIteration inside
a generator to signal that it should be finished.  Instead, the generator
should simply return when it is complete.

See https://www.python.org/dev/peps/pep-0479/ for details.
2018-09-20 15:36:59 +10:00

30 lines
617 B
Python

# tests for correct PEP479 behaviour (introduced in Python 3.5)
# basic case: StopIteration is converted into a RuntimeError
def gen():
yield 1
raise StopIteration
g = gen()
print(next(g))
try:
next(g)
except RuntimeError:
print('RuntimeError')
# trying to continue a failed generator now raises StopIteration
try:
next(g)
except StopIteration:
print('StopIteration')
# throwing a StopIteration which is uncaught will be converted into a RuntimeError
def gen():
yield 1
yield 2
g = gen()
print(next(g))
try:
g.throw(StopIteration)
except RuntimeError:
print('RuntimeError')