1
0
Эх сурвалжийг харах

add function name in error's snapshot

blueloveTH 2 жил өмнө
parent
commit
7632d2a918

+ 1 - 1
include/pocketpy/error.h

@@ -34,7 +34,7 @@ struct SourceData {
 
     SourceData(const Str& source, const Str& filename, CompileMode mode);
     std::pair<const char*,const char*> 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 {

+ 0 - 2
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); }

+ 4 - 1
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

+ 4 - 2
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<const char*,const char*> pair = get_line(lineno);
         Str line = "<?>";
         int removed_spaces = 0;

+ 0 - 5
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;