ruby-changes:40988
From: shugo <ko1@a...>
Date: Sun, 13 Dec 2015 11:48:46 +0900 (JST)
Subject: [ruby-changes:40988] shugo:r53067 (trunk): * object.c (rb_inspect): check the default internal encoding as
shugo 2015-12-13 11:48:34 +0900 (Sun, 13 Dec 2015) New Revision: 53067 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53067 Log: * object.c (rb_inspect): check the default internal encoding as String#inspect do. [ruby-dev:49415] [Bug #11787] Modified files: trunk/ChangeLog trunk/object.c trunk/test/ruby/test_m17n.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 53066) +++ ChangeLog (revision 53067) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Dec 13 11:47:35 2015 Shugo Maeda <shugo@r...> + + * 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 <knu@i...> * lib/shellwords.rb: Turn on frozen-string-literal after fixing Index: object.c =================================================================== --- object.c (revision 53066) +++ object.c (revision 53067) @@ -467,22 +467,23 @@ rb_any_to_s(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L467 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; } Index: test/ruby/test_m17n.rb =================================================================== --- test/ruby/test_m17n.rb (revision 53066) +++ test/ruby/test_m17n.rb (revision 53067) @@ -1648,4 +1648,17 @@ class TestM17N < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_m17n.rb#L1648 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/