Skip to content

Commit

Permalink
Remove location info from Exception#inspect
Browse files Browse the repository at this point in the history
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)
  ```
  • Loading branch information
shuujii committed Dec 14, 2019
1 parent 6c5ee8f commit d2f2f9d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 59 deletions.
1 change: 1 addition & 0 deletions include/mruby/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
void mrb_class_name_class(mrb_state*, struct RClass*, struct RClass*, mrb_sym);
mrb_bool mrb_const_name_p(mrb_state*, const char*, mrb_int);
mrb_value mrb_class_find_path(mrb_state*, struct RClass*);
mrb_value mrb_mod_to_s(mrb_state*, mrb_value);
void mrb_gc_mark_mt(mrb_state*, struct RClass*);
size_t mrb_gc_mark_mt_size(mrb_state*, struct RClass*);
void mrb_gc_free_mt(mrb_state*, struct RClass*);
Expand Down
61 changes: 4 additions & 57 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <mruby/proc.h>
#include <mruby/string.h>
#include <mruby/variable.h>
#include <mruby/debug.h>
#include <mruby/error.h>
#include <mruby/class.h>
#include <mruby/throw.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/t/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,10 @@ def z
assert_equal [true, true], Class4Exception19.new.a
end

assert('Exception#inspect without message') do
assert('Exception#inspect') do
assert_equal "Exception", Exception.new.inspect
assert_equal "Exception", Exception.new("").inspect
assert_equal "error! (Exception)", Exception.new("error!").inspect
end

assert('Exception#backtrace') do
Expand Down

0 comments on commit d2f2f9d

Please sign in to comment.