Skip to content

Commit 4958a5c

Browse files
byrootnobu
authored andcommitted
StringIO#initialize default to the source string encoding
[Bug #16497]
1 parent 9fae6f1 commit 4958a5c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

ext/stringio/stringio.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,12 @@ strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
363363
rb_str_resize(string, 0);
364364
}
365365
ptr->string = string;
366-
ptr->enc = convconfig.enc;
366+
if (argc == 1) {
367+
ptr->enc = rb_enc_get(string);
368+
}
369+
else {
370+
ptr->enc = convconfig.enc;
371+
}
367372
ptr->pos = 0;
368373
ptr->lineno = 0;
369374
if (ptr->flags & FMODE_SETENC_BY_BOM) set_encoding_by_bom(ptr);
@@ -1759,9 +1764,6 @@ strio_set_encoding_by_bom(VALUE self)
17591764
{
17601765
struct StringIO *ptr = StringIO(self);
17611766

1762-
if (ptr->enc) {
1763-
rb_raise(rb_eArgError, "encoding conversion is set");
1764-
}
17651767
if (!set_encoding_by_bom(ptr)) return Qnil;
17661768
return rb_enc_from_encoding(ptr->enc);
17671769
}

test/stringio/test_stringio.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,18 @@ def test_encoding_read
797797
end
798798
end
799799

800+
def test_binary_encoding_read_and_default_internal
801+
verbose, $VERBOSE = $VERBOSE, nil
802+
default_internal = Encoding.default_internal
803+
Encoding.default_internal = Encoding::UTF_8
804+
$VERBOSE = verbose
805+
assert_equal Encoding::BINARY, StringIO.new("Hello".b).read.encoding
806+
ensure
807+
$VERBOSE = nil
808+
Encoding.default_internal = default_internal
809+
$VERBOSE = verbose
810+
end
811+
800812
def assert_string(content, encoding, str, mesg = nil)
801813
assert_equal([content, encoding], [str, str.encoding], mesg)
802814
end

0 commit comments

Comments
 (0)