aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--ext/stringio/stringio.c5
-rw-r--r--test/stringio/test_stringio.rb12
3 files changed, 21 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 6b32bc4eb1..01dda976e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Fri Sep 26 12:52:36 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * ext/stringio/stringio.c (strio_write): ASCII-8BIT StringIO
+ should be writable any encoding strings, without conversion.
+ [ruby-core:65240] [Bug #10285]
+
Fri Sep 26 05:21:01 2014 Eric Wong <e@80x24.org>
* object.c (rb_class_real): do not dereference 0 VALUE
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 2964adab40..b426214729 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -1169,12 +1169,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);
@@ -1185,7 +1186,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 3fbe2f7eca..07d89d16e1 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -137,6 +137,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") }