mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
kill generator on body raise
This commit is contained in:
parent
51ab011253
commit
ca7fb15787
10
src/iter.cpp
10
src/iter.cpp
@ -49,7 +49,15 @@ namespace pkpy{
|
||||
for(PyObject* obj: s_backup) frame._s->push(obj);
|
||||
s_backup.clear();
|
||||
vm->callstack.push(std::move(frame));
|
||||
PyObject* ret = vm->_run_top_frame();
|
||||
|
||||
PyObject* ret;
|
||||
try{
|
||||
ret = vm->_run_top_frame();
|
||||
}catch(...){
|
||||
state = 2; // end this generator immediately when an exception is thrown
|
||||
throw;
|
||||
}
|
||||
|
||||
if(ret == PY_OP_YIELD){
|
||||
// backup the context
|
||||
frame = std::move(vm->callstack.top());
|
||||
|
@ -45,4 +45,23 @@ def f(n):
|
||||
yield j
|
||||
|
||||
t = f(3)
|
||||
assert list(t) == [0, 1, 0, 2, 0, 1]
|
||||
assert list(t) == [0, 1, 0, 2, 0, 1]
|
||||
|
||||
def f(n):
|
||||
for i in range(n):
|
||||
if i == n-1:
|
||||
raise ValueError
|
||||
yield i
|
||||
|
||||
t = f(3)
|
||||
t = iter(t)
|
||||
assert next(t) == 0
|
||||
assert next(t) == 1
|
||||
|
||||
try:
|
||||
next(t)
|
||||
exit(1)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
assert next(t) == StopIteration
|
Loading…
x
Reference in New Issue
Block a user