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:
Damien George
2018-09-14 00:44:06 +10:00
parent 17f7c683d2
commit 3f6ffe059f
12 changed files with 63 additions and 98 deletions

View File

@@ -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')