Skip to content

Commit

Permalink
StringIO#initialize default to the source string encoding
Browse files Browse the repository at this point in the history
[Bug #16497]
  • Loading branch information
byroot authored and nobu committed Mar 15, 2020
1 parent 9fae6f1 commit 4958a5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ext/stringio/stringio.c
Expand Up @@ -363,7 +363,12 @@ strio_init(int argc, VALUE *argv, struct StringIO *ptr, VALUE self)
rb_str_resize(string, 0);
}
ptr->string = string;
ptr->enc = convconfig.enc;
if (argc == 1) {
ptr->enc = rb_enc_get(string);
}
else {
ptr->enc = convconfig.enc;
}
ptr->pos = 0;
ptr->lineno = 0;
if (ptr->flags & FMODE_SETENC_BY_BOM) set_encoding_by_bom(ptr);
Expand Down Expand Up @@ -1759,9 +1764,6 @@ strio_set_encoding_by_bom(VALUE self)
{
struct StringIO *ptr = StringIO(self);

if (ptr->enc) {
rb_raise(rb_eArgError, "encoding conversion is set");
}
if (!set_encoding_by_bom(ptr)) return Qnil;
return rb_enc_from_encoding(ptr->enc);
}
Expand Down
12 changes: 12 additions & 0 deletions test/stringio/test_stringio.rb
Expand Up @@ -797,6 +797,18 @@ def test_encoding_read
end
end

def test_binary_encoding_read_and_default_internal
verbose, $VERBOSE = $VERBOSE, nil
default_internal = Encoding.default_internal
Encoding.default_internal = Encoding::UTF_8
$VERBOSE = verbose
assert_equal Encoding::BINARY, StringIO.new("Hello".b).read.encoding
ensure
$VERBOSE = nil
Encoding.default_internal = default_internal
$VERBOSE = verbose
end

def assert_string(content, encoding, str, mesg = nil)
assert_equal([content, encoding], [str, str.encoding], mesg)
end
Expand Down

0 comments on commit 4958a5c

Please sign in to comment.