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

ruby-changes:7267

From: akr <ko1@a...>
Date: Sat, 23 Aug 2008 11:24:00 +0900 (JST)
Subject: [ruby-changes:7267] Ruby:r18786 (trunk): * io.c (rb_io_extract_modeenc): check :textmode and :binmode in option

akr	2008-08-23 11:23:42 +0900 (Sat, 23 Aug 2008)

  New Revision: 18786

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

  Log:
    * io.c (rb_io_extract_modeenc): check :textmode and :binmode in option
      hash.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18785)
+++ ChangeLog	(revision 18786)
@@ -1,3 +1,8 @@
+Sat Aug 23 11:23:05 2008  Tanaka Akira  <akr@f...>
+
+	* io.c (rb_io_extract_modeenc): check :textmode and :binmode in option
+	  hash.
+
 Sat Aug 23 10:48:56 2008  Tanaka Akira  <akr@f...>
 
 	* ext/pty/pty.c (pty_getpty): follow rb_io_t's path -> pathv change.
Index: io.c
===================================================================
--- io.c	(revision 18785)
+++ io.c	(revision 18786)
@@ -125,6 +125,7 @@
 
 static ID id_write, id_read, id_getc, id_flush, id_readpartial;
 static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
+static VALUE sym_textmode, sym_binmode;
 
 struct timeval rb_time_interval(VALUE);
 
@@ -3864,6 +3865,18 @@
     }
 
     if (!NIL_P(opthash)) {
+	VALUE v;
+	v = rb_hash_aref(opthash, sym_textmode);
+	if (RTEST(v))
+            flags |= FMODE_TEXTMODE;
+	v = rb_hash_aref(opthash, sym_binmode);
+	if (RTEST(v)) {
+            flags |= FMODE_BINMODE;
+#ifdef O_BINARY
+            modenum |= O_BINARY;
+#endif
+        }
+
         if (io_extract_encoding_option(opthash, &enc, &enc2)) {
             if (has_enc) {
                 rb_raise(rb_eArgError, "encoding sepecified twice");
@@ -3871,6 +3884,9 @@
         }
     }
 
+    if ((flags & FMODE_BINMODE) && (flags & FMODE_TEXTMODE))
+        rb_raise(rb_eArgError, "both textmode and binmode specified");
+
     *mode_p = mode;
 
     *modenum_p = modenum;
@@ -8335,4 +8351,6 @@
     sym_intenc = ID2SYM(rb_intern("internal_encoding"));
     sym_encoding = ID2SYM(rb_intern("encoding"));
     sym_open_args = ID2SYM(rb_intern("open_args"));
+    sym_textmode = ID2SYM(rb_intern("textmode"));
+    sym_binmode = ID2SYM(rb_intern("binmode"));
 }
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 18785)
+++ test/ruby/test_io_m17n.rb	(revision 18786)
@@ -979,11 +979,18 @@
     }
   end
 
+  def test_both_textmode_binmode
+    assert_raise(ArgumentError) { open("not-exist", "r", :textmode=>true, :binmode=>true) }
+  end
+
   def test_textmode_decode_universal_newline_read
     with_tmpdir {
       generate_file("t.crlf", "a\r\nb\r\nc\r\n")
       assert_equal("a\nb\nc\n", File.read("t.crlf", mode:"rt:euc-jp:utf-8"))
       assert_equal("a\nb\nc\n", File.read("t.crlf", mode:"rt"))
+      open("t.crlf", "rt:euc-jp:utf-8") {|f| assert_equal("a\nb\nc\n", f.read) }
+      open("t.crlf", "rt") {|f| assert_equal("a\nb\nc\n", f.read) }
+      open("t.crlf", "r", :textmode=>true) {|f| assert_equal("a\nb\nc\n", f.read) }
 
       generate_file("t.cr", "a\rb\rc\r")
       assert_equal("a\nb\nc\n", File.read("t.cr", mode:"rt:euc-jp:utf-8"))
@@ -1105,6 +1112,9 @@
       open("t.txt", "rb") {|f|
         assert_equal(src, f.read)
       }
+      open("t.txt", "r", :binmode=>true) {|f|
+        assert_equal(src, f.read)
+      }
       if File::BINARY == 0
         open("t.txt", "r") {|f|
           assert_equal(src, f.read)

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

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