diff --git a/ChangeLog b/ChangeLog index 43074d25e2..bf12b89631 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sun Dec 13 11:47:35 2015 Shugo Maeda + + * object.c (rb_inspect): check the default internal encoding as + String#inspect do. + [ruby-dev:49415] [Bug #11787] + Sun Dec 13 11:38:12 2015 Akinori MUSHA * lib/shellwords.rb: Turn on frozen-string-literal after fixing diff --git a/object.c b/object.c index 3111701c58..99f58a8f6c 100644 --- a/object.c +++ b/object.c @@ -467,22 +467,23 @@ rb_any_to_s(VALUE obj) VALUE rb_str_escape(VALUE str); /* - * If the default external encoding is ASCII compatible, the encoding of - * the inspected result must be compatible with it. - * If the default external encoding is ASCII incompatible, + * If the default internal or external encoding is ASCII compatible, + * the encoding of the inspected result must be compatible with it. + * If the default internal or external encoding is ASCII incompatible, * the result must be ASCII only. */ VALUE rb_inspect(VALUE obj) { VALUE str = rb_obj_as_string(rb_funcallv(obj, id_inspect, 0, 0)); - rb_encoding *ext = rb_default_external_encoding(); - if (!rb_enc_asciicompat(ext)) { + rb_encoding *enc = rb_default_internal_encoding(); + if (enc == NULL) enc = rb_default_external_encoding(); + if (!rb_enc_asciicompat(enc)) { if (!rb_enc_str_asciionly_p(str)) return rb_str_escape(str); return str; } - if (rb_enc_get(str) != ext && !rb_enc_str_asciionly_p(str)) + if (rb_enc_get(str) != enc && !rb_enc_str_asciionly_p(str)) return rb_str_escape(str); return str; } diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index 2e9e65b52f..4efe18187b 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -1648,4 +1648,17 @@ class TestM17N < Test::Unit::TestCase assert_match(escape_plain, 0x5b.chr(::Encoding::UTF_8), bug10670) assert_match(escape_plain, 0x5b.chr, bug10670) end + + def test_inspect_with_default_internal + bug11787 = '[ruby-dev:49415] [Bug #11787]' + + orig_int = Encoding.default_internal + Encoding.default_internal = ::Encoding::EUC_JP + s = begin + [e("\xB4\xC1\xBB\xFA")].inspect + ensure + Encoding.default_internal = orig_int + end + assert_equal(e("[\"\xB4\xC1\xBB\xFA\"]"), s, bug11787) + end end