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

ruby-changes:7645

From: akr <ko1@a...>
Date: Sat, 6 Sep 2008 07:29:58 +0900 (JST)
Subject: [ruby-changes:7645] Ruby:r19166 (trunk): * io.c (rb_io_extract_modeenc): raise an error for ASCII incompatible

akr	2008-09-06 07:26:39 +0900 (Sat, 06 Sep 2008)

  New Revision: 19166

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19166

  Log:
    * io.c (rb_io_extract_modeenc): raise an error for ASCII incompatible
      encoding without binmode.

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/test/ruby/test_io_m17n.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19165)
+++ ChangeLog	(revision 19166)
@@ -1,3 +1,8 @@
+Sat Sep  6 07:24:49 2008  Tanaka Akira  <akr@f...>
+
+	* io.c (rb_io_extract_modeenc): raise an error for ASCII incompatible
+	  encoding without binmode.
+
 Sat Sep  6 06:28:46 2008  Tanaka Akira  <akr@f...>
 
 	* enc/trans/escape.trans: new file.
Index: io.c
===================================================================
--- io.c	(revision 19165)
+++ io.c	(revision 19166)
@@ -3898,6 +3898,9 @@
     if ((fmode & FMODE_BINMODE) && (fmode & FMODE_TEXTMODE))
         rb_raise(rb_eArgError, "both textmode and binmode specified");
 
+    if (enc && !rb_enc_asciicompat(enc) && !(fmode & FMODE_BINMODE))
+        rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode");
+
     *vmode_p = vmode;
 
     *oflags_p = oflags;
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 19165)
+++ test/ruby/test_io_m17n.rb	(revision 19166)
@@ -1227,24 +1227,74 @@
 
   def test_textmode_read_ascii_incompat_internal
     with_tmpdir {
+      # ascii incompatible internal encoding needs binmode.
+      assert_raise(ArgumentError) {
+        open("t.utf8.crlf", "rt:utf-8:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf8.crlf", "r:utf-8:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf16.crlf", "rt:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf16.crlf", "r:utf-16be") {|f| }
+      }
+    }
+  end
+
+  def test_binmode_read_ascii_incompat_internal
+    with_tmpdir {
       generate_file("t.utf8.crlf", "a\r\nb\r\n")
-      open("t.utf8.crlf", "rt:utf-8:utf-16be") {|f|
+      generate_file("t.utf16.crlf", "\0a\0\r\0\n\0b\0\r\0\n")
+      # ascii incompatible internal encoding needs binmode.
+      open("t.utf8.crlf", "rb:utf-8:utf-16be") {|f|
         content = f.read
-        # textmode doesn't affect for ascii incompatible internal encoding.
         assert_equal("\0a\0\r\0\n\0b\0\r\0\n".force_encoding("UTF-16BE"),
                      content)
       }
+      open("t.utf16.crlf", "rb:utf-16be") {|f|
+        content = f.read
+        assert_equal("\0a\0\r\0\n\0b\0\r\0\n".force_encoding("UTF-16BE"),
+                     content)
+      }
     }
   end
 
   def test_textmode_write_ascii_incompat_internal
     with_tmpdir {
-      open("t.utf8.lf", "wt:utf-8:utf-16be") {|f|
+      # ascii incompatible internal encoding needs binmode.
+      assert_raise(ArgumentError) {
+        open("t.utf8", "wt:utf-8:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf8", "w:utf-8:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf8", "w:utf-8:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf16", "wt:utf-16be") {|f| }
+      }
+      assert_raise(ArgumentError) {
+        open("t.utf16", "w:utf-16be") {|f| }
+      }
+    }
+  end
+
+  def test_binmode_write_ascii_incompat_internal
+    with_tmpdir {
+      open("t.utf8.lf", "wb:utf-8:utf-16be") {|f|
         f.print "\0a\0\n\0b\0\n".force_encoding("UTF-16BE")
       }
       content = File.read("t.utf8.lf", :mode=>"rb:ascii-8bit")
-      # textmode doesn't affect for ascii incompatible internal encoding.
       assert_equal("a\nb\n", content)
+
+      open("t.utf8.lf", "wb:utf-16be") {|f|
+        f.print "\0a\0\n\0b\0\n".force_encoding("UTF-16BE")
+      }
+      content = File.read("t.utf8.lf", :mode=>"rb:ascii-8bit")
+      assert_equal("\0a\0\n\0b\0\n", content)
     }
   end
 

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

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