Commit a16e8982 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Slightly change generator StopIteration handling

Unconditionally raise a StopIteration when trying to iterate
a finished generator, regardless of what originally stopped
it from iterating.
parent 6f84f646
...@@ -124,7 +124,7 @@ static void generatorSendInternal(BoxedGenerator* self, Box* v) { ...@@ -124,7 +124,7 @@ static void generatorSendInternal(BoxedGenerator* self, Box* v) {
// check if the generator already exited // check if the generator already exited
if (self->entryExited) { if (self->entryExited) {
freeGeneratorStack(self); freeGeneratorStack(self);
return; raiseExcHelper(StopIteration, (const char*)nullptr);
} }
self->returnValue = v; self->returnValue = v;
...@@ -189,9 +189,10 @@ Box* generatorSend(Box* s, Box* v) { ...@@ -189,9 +189,10 @@ Box* generatorSend(Box* s, Box* v) {
// exc. // exc.
assert(self->exception.type == NULL || self->exception.matches(StopIteration)); assert(self->exception.type == NULL || self->exception.matches(StopIteration));
ExcInfo old_exc = self->exception; ExcInfo old_exc = self->exception;
self->exception = excInfoForRaise(StopIteration, None, None); // Clear the exception for GC purposes:
self->exception = ExcInfo(nullptr, nullptr, nullptr);
if (old_exc.type == NULL) if (old_exc.type == NULL)
old_exc = self->exception; raiseExcHelper(StopIteration, (const char*)nullptr);
raiseRaw(old_exc); raiseRaw(old_exc);
} }
......
...@@ -108,9 +108,23 @@ g10.next() ...@@ -108,9 +108,23 @@ g10.next()
try: try:
g10.next() g10.next()
except StopIteration as e: except StopIteration as e:
print "Cought exc1:", type(e), e print "Caught exc1:", type(e), e
try: try:
g10.next() g10.next()
except StopIteration as e: except StopIteration as e:
print "Cought exc2:", type(e), e print "Caught exc2:", type(e), e
def f():
yield 1/0
g = f()
try:
g.next()
except Exception as e:
print type(e), e # ZeroDivisionError
try:
g.next()
except Exception as e:
print type(e), e # StopIteration
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment