Skip to content

Commit ec92ef2

Browse files
committed
Merge pull request #478 in G/truffleruby from gh-1473 to master
* commit 'a51357e335bb6f362f2adeac5f5d49d11095af18': `StringIO#set_encoding` should not attempt to change the encoding the underlying String if that String is frozen.
2 parents 647fc84 + a51357e commit ec92ef2

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ New features:
99
Bug fixes:
1010

1111
* Adding missing support for the `close_others` option to `exec` and `spawn`.
12-
* Allow signal `0` to be used with `Process.kill`.
1312
* FFI::Pointer now does the correct range checks for signed and unsigned values.
1413
* Allow signal `0` to be used with `Process.kill` (#1474).
1514
* `IO#dup` now properly sets the new `IO` instance to be close-on-exec.
1615
* `IO#reopen` now properly resets the receiver to be close-on-exec.
16+
* `StringIO#set_encoding` no longer raises an exception if the underlying
17+
`String` is frozen (#1473).
1718

1819
Changes:
1920

lib/truffle/stringio.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def check_writable
120120
def set_encoding(external, internal=nil, options=nil)
121121
encoding = external || Encoding.default_external
122122
@__data__.encoding = encoding
123-
@__data__.string.force_encoding(encoding)
123+
@__data__.string.force_encoding(encoding) if @writable
124+
124125
self
125126
end
126127

spec/ruby/library/stringio/set_encoding_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22
require_relative '../../spec_helper'
33

44
describe "StringIO#set_encoding" do
5-
it "sets the encoding of the underlying String" do
6-
io = StringIO.new
5+
it "sets the encoding of the underlying String if the String is not frozen" do
6+
str = "".encode(Encoding::US_ASCII)
7+
8+
io = StringIO.new(str)
79
io.set_encoding Encoding::UTF_8
810
io.string.encoding.should == Encoding::UTF_8
911
end
12+
13+
it "does not set the encoding of the underlying String if the String is frozen" do
14+
str = "".encode(Encoding::US_ASCII).freeze
15+
16+
io = StringIO.new(str)
17+
io.set_encoding Encoding::UTF_8
18+
io.string.encoding.should == Encoding::US_ASCII
19+
end
1020
end
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
fails:StringIO#external_encoding does not set the encoding of its buffer string if the string is frozen
21
fails:StringIO#external_encoding changes to match string if string's encoding is changed

0 commit comments

Comments
 (0)