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

ruby-changes:37880

From: nobu <ko1@a...>
Date: Fri, 13 Mar 2015 15:03:41 +0900 (JST)
Subject: [ruby-changes:37880] nobu:r49961 (trunk): io.c: don't raise after close

nobu	2015-03-13 15:03:25 +0900 (Fri, 13 Mar 2015)

  New Revision: 49961

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

  Log:
    io.c: don't raise after close
    
    * io.c (rb_io_close_read, rb_io_close_write): don't raise after
      close same as IO#close.

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/test/ruby/test_io.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49960)
+++ ChangeLog	(revision 49961)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Mar 13 15:03:20 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* io.c (rb_io_close_read, rb_io_close_write): don't raise after
+	  close same as IO#close.
+
 Fri Mar 13 12:29:07 2015  Tanaka Akira  <akr@f...>
 
 	* test/readline/test_readline.rb: Restore environment variables:
Index: io.c
===================================================================
--- io.c	(revision 49960)
+++ io.c	(revision 49961)
@@ -4528,7 +4528,8 @@ rb_io_close_read(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L4528
     rb_io_t *fptr;
     VALUE write_io;
 
-    GetOpenFile(io, fptr);
+    fptr = rb_io_get_fptr(rb_io_taint_check(io));
+    if (fptr->fd < 0) return Qnil;
     if (is_socket(fptr->fd, fptr->pathv)) {
 #ifndef SHUT_RD
 # define SHUT_RD 0
@@ -4544,20 +4545,19 @@ rb_io_close_read(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L4545
     write_io = GetWriteIO(io);
     if (io != write_io) {
 	rb_io_t *wfptr;
-	GetOpenFile(write_io, wfptr);
+	wfptr = rb_io_get_fptr(rb_io_taint_check(write_io));
 	wfptr->pid = fptr->pid;
 	fptr->pid = 0;
         RFILE(io)->fptr = wfptr;
 	/* bind to write_io temporarily to get rid of memory/fd leak */
 	fptr->tied_io_for_writing = 0;
-	fptr->mode &= ~FMODE_DUPLEX;
 	RFILE(write_io)->fptr = fptr;
 	rb_io_fptr_cleanup(fptr, FALSE);
 	/* should not finalize fptr because another thread may be reading it */
         return Qnil;
     }
 
-    if (fptr->mode & FMODE_WRITABLE) {
+    if ((fptr->mode & (FMODE_DUPLEX|FMODE_WRITABLE)) == FMODE_WRITABLE) {
 	rb_raise(rb_eIOError, "closing non-duplex IO for reading");
     }
     return rb_io_close(io);
@@ -4589,7 +4589,8 @@ rb_io_close_write(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L4589
     VALUE write_io;
 
     write_io = GetWriteIO(io);
-    GetOpenFile(write_io, fptr);
+    fptr = rb_io_get_fptr(rb_io_taint_check(write_io));
+    if (fptr->fd < 0) return Qnil;
     if (is_socket(fptr->fd, fptr->pathv)) {
 #ifndef SHUT_WR
 # define SHUT_WR 1
@@ -4602,14 +4603,13 @@ rb_io_close_write(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L4603
         return Qnil;
     }
 
-    if (fptr->mode & FMODE_READABLE) {
+    if ((fptr->mode & (FMODE_DUPLEX|FMODE_READABLE)) == FMODE_READABLE) {
 	rb_raise(rb_eIOError, "closing non-duplex IO for writing");
     }
 
     if (io != write_io) {
-	GetOpenFile(io, fptr);
+	fptr = rb_io_get_fptr(rb_io_taint_check(io));
 	fptr->tied_io_for_writing = 0;
-	fptr->mode &= ~FMODE_DUPLEX;
     }
     rb_io_close(write_io);
     return Qnil;
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 49960)
+++ test/ruby/test_io.rb	(revision 49961)
@@ -1364,6 +1364,9 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1364
       f.close_read
       f.write "foobarbaz"
       assert_raise(IOError) { f.read }
+      assert_nothing_raised(IOError) {f.close_read}
+      assert_nothing_raised(IOError) {f.close}
+      assert_nothing_raised(IOError) {f.close_read}
     end
   end
 
@@ -1371,6 +1374,9 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1374
     with_pipe do |r, w|
       r.close_read
       assert_raise(Errno::EPIPE) { w.write "foobarbaz" }
+      assert_nothing_raised(IOError) {r.close_read}
+      assert_nothing_raised(IOError) {r.close}
+      assert_nothing_raised(IOError) {r.close_read}
     end
   end
 
@@ -1398,6 +1404,9 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1404
       f.write "foobarbaz"
       f.close_write
       assert_equal("foobarbaz", f.read)
+      assert_nothing_raised(IOError) {f.close_write}
+      assert_nothing_raised(IOError) {f.close}
+      assert_nothing_raised(IOError) {f.close_write}
     end
   end
 

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

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