mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 12:40:15 +01:00
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.
This commit is contained in:
@@ -25,6 +25,20 @@ def gen3():
|
||||
g3 = gen3()
|
||||
print(next(g3))
|
||||
try:
|
||||
g3.throw(StopIteration)
|
||||
g3.throw(KeyError)
|
||||
except KeyError:
|
||||
print('got KeyError from downstream!')
|
||||
|
||||
# case where a thrown exception is caught and stops the generator
|
||||
def gen4():
|
||||
try:
|
||||
yield 1
|
||||
yield 2
|
||||
except:
|
||||
pass
|
||||
g4 = gen4()
|
||||
print(next(g4))
|
||||
try:
|
||||
g4.throw(ValueError)
|
||||
except StopIteration:
|
||||
print('got StopIteration from downstream!')
|
||||
print('got StopIteration')
|
||||
|
||||
Reference in New Issue
Block a user