Skip to content

Commit 5eca58a

Browse files
committed
StringIO#set_encoding should coerce the argument to an Encoding
* Fixes #2954
1 parent 7958712 commit 5eca58a

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Compatibility:
117117
* Add `Refinement#import_methods` method and add deprecation warning for `Refinement#include` and `Refinement#prepend` (#2733, @horakivo).
118118
* Upgrading `UNICODE` version to 13.0.0 and `EMOJI` version to 13.1 (#2733, @horakivo).
119119
* Add `rb_io_maybe_wait_readable`, `rb_io_maybe_wait_writable` and `rb_io_maybe_wait` functions (#2733, @andrykonchin).
120+
* `StringIO#set_encoding` should coerce the argument to an Encoding (#2954, @eregon).
120121

121122
Performance:
122123

lib/truffle/stringio.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def initialize_copy(from)
191191
end
192192

193193
def set_encoding(external, internal = nil, options = nil)
194-
encoding = external || Encoding.default_external
194+
encoding = Truffle::Type.coerce_to_encoding(external || Encoding.default_external)
195195
d = @__data__
196196
TruffleRuby.synchronized(d) do
197197
d.encoding = encoding

spec/ruby/library/stringio/set_encoding_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@
1717
io.set_encoding Encoding::UTF_8
1818
io.string.encoding.should == Encoding::US_ASCII
1919
end
20+
21+
it "accepts a String" do
22+
str = "".encode(Encoding::US_ASCII)
23+
io = StringIO.new(str)
24+
io.set_encoding("ASCII-8BIT")
25+
io.external_encoding.should == Encoding::BINARY
26+
str.encoding.should == Encoding::BINARY
27+
end
2028
end

0 commit comments

Comments
 (0)