Skip to content

Commit 7a4d09a

Browse files
committed
Never change the underlying string encoding
StringIO should be a view of given source string and set_encoding shouldn't change source encoding. Similar change for binmode and set_encoding_by_bom.
1 parent cb80e8d commit 7a4d09a

File tree

2 files changed

+1
-10
lines changed

2 files changed

+1
-10
lines changed

ext/stringio/stringio.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ set_encoding_by_bom(struct StringIO *ptr)
278278
if (idx) {
279279
extenc = rb_enc_from_index(idx);
280280
ptr->pos = bomlen;
281-
if (ptr->flags & FMODE_WRITABLE) {
282-
rb_enc_associate_index(ptr->string, idx);
283-
}
284281
}
285282
ptr->enc = extenc;
286283
return extenc;
@@ -696,9 +693,6 @@ strio_binmode(VALUE self)
696693
rb_encoding *enc = rb_ascii8bit_encoding();
697694

698695
ptr->enc = enc;
699-
if (WRITABLE(self)) {
700-
rb_enc_associate(ptr->string, enc);
701-
}
702696
return self;
703697
}
704698

@@ -1859,9 +1853,6 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self)
18591853
}
18601854
}
18611855
ptr->enc = enc;
1862-
if (!NIL_P(ptr->string) && WRITABLE(self)) {
1863-
rb_enc_associate(ptr->string, enc);
1864-
}
18651856

18661857
return self;
18671858
}

test/stringio/test_stringio.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def test_set_encoding
304304
f = StringIO.new()
305305
f.set_encoding("ISO-8859-16:ISO-8859-1")
306306
assert_equal(Encoding::ISO_8859_16, f.external_encoding)
307-
assert_equal(Encoding::ISO_8859_16, f.string.encoding)
307+
assert_equal(Encoding::UTF_8, f.string.encoding)
308308
assert_nil(f.internal_encoding)
309309
end
310310

0 commit comments

Comments
 (0)