Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/irb/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,14 @@ def set_encoding(extern, intern = nil, override: true)
verbose, $VERBOSE = $VERBOSE, nil
Encoding.default_external = extern unless extern.nil? || extern.empty?
Encoding.default_internal = intern unless intern.nil? || intern.empty?
[$stdin, $stdout, $stderr].each do |io|
io.set_encoding(extern, intern)
end
if override
[$stdin, $stdout, $stderr].each do |io|
io.set_encoding(extern, intern)
end
@CONF[:LC_MESSAGES].instance_variable_set(:@override_encoding, extern)
else
# IRB's own IO copies use the locale encoding. Leave the process streams
# alone so embedded IRB does not break later binary writes (irb#1162).
@CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern)
end
ensure
Expand Down
3 changes: 3 additions & 0 deletions test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def test_history_different_encodings
IRB.conf[:SAVE_HISTORY] = 2
IRB.conf[:LC_MESSAGES] = IRB::Locale.new("en_US.ASCII")
IRB.__send__(:set_encoding, Encoding::US_ASCII.name, override: false)
# Reline encodes history from the input's encoding. set_encoding no longer
# rewrites process IO, so this test points STDIN at the encoding it covers.
STDIN.set_encoding(Encoding::US_ASCII)
assert_history(<<~EXPECTED_HISTORY.encode(Encoding::US_ASCII), <<~INITIAL_HISTORY.encode(Encoding::UTF_8), <<~INPUT)
????
exit
Expand Down
16 changes: 16 additions & 0 deletions test/irb/test_input_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def teardown
end

class RelineInputMethodTest < InputMethodTest
def test_initialization_leaves_process_stdio_encoding_alone
encodings = [$stdin, $stdout, $stderr].map {|io| [io.external_encoding, io.internal_encoding] }

input = IRB::RelineInputMethod.new(IRB::RegexpCompletor.new)

assert_equal encodings, [$stdin, $stdout, $stderr].map {|io| [io.external_encoding, io.internal_encoding] }
assert_equal Reline.encoding_system_needs, input.instance_variable_get(:@stdout).external_encoding
end

def test_explicit_set_encoding_still_changes_process_stdio
IRB.__send__(:set_encoding, Encoding::US_ASCII.name)

assert_equal Encoding::US_ASCII, $stdout.external_encoding
assert_equal Encoding::US_ASCII, Encoding.default_external
end

def test_initialization
Reline.completion_proc = nil
Reline.dig_perfect_match_proc = nil
Expand Down