add function name in error's snapshot

This commit is contained in:
blueloveTH 2023-09-10 01:17:56 +08:00
parent 45bafb7534
commit 7632d2a918
5 changed files with 9 additions and 11 deletions

View File

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

View File

@ -110,8 +110,6 @@ struct Frame {
return co->codes[_ip]; return co->codes[_ip];
} }
Str snapshot();
PyObject** actual_sp_base() const { return _locals.a; } PyObject** actual_sp_base() const { return _locals.a; }
int stack_size() const { return _s->_sp - actual_sp_base(); } int stack_size() const { return _s->_sp - actual_sp_base(); }
ArgsView stack_view() const { return ArgsView(actual_sp_base(), _s->_sp); } ArgsView stack_view() const { return ArgsView(actual_sp_base(), _s->_sp); }

View File

@ -750,7 +750,10 @@ __NEXT_STEP:;
PK_UNUSED(e); PK_UNUSED(e);
PyObject* obj = POPX(); PyObject* obj = POPX();
Exception& _e = CAST(Exception&, obj); 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(); _pop_frame();
if(callstack.empty()){ if(callstack.empty()){
#if PK_DEBUG_FULL_EXCEPTION #if PK_DEBUG_FULL_EXCEPTION

View File

@ -30,9 +30,11 @@ namespace pkpy{
return {_start, i}; 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; 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); std::pair<const char*,const char*> pair = get_line(lineno);
Str line = "<?>"; Str line = "<?>";
int removed_spaces = 0; int removed_spaces = 0;

View File

@ -23,11 +23,6 @@ namespace pkpy{
return fn._closure->try_get(name); 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(){ bool Frame::jump_to_exception_handler(){
// try to find a parent try block // try to find a parent try block
int block = co->codes[_ip].block; int block = co->codes[_ip].block;