Skip to content

Commit

Permalink
[GR-18163] Fix IO#set_encoding
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3534
  • Loading branch information
eregon committed Nov 3, 2022
2 parents c00d9f4 + e60d3eb commit 1888b29
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 110 deletions.
32 changes: 31 additions & 1 deletion spec/ruby/core/io/set_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
require_relative '../../spec_helper'

describe :io_set_encoding_write, shared: true do
it "sets the encodings to nil" do
it "sets the encodings to nil when they were set previously" do
@io = new_io @name, "#{@object}:ibm437:ibm866"
@io.set_encoding nil, nil

@io.external_encoding.should be_nil
@io.internal_encoding.should be_nil
end

it "sets the encodings to nil when the IO is built with no explicit encoding" do
@io = new_io @name, @object

# Checking our assumptions first
@io.external_encoding.should be_nil
@io.internal_encoding.should be_nil

@io.set_encoding nil, nil

@io.external_encoding.should be_nil
@io.internal_encoding.should be_nil
end

it "prevents the encodings from changing when Encoding defaults are changed" do
@io = new_io @name, "#{@object}:utf-8:us-ascii"
@io.set_encoding nil, nil
Expand Down Expand Up @@ -38,6 +51,7 @@
@external = Encoding.default_external
@internal = Encoding.default_internal

# The defaults
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = nil

Expand Down Expand Up @@ -113,6 +127,22 @@
describe "with 'a+' mode" do
it_behaves_like :io_set_encoding_write, nil, "a+"
end

describe "with standard IOs" do
it "correctly resets them" do
STDOUT.external_encoding.should == nil
STDOUT.internal_encoding.should == nil

begin
STDOUT.set_encoding(Encoding::US_ASCII, Encoding::ISO_8859_1)
ensure
STDOUT.set_encoding(nil, nil)
end

STDOUT.external_encoding.should == nil
STDOUT.internal_encoding.should == nil
end
end
end

describe "IO#set_encoding" do
Expand Down
2 changes: 1 addition & 1 deletion src/main/ruby/truffleruby/core/argf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def initialize(argv = ARGV, *others)
#
def binmode
@binmode = true
set_encoding(Encoding::ASCII_8BIT)
set_encoding(Encoding::BINARY)
self
end

Expand Down
Loading

0 comments on commit 1888b29

Please sign in to comment.