[前][次][番号順一覧][スレッド一覧]

ruby-changes:59265

From: Nobuyoshi <ko1@a...>
Date: Sun, 15 Dec 2019 23:17:21 +0900 (JST)
Subject: [ruby-changes:59265] e8c62836a6 (master): IO#set_encoding_by_bom should err when encoding is already set

https://git.ruby-lang.org/ruby.git/commit/?id=e8c62836a6

From e8c62836a6292bf2f691de458b24ea50c51b452a Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 15 Dec 2019 23:13:16 +0900
Subject: IO#set_encoding_by_bom should err when encoding is already set

Except for ASCII-8BIT.  [Bug #16422]

diff --git a/io.c b/io.c
index 63d8172..eaaccbb 100644
--- a/io.c
+++ b/io.c
@@ -8334,6 +8334,10 @@ rb_io_set_encoding_by_bom(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L8334
     if (fptr->encs.enc2) {
         rb_raise(rb_eArgError, "encoding conversion is set");
     }
+    else if (fptr->encs.enc && fptr->encs.enc != rb_ascii8bit_encoding()) {
+        rb_raise(rb_eArgError, "encoding is set to %s already",
+                 rb_enc_name(fptr->encs.enc));
+    }
     if (!io_set_encoding_by_bom(io)) return Qnil;
     return rb_enc_from_encoding(fptr->encs.enc);
 }
diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb
index 022ff33..e5b0ef0 100644
--- a/test/ruby/test_io_m17n.rb
+++ b/test/ruby/test_io_m17n.rb
@@ -2102,6 +2102,9 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L2102
         File.open(path, "rb") {|f|
           assert_equal(Encoding.find(name), f.set_encoding_by_bom)
         }
+        File.open(path, "rb", encoding: "iso-8859-1") {|f|
+          assert_raise(ArgumentError) {f.set_encoding_by_bom}
+        }
       }
     end
   end
@@ -2114,6 +2117,10 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L2117
       bug3407 = '[ruby-core:30641]'
       result = File.read(path, encoding: 'BOM|UTF-8')
       assert_equal("a", result.b, bug3407)
+
+      File.open(path, "rb", encoding: "iso-8859-1") {|f|
+        assert_raise(ArgumentError) {f.set_encoding_by_bom}
+      }
     }
   end
 
@@ -2148,6 +2155,9 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L2155
       File.open(path, "rb") {|f|
         assert_nil(f.set_encoding_by_bom)
       }
+      File.open(path, "rb", encoding: "iso-8859-1") {|f|
+        assert_raise(ArgumentError) {f.set_encoding_by_bom}
+      }
     }
   end
 
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]