From 897b2177c19cf2f0dc956b9b868191f5f4d29112 Mon Sep 17 00:00:00 2001 From: usa Date: Fri, 17 Oct 2014 04:20:40 +0000 Subject: merge revision(s) 47716: [Backport #10285] * ext/stringio/stringio.c (strio_write): ASCII-8BIT StringIO should be writable any encoding strings, without conversion. [ruby-core:65240] [Bug #10285] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@47988 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ ext/stringio/stringio.c | 5 +++-- test/stringio/test_stringio.rb | 12 ++++++++++++ version.h | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3d0f200a7c..089176cb37 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Oct 17 13:19:44 2014 Nobuyoshi Nakada + + * ext/stringio/stringio.c (strio_write): ASCII-8BIT StringIO + should be writable any encoding strings, without conversion. + [ruby-core:65240] [Bug #10285] + Fri Oct 17 13:10:08 2014 Nobuyoshi Nakada * vm_eval.c (eval_string_with_cref): fix super from eval with diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 2e7c367276..4dc279cd12 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1171,12 +1171,13 @@ strio_write(VALUE self, VALUE str) struct StringIO *ptr = writable(self); long len, olen; rb_encoding *enc, *enc2; + rb_encoding *const ascii8bit = rb_ascii8bit_encoding(); if (!RB_TYPE_P(str, T_STRING)) str = rb_obj_as_string(str); enc = rb_enc_get(ptr->string); enc2 = rb_enc_get(str); - if (enc != enc2 && enc != rb_ascii8bit_encoding()) { + if (enc != enc2 && enc != ascii8bit) { str = rb_str_conv_enc(str, enc2, enc); } len = RSTRING_LEN(str); @@ -1187,7 +1188,7 @@ strio_write(VALUE self, VALUE str) ptr->pos = olen; } if (ptr->pos == olen) { - if (enc2 == rb_ascii8bit_encoding()) { + if (enc == ascii8bit || enc2 == ascii8bit) { rb_enc_str_buf_cat(ptr->string, RSTRING_PTR(str), len, enc); OBJ_INFECT(ptr->string, str); } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 4b7e904f4b..ada5a3ae24 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -129,6 +129,18 @@ class TestStringIO < Test::Unit::TestCase assert_equal(Encoding::UTF_8, s.encoding, "honor the original encoding over ASCII-8BIT") end + def test_set_encoding + bug10285 = '[ruby-core:65240] [Bug #10285]' + f = StringIO.new() + f.set_encoding(Encoding::ASCII_8BIT) + f.write("quz \x83 mat".b) + s = "foo \x97 bar".force_encoding(Encoding::WINDOWS_1252) + assert_nothing_raised(Encoding::CompatibilityError, bug10285) { + f.write(s) + } + assert_equal(Encoding::ASCII_8BIT, f.string.encoding, bug10285) + end + def test_mode_error f = StringIO.new("", "r") assert_raise(IOError) { f.write("foo") } diff --git a/version.h b/version.h index 0c32c68c4b..2c3b468c00 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.0.0" #define RUBY_RELEASE_DATE "2014-10-17" -#define RUBY_PATCHLEVEL 588 +#define RUBY_PATCHLEVEL 589 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 10 -- cgit v1.2.3