Skip to content

Commit 4f76837

Browse files
committed
vm,py: fix generators called after end of code
1 parent 8e7ded6 commit 4f76837

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

py/generator.go

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func (it *Generator) Send(arg Object) Object {
8585
panic(ExceptionNewf(TypeError, "can't send non-None value to a just-started generator"))
8686
}
8787
} else {
88+
// If already returned a non yield value then stop
89+
if !it.Frame.Yielded {
90+
panic(StopIteration)
91+
}
8892
// Push arg onto the frame's value stack
8993
it.Frame.Stack = append(it.Frame.Stack, arg)
9094
}

vm/eval.go

+16
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,22 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
16391639
// }
16401640
// }()
16411641

1642+
// FIXME
1643+
// if (co->co_flags & CO_GENERATOR) {
1644+
// if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1645+
// /* We were in an except handler when we left,
1646+
// restore the exception state which was put aside
1647+
// (see YIELD_VALUE). */
1648+
// swap_exc_state(tstate, f);
1649+
// }
1650+
// else
1651+
// save_exc_state(tstate, f);
1652+
// }
1653+
1654+
if int(frame.Lasti) >= len(frame.Code.Code) {
1655+
panic(py.ExceptionNewf(py.SystemError, "vm: instruction out of range - code most likely finished already"))
1656+
}
1657+
16421658
var opcode OpCode
16431659
var arg int32
16441660
for vm.why == whyNot {

vm/tests/generators.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ def g1():
77
assert next(i) == 1
88
assert next(i) == 2
99
assert next(i) == 3
10-
ok = False
11-
try:
12-
next(i)
13-
except StopIteration:
14-
ok = True
15-
assert ok, "StopIteration not raised"
10+
for _ in (1, 2):
11+
ok = False
12+
try:
13+
next(i)
14+
except StopIteration:
15+
ok = True
16+
assert ok, "StopIteration not raised"
1617

1718
doc="generator 2"
1819
def g2():

0 commit comments

Comments
 (0)