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

ruby-changes:7232

From: akr <ko1@a...>
Date: Thu, 21 Aug 2008 19:07:35 +0900 (JST)
Subject: [ruby-changes:7232] Ruby:r18751 (trunk): * io.c (io_extract_encoding_option): if internal encoding is not

akr	2008-08-21 19:07:21 +0900 (Thu, 21 Aug 2008)

  New Revision: 18751

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

  Log:
    * io.c (io_extract_encoding_option): if internal encoding is not
      specified, enc is external encoding.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18750)
+++ ChangeLog	(revision 18751)
@@ -1,3 +1,8 @@
+Thu Aug 21 19:05:40 2008  Tanaka Akira  <akr@f...>
+
+	* io.c (io_extract_encoding_option): if internal encoding is not
+	  specified, enc is external encoding.
+
 Thu Aug 21 14:22:50 2008  Shugo Maeda  <shugo@r...>
 
 	* strftime.c: include ruby/config.h instead of ruby/ruby.h.
Index: io.c
===================================================================
--- io.c	(revision 18750)
+++ io.c	(revision 18751)
@@ -3782,9 +3782,12 @@
 	    }
 	    else {
 		*enc_p = intencoding;
+                *enc2_p = extencoding;
 	    }
 	}
-	*enc2_p = extencoding;
+        else {
+            *enc_p = extencoding;
+        }
     }
     else {
 	if (!NIL_P(intenc)) {
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 18750)
+++ test/ruby/test_io_m17n.rb	(revision 18751)
@@ -75,6 +75,26 @@
     }
   end
 
+  def test_open_r_enc_in_opt
+    with_tmpdir {
+      generate_file('tmp', "")
+      open("tmp", "r", encoding: "euc-jp") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(nil, f.internal_encoding)
+      }
+    }
+  end
+
+  def test_open_r_enc_in_opt2
+    with_tmpdir {
+      generate_file('tmp', "")
+      open("tmp", "r", external_encoding: "euc-jp") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(nil, f.internal_encoding)
+      }
+    }
+  end
+
   def test_open_r_enc_enc
     with_tmpdir {
       generate_file('tmp', "")
@@ -85,6 +105,26 @@
     }
   end
 
+  def test_open_r_enc_enc_in_opt
+    with_tmpdir {
+      generate_file('tmp', "")
+      open("tmp", "r", encoding: "euc-jp:utf-8") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(Encoding::UTF_8, f.internal_encoding)
+      }
+    }
+  end
+
+  def test_open_r_enc_enc_in_opt2
+    with_tmpdir {
+      generate_file('tmp', "")
+      open("tmp", "r", external_encoding: "euc-jp", internal_encoding: "utf-8") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(Encoding::UTF_8, f.internal_encoding)
+      }
+    }
+  end
+
   def test_open_w
     with_tmpdir {
       open("tmp", "w") {|f|
@@ -112,6 +152,24 @@
     }
   end
 
+  def test_open_w_enc_in_opt
+    with_tmpdir {
+      open("tmp", "w", encoding: "euc-jp") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(nil, f.internal_encoding)
+      }
+    }
+  end
+
+  def test_open_w_enc_in_opt2
+    with_tmpdir {
+      open("tmp", "w", external_encoding: "euc-jp") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(nil, f.internal_encoding)
+      }
+    }
+  end
+
   def test_open_w_enc_enc
     with_tmpdir {
       open("tmp", "w:euc-jp:utf-8") {|f|
@@ -121,6 +179,24 @@
     }
   end
 
+  def test_open_w_enc_enc_in_opt
+    with_tmpdir {
+      open("tmp", "w", encoding: "euc-jp:utf-8") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(Encoding::UTF_8, f.internal_encoding)
+      }
+    }
+  end
+
+  def test_open_w_enc_enc_in_opt2
+    with_tmpdir {
+      open("tmp", "w", external_encoding: "euc-jp", internal_encoding: "utf-8") {|f|
+        assert_equal(Encoding::EUC_JP, f.external_encoding)
+        assert_equal(Encoding::UTF_8, f.internal_encoding)
+      }
+    }
+  end
+
   def test_open_w_enc_enc_perm
     with_tmpdir {
       open("tmp", "w:euc-jp:utf-8", 0600) {|f|
@@ -512,7 +588,7 @@
     }
   end
 
-  def test_getc_invalid
+  def test_getc_invalid2
     with_pipe("utf-8:euc-jp") {|r, w|
       before1 = "\u{3042}"
       before2 = "\u{3044}"
@@ -530,7 +606,7 @@
     }
   end
 
-  def test_getc_invalid2
+  def test_getc_invalid3
     with_pipe("utf-16le:euc-jp") {|r, w|
       before1 = "\x42\x30".force_encoding("utf-16le")
       before2 = "\x44\x30".force_encoding("utf-16le")
@@ -690,5 +766,205 @@
     }
   end
 
+  def test_popen_r_enc
+    IO.popen("#{EnvUtil.rubybin} -e 'putc 255'", "r:ascii-8bit") {|f|
+      assert_equal(Encoding::ASCII_8BIT, f.external_encoding)
+      assert_equal(nil, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::ASCII_8BIT, s.encoding)
+      assert_equal("\xff".force_encoding("ascii-8bit"), s)
+    }
+  end
+
+  def test_popen_r_enc_in_opt
+    IO.popen("#{EnvUtil.rubybin} -e 'putc 255'", "r", encoding: "ascii-8bit") {|f|
+      assert_equal(Encoding::ASCII_8BIT, f.external_encoding)
+      assert_equal(nil, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::ASCII_8BIT, s.encoding)
+      assert_equal("\xff".force_encoding("ascii-8bit"), s)
+    }
+  end
+
+  def test_popen_r_enc_in_opt2
+    IO.popen("#{EnvUtil.rubybin} -e 'putc 255'", "r", external_encoding: "ascii-8bit") {|f|
+      assert_equal(Encoding::ASCII_8BIT, f.external_encoding)
+      assert_equal(nil, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::ASCII_8BIT, s.encoding)
+      assert_equal("\xff".force_encoding("ascii-8bit"), s)
+    }
+  end
+
+  def test_popen_r_enc_enc
+    IO.popen("#{EnvUtil.rubybin} -e 'putc 0xa1'", "r:shift_jis:euc-jp") {|f|
+      assert_equal(Encoding::Shift_JIS, f.external_encoding)
+      assert_equal(Encoding::EUC_JP, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::EUC_JP, s.encoding)
+      assert_equal("\x8e\xa1".force_encoding("euc-jp"), s)
+    }
+  end
+
+  def test_popen_r_enc_enc_in_opt
+    IO.popen("#{EnvUtil.rubybin} -e 'putc 0xa1'", "r", encoding: "shift_jis:euc-jp") {|f|
+      assert_equal(Encoding::Shift_JIS, f.external_encoding)
+      assert_equal(Encoding::EUC_JP, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::EUC_JP, s.encoding)
+      assert_equal("\x8e\xa1".force_encoding("euc-jp"), s)
+    }
+  end
+
+  def test_popen_r_enc_enc_in_opt2
+    IO.popen("#{EnvUtil.rubybin} -e 'putc 0xa1'", "r", external_encoding: "shift_jis", internal_encoding: "euc-jp") {|f|
+      assert_equal(Encoding::Shift_JIS, f.external_encoding)
+      assert_equal(Encoding::EUC_JP, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::EUC_JP, s.encoding)
+      assert_equal("\x8e\xa1".force_encoding("euc-jp"), s)
+    }
+  end
+
+  def test_popenv_r_enc_enc_in_opt2
+    IO.popen([EnvUtil.rubybin, "-e", "putc 0xa1"], "r", external_encoding: "shift_jis", internal_encoding: "euc-jp") {|f|
+      assert_equal(Encoding::Shift_JIS, f.external_encoding)
+      assert_equal(Encoding::EUC_JP, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::EUC_JP, s.encoding)
+      assert_equal("\x8e\xa1".force_encoding("euc-jp"), s)
+    }
+  end
+
+  def test_open_pipe_r_enc
+    open("|#{EnvUtil.rubybin} -e 'putc 255'", "r:ascii-8bit") {|f|
+      assert_equal(Encoding::ASCII_8BIT, f.external_encoding)
+      assert_equal(nil, f.internal_encoding)
+      s = f.read
+      assert_equal(Encoding::ASCII_8BIT, s.encoding)
+      assert_equal("\xff".force_encoding("ascii-8bit"), s)
+    }
+  end
+
+  def test_s_foreach_enc
+    with_tmpdir {
+      generate_file("t", "\xff")
+      IO.foreach("t", :mode => "r:ascii-8bit") {|s|
+        assert_equal(Encoding::ASCII_8BIT, s.encoding)
+        assert_equal("\xff".force_encoding("ascii-8bit"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_enc_in_opt
+    with_tmpdir {
+      generate_file("t", "\xff")
+      IO.foreach("t", :encoding => "ascii-8bit") {|s|
+        assert_equal(Encoding::ASCII_8BIT, s.encoding)
+        assert_equal("\xff".force_encoding("ascii-8bit"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_enc_in_opt2
+    with_tmpdir {
+      generate_file("t", "\xff")
+      IO.foreach("t", :external_encoding => "ascii-8bit") {|s|
+        assert_equal(Encoding::ASCII_8BIT, s.encoding)
+        assert_equal("\xff".force_encoding("ascii-8bit"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_enc_enc
+    with_tmpdir {
+      generate_file("t", "\u3042")
+      IO.foreach("t", :mode => "r:utf-8:euc-jp") {|s|
+        assert_equal(Encoding::EUC_JP, s.encoding)
+        assert_equal("\xa4\xa2".force_encoding("euc-jp"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_enc_enc_in_opt
+    with_tmpdir {
+      generate_file("t", "\u3042")
+      IO.foreach("t", :mode => "r", :encoding => "utf-8:euc-jp") {|s|
+        assert_equal(Encoding::EUC_JP, s.encoding)
+        assert_equal("\xa4\xa2".force_encoding("euc-jp"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_enc_enc_in_opt2
+    with_tmpdir {
+      generate_file("t", "\u3042")
+      IO.foreach("t", :mode => "r", :external_encoding => "utf-8", :internal_encoding => "euc-jp") {|s|
+        assert_equal(Encoding::EUC_JP, s.encoding)
+        assert_equal("\xa4\xa2".force_encoding("euc-jp"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_open_args_enc
+    with_tmpdir {
+      generate_file("t", "\xff")
+      IO.foreach("t", :open_args => ["r:ascii-8bit"]) {|s|
+        assert_equal(Encoding::ASCII_8BIT, s.encoding)
+        assert_equal("\xff".force_encoding("ascii-8bit"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_open_args_enc_in_opt
+    with_tmpdir {
+      generate_file("t", "\xff")
+      IO.foreach("t", :open_args => ["r", encoding: "ascii-8bit"]) {|s|
+        assert_equal(Encoding::ASCII_8BIT, s.encoding)
+        assert_equal("\xff".force_encoding("ascii-8bit"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_open_args_enc_in_opt2
+    with_tmpdir {
+      generate_file("t", "\xff")
+      IO.foreach("t", :open_args => ["r", external_encoding: "ascii-8bit"]) {|s|
+        assert_equal(Encoding::ASCII_8BIT, s.encoding)
+        assert_equal("\xff".force_encoding("ascii-8bit"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_open_args_enc_enc
+    with_tmpdir {
+      generate_file("t", "\u3042")
+      IO.foreach("t", :open_args => ["r:utf-8:euc-jp"]) {|s|
+        assert_equal(Encoding::EUC_JP, s.encoding)
+        assert_equal("\xa4\xa2".force_encoding("euc-jp"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_open_args_enc_enc_in_opt
+    with_tmpdir {
+      generate_file("t", "\u3042")
+      IO.foreach("t", :open_args => ["r", encoding: "utf-8:euc-jp"]) {|s|
+        assert_equal(Encoding::EUC_JP, s.encoding)
+        assert_equal("\xa4\xa2".force_encoding("euc-jp"), s)
+      }
+    }
+  end
+
+  def test_s_foreach_open_args_enc_enc_in_opt2
+    with_tmpdir {
+      generate_file("t", "\u3042")
+      IO.foreach("t", :open_args => ["r", external_encoding: "utf-8", internal_encoding: "euc-jp"]) {|s|
+        assert_equal(Encoding::EUC_JP, s.encoding)
+        assert_equal("\xa4\xa2".force_encoding("euc-jp"), s)
+      }
+    }
+  end
+
 end
 

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

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