Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[win32] Transcode input from console [Bug #18353] #5196

Merged
merged 2 commits into from
Dec 1, 2021
Merged
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
12 changes: 12 additions & 0 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -12224,6 +12224,18 @@ rb_stdio_set_default_encoding(void)
{
VALUE val = Qnil;

#ifdef _WIN32
if (isatty(fileno(stdin))) {
rb_encoding *external = rb_locale_encoding();
rb_encoding *internal = rb_default_internal_encoding();
if (!internal) internal = rb_default_external_encoding();
io_encoding_set(RFILE(rb_stdin)->fptr,
rb_enc_from_encoding(external),
rb_enc_from_encoding(internal),
Qnil);
}
else
#endif
rb_io_set_encoding(1, &val, rb_stdin);
rb_io_set_encoding(1, &val, rb_stdout);
rb_io_set_encoding(1, &val, rb_stderr);
Expand Down
32 changes: 17 additions & 15 deletions spec/ruby/language/predefined_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1208,13 +1208,24 @@ def obj.foo2; yield; end
end

describe "STDIN" do
it "has the same external encoding as Encoding.default_external" do
STDIN.external_encoding.should equal(Encoding.default_external)
end
platform_is_not :windows do
it "has the same external encoding as Encoding.default_external" do
STDIN.external_encoding.should equal(Encoding.default_external)
end

it "has the same external encoding as Encoding.default_external when that encoding is changed" do
Encoding.default_external = Encoding::ISO_8859_16
STDIN.external_encoding.should equal(Encoding::ISO_8859_16)
end

it "has the same external encoding as Encoding.default_external when that encoding is changed" do
Encoding.default_external = Encoding::ISO_8859_16
STDIN.external_encoding.should equal(Encoding::ISO_8859_16)
it "has nil for the internal encoding" do
STDIN.internal_encoding.should be_nil
end

it "has nil for the internal encoding despite Encoding.default_internal being changed" do
Encoding.default_internal = Encoding::IBM437
STDIN.internal_encoding.should be_nil
end
end

it "has the encodings set by #set_encoding" do
Expand All @@ -1229,15 +1240,6 @@ def obj.foo2; yield; end
"p [STDIN.external_encoding.name, STDIN.internal_encoding.name]"
ruby_exe(code).chomp.should == %{["IBM775", "IBM866"]}
end

it "has nil for the internal encoding" do
STDIN.internal_encoding.should be_nil
end

it "has nil for the internal encoding despite Encoding.default_internal being changed" do
Encoding.default_internal = Encoding::IBM437
STDIN.internal_encoding.should be_nil
end
end

describe "STDOUT" do
Expand Down