1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Revert "Revert "error.c: Let Exception#inspect inspect its message""

This reverts commit b9f030954a.

[Bug #18170]
This commit is contained in:
Yusuke Endoh 2022-09-23 16:40:59 +09:00
parent 6e46bf1e54
commit a78c733cc3
4 changed files with 18 additions and 3 deletions

12
error.c
View file

@ -34,6 +34,7 @@
#include "internal/io.h"
#include "internal/load.h"
#include "internal/object.h"
#include "internal/string.h"
#include "internal/symbol.h"
#include "internal/thread.h"
#include "internal/variable.h"
@ -1439,8 +1440,15 @@ exc_inspect(VALUE exc)
str = rb_str_buf_new2("#<");
klass = rb_class_name(klass);
rb_str_buf_append(str, klass);
rb_str_buf_cat(str, ": ", 2);
rb_str_buf_append(str, exc);
if (RTEST(rb_str_include(exc, rb_str_new2("\n")))) {
rb_str_catf(str, ":%+"PRIsVALUE, exc);
}
else {
rb_str_buf_cat(str, ": ", 2);
rb_str_buf_append(str, exc);
}
rb_str_buf_cat(str, ">", 1);
return str;

View file

@ -44,6 +44,7 @@ const char *ruby_escaped_char(int c);
void rb_str_make_independent(VALUE str);
int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc);
int rb_ascii8bit_appendable_encoding_index(rb_encoding *enc, unsigned int code);
VALUE rb_str_include(VALUE str, VALUE arg);
static inline bool STR_EMBED_P(VALUE str);
static inline bool STR_SHARED_P(VALUE str);

View file

@ -6456,7 +6456,7 @@ rb_str_reverse_bang(VALUE str)
*
*/
static VALUE
VALUE
rb_str_include(VALUE str, VALUE arg)
{
long i;

View file

@ -478,6 +478,12 @@ end.join
def to_s; ""; end
end
assert_equal(e.inspect, e.new.inspect)
# https://bugs.ruby-lang.org/issues/18170#note-13
assert_equal('#<Exception:"foo\nbar">', Exception.new("foo\nbar").inspect)
assert_equal('#<Exception: foo bar>', Exception.new("foo bar").inspect)
assert_equal('#<Exception: foo\bar>', Exception.new("foo\\bar").inspect)
assert_equal('#<Exception: "foo\nbar">', Exception.new('"foo\nbar"').inspect)
end
def test_to_s