Skip to content
Permalink
Browse files

[ruby/stringio] StringIO#initialize default to the source string enco…

…ding

[Bug #16497]

ruby/stringio@4958a5c
(cherry picked from commit e257c08)
  • Loading branch information
byroot authored and nurse committed Mar 12, 2020
1 parent 3efbd52 commit 47b08728cf3d0441a3da4dc1dcdd578817b0e036
Showing with 21 additions and 4 deletions.
  1. +6 −4 ext/stringio/stringio.c
  2. +3 −0 spec/ruby/library/stringio/initialize_spec.rb
  3. +12 −0 test/stringio/test_stringio.rb
@@ -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);
@@ -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);
}
@@ -187,11 +187,14 @@
describe "StringIO#initialize sets the encoding to" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
Encoding.default_external = Encoding::ISO_8859_2
Encoding.default_internal = Encoding::ISO_8859_2
end

after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end

it "Encoding.default_external when passed no arguments" do
@@ -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

0 comments on commit 47b0872

Please sign in to comment.
You can’t perform that action at this time.