fix a bug of traceback

This commit is contained in:
blueloveTH 2023-10-08 01:36:11 +08:00
parent b64db837f5
commit 4df6d4fdb1
3 changed files with 19 additions and 13 deletions

View File

@ -760,12 +760,6 @@ __NEXT_STEP:;
PK_UNUSED(e);
PyObject* obj = POPX();
Exception& _e = CAST(Exception&, obj);
int actual_ip = frame->_ip;
if(_e._ip_on_error >= 0 && _e._code_on_error == (void*)frame->co) actual_ip = _e._ip_on_error;
int current_line = frame->co->lines[actual_ip]; // current line
auto current_f_name = frame->co->name.sv(); // current function name
if(frame->_callable == nullptr) current_f_name = ""; // not in a function
_e.st_push(frame->co->src->snapshot(current_line, nullptr, current_f_name));
_pop_frame();
if(callstack.empty()){
#if PK_DEBUG_FULL_EXCEPTION

View File

@ -1082,13 +1082,21 @@ void VM::_error(Exception e){
}
void VM::_raise(bool re_raise){
Frame* top = top_frame().get();
Frame* frame = top_frame().get();
Exception& e = PK_OBJ_GET(Exception, s_data.top());
if(!re_raise){
Exception& e = PK_OBJ_GET(Exception, s_data.top());
e._ip_on_error = top->_ip;
e._code_on_error = (void*)top->co;
e._ip_on_error = frame->_ip;
e._code_on_error = (void*)frame->co;
}
bool ok = top->jump_to_exception_handler();
bool ok = frame->jump_to_exception_handler();
int actual_ip = frame->_ip;
if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error;
int current_line = frame->co->lines[actual_ip]; // current line
auto current_f_name = frame->co->name.sv(); // current function name
if(frame->_callable == nullptr) current_f_name = ""; // not in a function
e.st_push(frame->co->src->snapshot(current_line, nullptr, current_f_name));
if(ok) throw HandledException();
else throw UnhandledException();
}

View File

@ -6,5 +6,9 @@ try:
except KeyError:
s = traceback.format_exc()
assert s == r'''Traceback (most recent call last):
KeyError: 6'''
ok = s == '''Traceback (most recent call last):
File "80_traceback.py", line 5
b = a[6]
KeyError: 6'''
assert ok, s