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

ruby-changes:7238

From: akr <ko1@a...>
Date: Fri, 22 Aug 2008 02:12:17 +0900 (JST)
Subject: [ruby-changes:7238] Ruby:r18756 (trunk): * io.c (rb_io_initialize): accept hash argument.

akr	2008-08-22 02:09:56 +0900 (Fri, 22 Aug 2008)

  New Revision: 18756

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

  Log:
    * io.c (rb_io_initialize): accept hash argument.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18755)
+++ ChangeLog	(revision 18756)
@@ -1,3 +1,7 @@
+Fri Aug 22 02:08:58 2008  Tanaka Akira  <akr@f...>
+
+	* io.c (rb_io_initialize): accept hash argument.
+
 Thu Aug 21 23:51:51 2008  Shugo Maeda  <shugo@r...>
 
 	* strftime.c (rb_strftime): supported %F and %<precision>N.
Index: io.c
===================================================================
--- io.c	(revision 18755)
+++ io.c	(revision 18756)
@@ -5472,31 +5472,31 @@
     VALUE fnum, mode, orig;
     rb_io_t *fp, *ofp = NULL;
     int fd, flags, modenum = O_RDONLY;
+    convconfig_t convconfig;
+    VALUE opt;
 
     rb_secure(4);
+
+    opt = pop_last_hash(&argc, &argv);
     rb_scan_args(argc, argv, "11", &fnum, &mode);
-    if (argc == 2) {
-	if (FIXNUM_P(mode)) {
-	    modenum = FIX2LONG(mode);
-	}
-	else {
-	    SafeStringValue(mode);
-	    modenum = rb_io_mode_modenum(StringValueCStr(mode));
-	}
-    }
+    rb_io_extract_modeenc(mode, opt, &modenum, &flags, &convconfig);
     orig = rb_io_check_io(fnum);
     if (NIL_P(orig)) {
 	fd = NUM2INT(fnum);
         UPDATE_MAXFD(fd);
-	if (argc != 2) {
+	if (NIL_P(mode)) {
 #if defined(HAVE_FCNTL) && defined(F_GETFL)
 	    modenum = fcntl(fd, F_GETFL);
 	    if (modenum == -1) rb_sys_fail(0);
+            flags = rb_io_modenum_flags(modenum);
 #endif
 	}
 	MakeOpenFile(io, fp);
         fp->fd = fd;
-	fp->mode = rb_io_modenum_flags(modenum);
+	fp->mode = flags;
+        fp->enc = convconfig.enc;
+        fp->enc2 = convconfig.enc2;
+        clear_codeconv(fp);
         io_check_tty(fp);
     }
     else if (RFILE(io)->fptr) {
@@ -5508,8 +5508,7 @@
 	    VALUE s = rb_inspect(orig);
 	    rb_raise(rb_eIOError, "too many shared IO for %s", StringValueCStr(s));
 	}
-	if (argc == 2) {
-	    flags = rb_io_modenum_flags(modenum);
+	if (!NIL_P(mode)) {
 	    if ((ofp->mode ^ flags) & (FMODE_READWRITE|FMODE_BINMODE)) {
 		if (FIXNUM_P(mode)) {
 		    rb_raise(rb_eArgError, "incompatible mode 0x%x", modenum);
@@ -5519,6 +5518,9 @@
 		}
 	    }
 	}
+        if (convconfig.enc || convconfig.enc2) {
+            rb_raise(rb_eArgError, "encoding specified for shared IO");
+        }
 	ofp->refcnt++;
 	RFILE(io)->fptr = ofp;
     }
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 18755)
+++ test/ruby/test_io_m17n.rb	(revision 18756)
@@ -206,6 +206,19 @@
     }
   end
 
+  def test_io_new_enc
+    with_tmpdir {
+      generate_file("tmp", "\xa1")
+      fd = IO.sysopen("tmp")
+      f = IO.new(fd, "r:sjis")
+      begin
+        assert_equal(Encoding::Shift_JIS, f.read.encoding)
+      ensure
+        f.close
+      end
+    }
+  end
+
   def test_stdin
     assert_equal(Encoding.default_external, STDIN.external_encoding)
     assert_equal(nil, STDIN.internal_encoding)

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

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