diff --git a/src/error.h b/src/error.h index eecf56d1..16fe7bbb 100644 --- a/src/error.h +++ b/src/error.h @@ -9,6 +9,7 @@ struct NeedMoreLines { struct HandledException {}; struct UnhandledException {}; +struct ToBeRaisedException {}; enum CompileMode { EXEC_MODE, diff --git a/src/vm.h b/src/vm.h index 9b47da7c..e43c0ea8 100644 --- a/src/vm.h +++ b/src/vm.h @@ -29,7 +29,7 @@ class VM { while(frame->has_next_bytecode()){ const Bytecode& byte = frame->next_bytecode(); // if(frame->_module != builtins){ - // printf("%d: %s (%d) %s\n", frame->get_ip(), OP_NAMES[byte.op], byte.arg, frame->stack_info().c_str()); + // printf("%d: %s (%d) %s\n", frame->_ip, OP_NAMES[byte.op], byte.arg, frame->stack_info().c_str()); // } switch (byte.op) { @@ -558,10 +558,10 @@ public: ret = run_frame(frame); if(ret != __py2py_call_signal){ + callstack.pop(); if(frame->id == base_id){ // [ frameBase<- ] - break; + return ret; }else{ - callstack.pop(); frame = callstack.top().get(); frame->push(ret); } @@ -575,20 +575,15 @@ public: _Exception& _e = PyException_AS_C(obj); _e.st_push(frame->curr_snapshot()); callstack.pop(); - - if(!callstack.empty()){ - frame = callstack.top().get(); - if(frame->id < base_id) throw e; - frame->push(obj); - need_raise = true; - continue; - } - throw _e; + if(callstack.empty()) throw _e; + frame = callstack.top().get(); + frame->push(obj); + if(frame->id < base_id) throw ToBeRaisedException(); + need_raise = true; + }catch(ToBeRaisedException& e){ + need_raise = true; } } - - callstack.pop(); - return ret; } PyVar new_user_type_object(PyVar mod, _Str name, PyVar base){ diff --git a/tests/_exception.py b/tests/_exception.py index 18403531..46f8f18a 100644 --- a/tests/_exception.py +++ b/tests/_exception.py @@ -1,16 +1,27 @@ +class A: + def __getitem__(self, i): + raise KeyError(i) + try: - raise KeyError + a = A() + b = a[1] except: - print("exception caught") -print(123) + print("PASS 01") + +try: + a = {'1': 3, 4: None} + x = a[1] +except: + print("PASS 02") +assert True def f(): try: raise KeyError('foo') except A: # will fail to catch - print("xx") + assert False except: - print("exception caught") - print(123) + print("PASS 03") + assert True f() \ No newline at end of file