error.c: Let Exception#inspect inspect its message
... only when the message string has a newline. `p StandardError.new("foo\nbar")` now prints `#<StandardError: "foo\nbar">' instead of: #<StandardError: bar> [Bug #18170]
This commit is contained in:
parent
dbfb3b1917
commit
9d927204e7
Notes:
git
2022-06-07 11:07:38 +09:00
12
error.c
12
error.c
@ -34,6 +34,7 @@
|
|||||||
#include "internal/io.h"
|
#include "internal/io.h"
|
||||||
#include "internal/load.h"
|
#include "internal/load.h"
|
||||||
#include "internal/object.h"
|
#include "internal/object.h"
|
||||||
|
#include "internal/string.h"
|
||||||
#include "internal/symbol.h"
|
#include "internal/symbol.h"
|
||||||
#include "internal/thread.h"
|
#include "internal/thread.h"
|
||||||
#include "internal/variable.h"
|
#include "internal/variable.h"
|
||||||
@ -1422,8 +1423,15 @@ exc_inspect(VALUE exc)
|
|||||||
str = rb_str_buf_new2("#<");
|
str = rb_str_buf_new2("#<");
|
||||||
klass = rb_class_name(klass);
|
klass = rb_class_name(klass);
|
||||||
rb_str_buf_append(str, 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);
|
rb_str_buf_cat(str, ">", 1);
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
|
@ -43,6 +43,7 @@ char *rb_str_to_cstr(VALUE str);
|
|||||||
const char *ruby_escaped_char(int c);
|
const char *ruby_escaped_char(int c);
|
||||||
void rb_str_make_independent(VALUE str);
|
void rb_str_make_independent(VALUE str);
|
||||||
int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc);
|
int rb_enc_str_coderange_scan(VALUE str, rb_encoding *enc);
|
||||||
|
VALUE rb_str_include(VALUE str, VALUE arg);
|
||||||
|
|
||||||
static inline bool STR_EMBED_P(VALUE str);
|
static inline bool STR_EMBED_P(VALUE str);
|
||||||
static inline bool STR_SHARED_P(VALUE str);
|
static inline bool STR_SHARED_P(VALUE str);
|
||||||
|
2
string.c
2
string.c
@ -6376,7 +6376,7 @@ rb_str_reverse_bang(VALUE str)
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
VALUE
|
||||||
rb_str_include(VALUE str, VALUE arg)
|
rb_str_include(VALUE str, VALUE arg)
|
||||||
{
|
{
|
||||||
long i;
|
long i;
|
||||||
|
@ -478,6 +478,12 @@ end.join
|
|||||||
def to_s; ""; end
|
def to_s; ""; end
|
||||||
end
|
end
|
||||||
assert_equal(e.inspect, e.new.inspect)
|
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
|
end
|
||||||
|
|
||||||
def test_to_s
|
def test_to_s
|
||||||
|
Loading…
x
Reference in New Issue
Block a user