Răsfoiți Sursa

fix a bug of `traceback`

blueloveTH 2 ani în urmă
părinte
comite
4df6d4fdb1
3 a modificat fișierele cu 19 adăugiri și 13 ștergeri
  1. 0 6
      src/ceval.cpp
  2. 13 5
      src/vm.cpp
  3. 6 2
      tests/80_traceback.py

+ 0 - 6
src/ceval.cpp

@@ -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

+ 13 - 5
src/vm.cpp

@@ -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();
 }

+ 6 - 2
tests/80_traceback.py

@@ -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