Set default for Encoding.default_external to UTF-8 on Windows (#2877)

* Use UTF-8 as default for Encoding.default_external on Windows
* Document UTF-8 change on Windows to Encoding.default_external

fix https://bugs.ruby-lang.org/issues/16604
This commit is contained in:
Lars Kanis 2020-12-07 17:48:37 +01:00 committed by GitHub
parent 3bf7b999e5
commit 94b6933d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2020-12-08 01:49:11 +09:00
Merged-By: nurse <naruse@airemix.jp>
4 changed files with 11 additions and 6 deletions

View File

@ -1684,7 +1684,9 @@ rb_enc_default_external(void)
* File data written to disk will be transcoded to the default external * File data written to disk will be transcoded to the default external
* encoding when written, if default_internal is not nil. * encoding when written, if default_internal is not nil.
* *
* The default external encoding is initialized by the locale or -E option. * The default external encoding is initialized by the -E option.
* If -E isn't set, it is initialized to UTF-8 on Windows and the locale on
* other operating systems.
*/ */
static VALUE static VALUE
get_default_external(VALUE klass) get_default_external(VALUE klass)

7
ruby.c
View File

@ -1819,6 +1819,9 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
Init_ruby_description(); Init_ruby_description();
Init_enc(); Init_enc();
lenc = rb_locale_encoding(); lenc = rb_locale_encoding();
#if UTF8_PATH
uenc = rb_utf8_encoding();
#endif
rb_enc_associate(rb_progname, lenc); rb_enc_associate(rb_progname, lenc);
rb_obj_freeze(rb_progname); rb_obj_freeze(rb_progname);
parser = rb_parser_new(); parser = rb_parser_new();
@ -1839,7 +1842,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
enc = rb_enc_from_index(opt->ext.enc.index); enc = rb_enc_from_index(opt->ext.enc.index);
} }
else { else {
enc = lenc; enc = IF_UTF8_PATH(uenc, lenc);
} }
rb_enc_set_default_external(rb_enc_from_encoding(enc)); rb_enc_set_default_external(rb_enc_from_encoding(enc));
if (opt->intern.enc.index >= 0) { if (opt->intern.enc.index >= 0) {
@ -1944,7 +1947,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
enc = rb_enc_from_index(opt->ext.enc.index); enc = rb_enc_from_index(opt->ext.enc.index);
} }
else { else {
enc = lenc; enc = IF_UTF8_PATH(uenc, lenc);
} }
rb_enc_set_default_external(rb_enc_from_encoding(enc)); rb_enc_set_default_external(rb_enc_from_encoding(enc));
if (opt->intern.enc.index >= 0) { if (opt->intern.enc.index >= 0) {

View File

@ -58,8 +58,8 @@ describe 'The -K command line option' do
end end
it "ignores unknown codes" do it "ignores unknown codes" do
locale = Encoding.find('locale') external = Encoding.find('external')
ruby_exe(@test_string, options: '-KZ').should == ruby_exe(@test_string, options: '-KZ').should ==
[Encoding::UTF_8.name, locale.name, nil].inspect [Encoding::UTF_8.name, external.name, nil].inspect
end end
end end

View File

@ -9,7 +9,7 @@ describe "StringIO#binmode" do
it "changes external encoding to BINARY" do it "changes external encoding to BINARY" do
io = StringIO.new io = StringIO.new
io.external_encoding.should == Encoding.find('locale') io.external_encoding.should == Encoding.find('external')
io.binmode io.binmode
io.external_encoding.should == Encoding::BINARY io.external_encoding.should == Encoding::BINARY
end end