From 5d3d9b2dec014fc9593fda8042c9e69ee2ee28cf Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 10 Sep 2023 01:51:12 +0800 Subject: [PATCH] ... --- include/pocketpy/error.h | 6 ++++-- src/ceval.cpp | 4 +++- src/error.cpp | 4 ++-- src/vm.cpp | 3 ++- tests/99_bugs.py | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/pocketpy/error.h b/include/pocketpy/error.h index b60319f3..4d6de5ca 100644 --- a/include/pocketpy/error.h +++ b/include/pocketpy/error.h @@ -43,9 +43,11 @@ struct Exception { bool is_re; stack 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; }; diff --git a/src/ceval.cpp b/src/ceval.cpp index 5073b138..5e517e52 100644 --- a/src/ceval.cpp +++ b/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)); diff --git a/src/error.cpp b/src/error.cpp index cd3d6d44..f1d801b1 100644 --- a/src/error.cpp +++ b/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 { diff --git a/src/vm.cpp b/src/vm.cpp index d5d3a65b..13e79adb 100644 --- a/src/vm.cpp +++ b/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(); } diff --git a/tests/99_bugs.py b/tests/99_bugs.py index ad05f7f6..c86a8813 100644 --- a/tests/99_bugs.py +++ b/tests/99_bugs.py @@ -69,4 +69,4 @@ try: assert test(0) == '0.00' exit(1) except UnboundLocalError: - pass \ No newline at end of file + pass