From d2f2f9db511afc348a88f864b9f0b0ccfc59a8f1 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sat, 14 Dec 2019 21:48:32 +0900 Subject: Remove location info from `Exception#inspect` Because location info (file name and line number) is kept in the backtrace, it should not be kept in the result of `inspect` (and the exception object itself), I think. ### Example ```ruby # example.rb begin raise "err" rescue => e p e end ``` #### Before this patch: ``` $ bin/mruby example.rb example.rb:2: err (RuntimeError) ``` #### After this patch: ``` $ bin/mruby example.rb err (RuntimeError) ``` --- src/error.c | 61 ++++-------------------------------------------------------- src/string.c | 1 - 2 files changed, 4 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/error.c b/src/error.c index 43b09ec66..2e3e8c09d 100644 --- a/src/error.c +++ b/src/error.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -131,34 +130,10 @@ exc_message(mrb_state *mrb, mrb_value exc) static mrb_value exc_inspect(mrb_state *mrb, mrb_value exc) { - mrb_value str, mesg, file, line; - mrb_bool append_mesg; - const char *cname; - - mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg")); - file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file")); - line = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "line")); - - append_mesg = !mrb_nil_p(mesg); - if (append_mesg) { - mesg = mrb_obj_as_string(mrb, mesg); - append_mesg = RSTRING_LEN(mesg) > 0; - } - - cname = mrb_obj_classname(mrb, exc); - str = mrb_str_new_cstr(mrb, cname); - if (mrb_string_p(file) && mrb_fixnum_p(line)) { - if (append_mesg) { - str = mrb_format(mrb, "%v:%v: %v (%v)", file, line, mesg, str); - } - else { - str = mrb_format(mrb, "%v:%v: %v", file, line, str); - } - } - else if (append_mesg) { - str = mrb_format(mrb, "%v: %v", str, mesg); - } - return str; + mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg")); + mrb_value cname = mrb_mod_to_s(mrb, mrb_obj_value(mrb_obj_class(mrb, exc))); + mesg = mrb_obj_as_string(mrb, mesg); + return RSTRING_LEN(mesg) == 0 ? cname : mrb_format(mrb, "%v (%v)", mesg, cname); } void mrb_keep_backtrace(mrb_state *mrb, mrb_value exc); @@ -192,33 +167,6 @@ exc_set_backtrace(mrb_state *mrb, mrb_value exc) return backtrace; } -static void -exc_debug_info(mrb_state *mrb, struct RObject *exc) -{ - mrb_callinfo *ci = mrb->c->ci; - const mrb_code *pc = ci->pc; - - if (mrb_obj_iv_defined(mrb, exc, mrb_intern_lit(mrb, "file"))) return; - while (ci >= mrb->c->cibase) { - const mrb_code *err = ci->err; - - if (!err && pc) err = pc - 1; - if (err && ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) { - mrb_irep *irep = ci->proc->body.irep; - - int32_t const line = mrb_debug_get_line(mrb, irep, err - irep->iseq); - char const* file = mrb_debug_get_filename(mrb, irep, err - irep->iseq); - if (line != -1 && file) { - mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "file"), mrb_str_new_cstr(mrb, file)); - mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "line"), mrb_fixnum_value(line)); - return; - } - } - pc = ci->pc; - ci--; - } -} - void mrb_exc_set(mrb_state *mrb, mrb_value exc) { @@ -232,7 +180,6 @@ mrb_exc_set(mrb_state *mrb, mrb_value exc) mrb->gc.arena_idx--; } if (!mrb->gc.out_of_memory && !mrb_frozen_p(mrb->exc)) { - exc_debug_info(mrb, mrb->exc); mrb_keep_backtrace(mrb, exc); } } diff --git a/src/string.c b/src/string.c index 5a0a6a233..b3f07dfb0 100644 --- a/src/string.c +++ b/src/string.c @@ -1095,7 +1095,6 @@ mrb_str_equal_m(mrb_state *mrb, mrb_value str1) return mrb_bool_value(mrb_str_equal(mrb, str1, str2)); } /* ---------------------------------- */ -mrb_value mrb_mod_to_s(mrb_state *mrb, mrb_value klass); MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str) -- cgit v1.2.3