Commit 66bfd64a authored by Dylan Trotter's avatar Dylan Trotter

Generators now return nil when exhausted instead of raising StopIteration.

parent e06bed33
...@@ -77,11 +77,8 @@ class Writer(object): ...@@ -77,11 +77,8 @@ class Writer(object):
# Assume that body is aligned with goto labels. # Assume that body is aligned with goto labels.
self.write(body) self.write(body)
with self.indent_block(): with self.indent_block():
if block_.is_generator: self.write('return {}, nil'.format(
self.write('return nil, πF.Raise(' 'nil' if block_.is_generator else 'πg.None'))
'πg.StopIterationType.ToObject(), nil, nil)')
else:
self.write('return πg.None, nil')
self.write('})') self.write('})')
def write_import_block(self, imports): def write_import_block(self, imports):
......
...@@ -86,6 +86,9 @@ func (g *Generator) resume(f *Frame, sendValue *Object) (*Object, *BaseException ...@@ -86,6 +86,9 @@ func (g *Generator) resume(f *Frame, sendValue *Object) (*Object, *BaseException
} }
result, raised := g.block.execInternal(g.frame, sendValue) result, raised := g.block.execInternal(g.frame, sendValue)
g.mutex.Lock() g.mutex.Lock()
if result == nil && raised == nil {
raised = f.Raise(StopIterationType.ToObject(), nil, nil)
}
if raised == nil { if raised == nil {
g.state = generatorStateReady g.state = generatorStateReady
} else { } else {
......
...@@ -29,7 +29,7 @@ func TestGeneratorNext(t *testing.T) { ...@@ -29,7 +29,7 @@ func TestGeneratorNext(t *testing.T) {
}) })
recursive = NewGenerator(recursiveBlock, NewDict()).ToObject() recursive = NewGenerator(recursiveBlock, NewDict()).ToObject()
empty := NewBlock(func(f *Frame, _ *Object) (*Object, *BaseException) { empty := NewBlock(func(f *Frame, _ *Object) (*Object, *BaseException) {
return nil, f.Raise(StopIterationType.ToObject(), nil, nil) return nil, nil
}) })
exhausted := NewGenerator(empty, NewDict()).ToObject() exhausted := NewGenerator(empty, NewDict()).ToObject()
mustNotRaise(ListType.Call(newFrame(nil), Args{exhausted}, nil)) mustNotRaise(ListType.Call(newFrame(nil), Args{exhausted}, nil))
...@@ -46,7 +46,7 @@ func TestGeneratorNext(t *testing.T) { ...@@ -46,7 +46,7 @@ func TestGeneratorNext(t *testing.T) {
func TestGeneratorSend(t *testing.T) { func TestGeneratorSend(t *testing.T) {
empty := NewBlock(func(f *Frame, _ *Object) (*Object, *BaseException) { empty := NewBlock(func(f *Frame, _ *Object) (*Object, *BaseException) {
return nil, f.Raise(StopIterationType.ToObject(), nil, nil) return nil, nil
}) })
cases := []invokeTestCase{ cases := []invokeTestCase{
invokeTestCase{args: wrapArgs(NewGenerator(empty, NewDict()), 123), wantExc: mustCreateException(TypeErrorType, "can't send non-None value to a just-started generator")}, invokeTestCase{args: wrapArgs(NewGenerator(empty, NewDict()), 123), wantExc: mustCreateException(TypeErrorType, "can't send non-None value to a just-started generator")},
...@@ -78,7 +78,7 @@ func TestGeneratorSimple(t *testing.T) { ...@@ -78,7 +78,7 @@ func TestGeneratorSimple(t *testing.T) {
f.PushCheckpoint(2) f.PushCheckpoint(2)
return NewStr("bar").ToObject(), nil return NewStr("bar").ToObject(), nil
Yield2: Yield2:
return nil, f.Raise(StopIterationType.ToObject(), nil, nil) return nil, nil
}) })
cas := &invokeTestCase{ cas := &invokeTestCase{
args: wrapArgs(NewGenerator(b, NewDict())), args: wrapArgs(NewGenerator(b, NewDict())),
......
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