blueloveTH 6 miesięcy temu
rodzic
commit
4a81e975c8
2 zmienionych plików z 21 dodań i 4 usunięć
  1. 10 4
      src/interpreter/frame.c
  2. 11 0
      tests/28_exception.py

+ 10 - 4
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) {

+ 11 - 0
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():