blueloveTH 2 years ago
parent
commit
5d3d9b2dec
5 changed files with 12 additions and 7 deletions
  1. 4 2
      include/pocketpy/error.h
  2. 3 1
      src/ceval.cpp
  3. 2 2
      src/error.cpp
  4. 2 1
      src/vm.cpp
  5. 1 1
      tests/99_bugs.py

+ 4 - 2
include/pocketpy/error.h

@@ -43,9 +43,11 @@ struct Exception {
     bool is_re;
     stack<Str> stacktrace;
 
-    Exception(StrName type, Str msg): type(type), msg(msg), is_re(true) {}
+    int _ip_on_error;
+
+    Exception(StrName type, Str msg): type(type), msg(msg), is_re(true), _ip_on_error(-1) {}
     bool match_type(StrName t) const { return this->type == t;}
-    void st_push(Str snapshot);
+    void st_push(Str&& snapshot);
     Str summary() const;
 };
 

+ 3 - 1
src/ceval.cpp

@@ -750,7 +750,9 @@ __NEXT_STEP:;
             PK_UNUSED(e);
             PyObject* obj = POPX();
             Exception& _e = CAST(Exception&, obj);
-            int current_line = frame->co->lines[frame->_ip];        // current line
+            int actual_ip = frame->_ip;
+            if(_e._ip_on_error >= 0) 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));

+ 2 - 2
src/error.cpp

@@ -51,9 +51,9 @@ namespace pkpy{
         return ss.str();
     }
 
-    void Exception::st_push(Str snapshot){
+    void Exception::st_push(Str&& snapshot){
         if(stacktrace.size() >= 8) return;
-        stacktrace.push(snapshot);
+        stacktrace.push(std::move(snapshot));
     }
 
     Str Exception::summary() const {

+ 2 - 1
src/vm.cpp

@@ -1069,11 +1069,12 @@ PyObject* VM::bind_property(PyObject* obj, Str name, NativeFuncC fget, NativeFun
 }
 
 void VM::_error(Exception e){
+    e._ip_on_error = top_frame()->_ip;
     if(callstack.empty()){
         e.is_re = false;
         throw e;
     }
-    PUSH(VAR(e));
+    PUSH(VAR(std::move(e)));
     _raise();
 }
 

+ 1 - 1
tests/99_bugs.py

@@ -69,4 +69,4 @@ try:
     assert test(0) == '0.00'
     exit(1)
 except UnboundLocalError:
-    pass
+    pass