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

ruby-changes:48421

From: nobu <ko1@a...>
Date: Sun, 29 Oct 2017 14:46:28 +0900 (JST)
Subject: [ruby-changes:48421] nobu:r60535 (trunk): io.c: honor buffered mode

nobu	2017-10-29 14:46:23 +0900 (Sun, 29 Oct 2017)

  New Revision: 60535

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

  Log:
    io.c: honor buffered mode
    
    * io.c (io_writev): honor buffered mode to get rid of broken pipe
      error when stdout is redirected to a pipeline.
      [ruby-core:83578] [Feature #14042]

  Modified files:
    trunk/io.c
    trunk/test/ruby/test_io.rb
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 60534)
+++ test/ruby/test_io.rb	(revision 60535)
@@ -1274,6 +1274,15 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L1274
     assert_in_out_err([], "STDOUT.write(:foo, :bar)", ["foobar"])
   end
 
+  def test_write_buffered_with_multiple_arguments
+    out, err, (_, status) = EnvUtil.invoke_ruby(["-e", "sleep 0.1;puts 'foo'"], "", true, true) do |_, o, e, i|
+      [o.read, e.read, Process.waitpid2(i)]
+    end
+    assert_predicate(status, :success?)
+    assert_equal("foo\n", out)
+    assert_empty(err)
+  end
+
   def test_write_non_writable
     with_pipe do |r, w|
       assert_raise(IOError) do
Index: io.c
===================================================================
--- io.c	(revision 60534)
+++ io.c	(revision 60535)
@@ -1637,12 +1637,16 @@ io_writev(int argc, VALUE *argv, VALUE i https://github.com/ruby/ruby/blob/trunk/io.c#L1637
 
     for (i = 0; i < argc; i += cnt) {
 #ifdef HAVE_WRITEV
-	if ((cnt = argc - i) >= IOV_MAX) cnt = IOV_MAX-1;
-	n = io_fwritev(cnt, &argv[i], fptr);
-#else
-	/* sync at last item */
-	n = io_fwrite(argv[i], fptr, (i < argc-1));
+	if ((fptr->mode & (FMODE_SYNC|FMODE_TTY)) && ((cnt = argc - i) < IOV_MAX)) {
+	    n = io_fwritev(cnt, &argv[i], fptr);
+	}
+	else
 #endif
+	{
+	    cnt = 1;
+	    /* sync at last item */
+	    n = io_fwrite(rb_obj_as_string(argv[i]), fptr, (i < argc-1));
+	}
 	if (n == -1L) rb_sys_fail_path(fptr->pathv);
 	total = rb_fix_plus(LONG2FIX(n), total);
     }

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

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