Skip to content

Commit d2f2f9d

Browse files
committed
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) ```
1 parent 6c5ee8f commit d2f2f9d

File tree

4 files changed

+8
-59
lines changed

4 files changed

+8
-59
lines changed

include/mruby/class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
9090
void mrb_class_name_class(mrb_state*, struct RClass*, struct RClass*, mrb_sym);
9191
mrb_bool mrb_const_name_p(mrb_state*, const char*, mrb_int);
9292
mrb_value mrb_class_find_path(mrb_state*, struct RClass*);
93+
mrb_value mrb_mod_to_s(mrb_state*, mrb_value);
9394
void mrb_gc_mark_mt(mrb_state*, struct RClass*);
9495
size_t mrb_gc_mark_mt_size(mrb_state*, struct RClass*);
9596
void mrb_gc_free_mt(mrb_state*, struct RClass*);

src/error.c

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <mruby/proc.h>
1414
#include <mruby/string.h>
1515
#include <mruby/variable.h>
16-
#include <mruby/debug.h>
1716
#include <mruby/error.h>
1817
#include <mruby/class.h>
1918
#include <mruby/throw.h>
@@ -131,34 +130,10 @@ exc_message(mrb_state *mrb, mrb_value exc)
131130
static mrb_value
132131
exc_inspect(mrb_state *mrb, mrb_value exc)
133132
{
134-
mrb_value str, mesg, file, line;
135-
mrb_bool append_mesg;
136-
const char *cname;
137-
138-
mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
139-
file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file"));
140-
line = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "line"));
141-
142-
append_mesg = !mrb_nil_p(mesg);
143-
if (append_mesg) {
144-
mesg = mrb_obj_as_string(mrb, mesg);
145-
append_mesg = RSTRING_LEN(mesg) > 0;
146-
}
147-
148-
cname = mrb_obj_classname(mrb, exc);
149-
str = mrb_str_new_cstr(mrb, cname);
150-
if (mrb_string_p(file) && mrb_fixnum_p(line)) {
151-
if (append_mesg) {
152-
str = mrb_format(mrb, "%v:%v: %v (%v)", file, line, mesg, str);
153-
}
154-
else {
155-
str = mrb_format(mrb, "%v:%v: %v", file, line, str);
156-
}
157-
}
158-
else if (append_mesg) {
159-
str = mrb_format(mrb, "%v: %v", str, mesg);
160-
}
161-
return str;
133+
mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
134+
mrb_value cname = mrb_mod_to_s(mrb, mrb_obj_value(mrb_obj_class(mrb, exc)));
135+
mesg = mrb_obj_as_string(mrb, mesg);
136+
return RSTRING_LEN(mesg) == 0 ? cname : mrb_format(mrb, "%v (%v)", mesg, cname);
162137
}
163138

164139
void mrb_keep_backtrace(mrb_state *mrb, mrb_value exc);
@@ -192,33 +167,6 @@ exc_set_backtrace(mrb_state *mrb, mrb_value exc)
192167
return backtrace;
193168
}
194169

195-
static void
196-
exc_debug_info(mrb_state *mrb, struct RObject *exc)
197-
{
198-
mrb_callinfo *ci = mrb->c->ci;
199-
const mrb_code *pc = ci->pc;
200-
201-
if (mrb_obj_iv_defined(mrb, exc, mrb_intern_lit(mrb, "file"))) return;
202-
while (ci >= mrb->c->cibase) {
203-
const mrb_code *err = ci->err;
204-
205-
if (!err && pc) err = pc - 1;
206-
if (err && ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
207-
mrb_irep *irep = ci->proc->body.irep;
208-
209-
int32_t const line = mrb_debug_get_line(mrb, irep, err - irep->iseq);
210-
char const* file = mrb_debug_get_filename(mrb, irep, err - irep->iseq);
211-
if (line != -1 && file) {
212-
mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "file"), mrb_str_new_cstr(mrb, file));
213-
mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "line"), mrb_fixnum_value(line));
214-
return;
215-
}
216-
}
217-
pc = ci->pc;
218-
ci--;
219-
}
220-
}
221-
222170
void
223171
mrb_exc_set(mrb_state *mrb, mrb_value exc)
224172
{
@@ -232,7 +180,6 @@ mrb_exc_set(mrb_state *mrb, mrb_value exc)
232180
mrb->gc.arena_idx--;
233181
}
234182
if (!mrb->gc.out_of_memory && !mrb_frozen_p(mrb->exc)) {
235-
exc_debug_info(mrb, mrb->exc);
236183
mrb_keep_backtrace(mrb, exc);
237184
}
238185
}

src/string.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,6 @@ mrb_str_equal_m(mrb_state *mrb, mrb_value str1)
10951095
return mrb_bool_value(mrb_str_equal(mrb, str1, str2));
10961096
}
10971097
/* ---------------------------------- */
1098-
mrb_value mrb_mod_to_s(mrb_state *mrb, mrb_value klass);
10991098

11001099
MRB_API mrb_value
11011100
mrb_str_to_str(mrb_state *mrb, mrb_value str)

test/t/exception.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,10 @@ def z
352352
assert_equal [true, true], Class4Exception19.new.a
353353
end
354354

355-
assert('Exception#inspect without message') do
355+
assert('Exception#inspect') do
356356
assert_equal "Exception", Exception.new.inspect
357+
assert_equal "Exception", Exception.new("").inspect
358+
assert_equal "error! (Exception)", Exception.new("error!").inspect
357359
end
358360

359361
assert('Exception#backtrace') do

0 commit comments

Comments
 (0)