diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index a3bdb367..78a93d79 100644 --- a/src/interpreter/frame.c +++ b/src/interpreter/frame.c @@ -61,10 +61,16 @@ void Frame__delete(py_Frame* self) { } int Frame__goto_exception_handler(py_Frame* self, ValueStack* value_stack, py_Ref exc) { - if(self->exc_stack.length == 0) return -1; - FrameExcInfo* info = &c11_vector__back(FrameExcInfo, &self->exc_stack); - value_stack->sp = (self->p0 + info->offset); // unwind the stack - return c11__at(CodeBlock, &self->co->blocks, info->iblock)->end; + FrameExcInfo* p = self->exc_stack.data; + for(int i = self->exc_stack.length - 1; i >= 0; i--) { + if(py_isnil(&p[i].exc)) { + value_stack->sp = (self->p0 + p[i].offset); // unwind the stack + return c11__at(CodeBlock, &self->co->blocks, p[i].iblock)->end; + } else { + self->exc_stack.length--; + } + } + return -1; } void Frame__begin_try(py_Frame* self, py_TValue* sp) { diff --git a/tests/28_exception.py b/tests/28_exception.py index 6c657593..9c174312 100644 --- a/tests/28_exception.py +++ b/tests/28_exception.py @@ -179,6 +179,17 @@ for i in range(10): assert e.args[0] == i assert a == list(range(10)) +# inner exc +x = 0 +try: + try: + [][1] + except: + raise KeyError +except KeyError: + x = 5 +assert x == 5 + """ # finally, only def finally_only():