kill generator on body raise

This commit is contained in:
blueloveTH 2023-09-07 00:35:47 +08:00
parent 51ab011253
commit ca7fb15787
2 changed files with 29 additions and 2 deletions

View File

@ -49,7 +49,15 @@ namespace pkpy{
for(PyObject* obj: s_backup) frame._s->push(obj); for(PyObject* obj: s_backup) frame._s->push(obj);
s_backup.clear(); s_backup.clear();
vm->callstack.push(std::move(frame)); 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){ if(ret == PY_OP_YIELD){
// backup the context // backup the context
frame = std::move(vm->callstack.top()); frame = std::move(vm->callstack.top());

View File

@ -46,3 +46,22 @@ def f(n):
t = f(3) 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