diff --git a/include/pocketpy/error.h b/include/pocketpy/error.h index eda4ebe7..b60319f3 100644 --- a/include/pocketpy/error.h +++ b/include/pocketpy/error.h @@ -34,7 +34,7 @@ struct SourceData { SourceData(const Str& source, const Str& filename, CompileMode mode); std::pair get_line(int lineno) const; - Str snapshot(int lineno, const char* cursor=nullptr); + Str snapshot(int lineno, const char* cursor=nullptr, std::string_view name=""); }; struct Exception { diff --git a/include/pocketpy/frame.h b/include/pocketpy/frame.h index 294cbe9a..90f90112 100644 --- a/include/pocketpy/frame.h +++ b/include/pocketpy/frame.h @@ -110,8 +110,6 @@ struct Frame { return co->codes[_ip]; } - Str snapshot(); - PyObject** actual_sp_base() const { return _locals.a; } int stack_size() const { return _s->_sp - actual_sp_base(); } ArgsView stack_view() const { return ArgsView(actual_sp_base(), _s->_sp); } diff --git a/src/ceval.cpp b/src/ceval.cpp index a580b7a7..5073b138 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -750,7 +750,10 @@ __NEXT_STEP:; PK_UNUSED(e); PyObject* obj = POPX(); Exception& _e = CAST(Exception&, obj); - _e.st_push(frame->snapshot()); + int current_line = frame->co->lines[frame->_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 diff --git a/src/error.cpp b/src/error.cpp index 822694b8..cd3d6d44 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -30,9 +30,11 @@ namespace pkpy{ return {_start, i}; } - Str SourceData::snapshot(int lineno, const char* cursor){ + Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name){ std::stringstream ss; - ss << " " << "File \"" << filename << "\", line " << lineno << '\n'; + ss << " " << "File \"" << filename << "\", line " << lineno; + if(!name.empty()) ss << ", in " << name; + ss << '\n'; std::pair pair = get_line(lineno); Str line = ""; int removed_spaces = 0; diff --git a/src/frame.cpp b/src/frame.cpp index 2fafd34e..8beefaf4 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -23,11 +23,6 @@ namespace pkpy{ return fn._closure->try_get(name); } - Str Frame::snapshot(){ - int line = co->lines[_ip]; - return co->src->snapshot(line); - } - bool Frame::jump_to_exception_handler(){ // try to find a parent try block int block = co->codes[_ip].block;