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

ruby-changes:33747

From: akr <ko1@a...>
Date: Mon, 5 May 2014 22:37:20 +0900 (JST)
Subject: [ruby-changes:33747] akr:r45828 (trunk): * process.c (check_exec_redirect): Open the file in write mode for

akr	2014-05-05 22:37:09 +0900 (Mon, 05 May 2014)

  New Revision: 45828

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

  Log:
    * process.c (check_exec_redirect): Open the file in write mode for
      redirect from [:out, :err].
      Proposed and implemented by Yusuke Endoh.
      [ruby-dev:41430] [Feature #3348]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/process.c
    trunk/test/ruby/test_process.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45827)
+++ ChangeLog	(revision 45828)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon May  5 22:29:47 2014  Tanaka Akira  <akr@f...>
+
+	* process.c (check_exec_redirect): Open the file in write mode for
+	  redirect from [:out, :err].
+	  Proposed and implemented by Yusuke Endoh.
+	  [ruby-dev:41430] [Feature #3348]
+
 Mon May  5 21:52:35 2014  Tanaka Akira  <akr@f...>
 
 	* ext/pathname/lib/pathname.rb (cleanpath_aggressive): make all
Index: process.c
===================================================================
--- process.c	(revision 45827)
+++ process.c	(revision 45828)
@@ -1595,7 +1595,19 @@ check_exec_redirect(VALUE key, VALUE val https://github.com/ruby/ruby/blob/trunk/process.c#L1595
             key = check_exec_redirect_fd(key, 1);
         if (FIXNUM_P(key) && (FIX2INT(key) == 1 || FIX2INT(key) == 2))
             flags = INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
-        else
+        else if (TYPE(key) == T_ARRAY) {
+	    int i;
+	    for (i = 0; i < RARRAY_LEN(key); i++) {
+		VALUE v = RARRAY_PTR(key)[i];
+		VALUE fd = check_exec_redirect_fd(v, 1);
+		if (FIX2INT(fd) != 1 && FIX2INT(fd) != 2) break;
+	    }
+	    if (i == RARRAY_LEN(key))
+		flags = INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
+	    else
+		flags = INT2NUM(O_RDONLY);
+	}
+	else
             flags = INT2NUM(O_RDONLY);
         perm = INT2FIX(0644);
         param = hide_obj(rb_ary_new3(3, hide_obj(EXPORT_DUP(path)),
Index: NEWS
===================================================================
--- NEWS	(revision 45827)
+++ NEWS	(revision 45828)
@@ -27,6 +27,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L27
   * New methods
     * IO#statfs returns filesystem information as File::Statfs. (experimental)
 
+* Process
+  * Extended method:
+    * Process execution methods such as Process.spawn opens the file in write
+      mode for redirect from [:out, :err].
+
 * Symbol
   * New methods
     * Symbol.find(str) returns whether given string is defined as symbol or not.
@@ -64,6 +69,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L69
       arguments of the lambda, if just an array is yielded and its length
       matches.
 
+* Process
+  * Process execution methods such as Process.spawn opens the file in write
+    mode for redirect from [:out, :err].
+    Before Ruby 2.2, it was opened in read mode.
+
 === Stdlib updates (outstanding ones only)
 
 * Find, Pathname
Index: test/ruby/test_process.rb
===================================================================
--- test/ruby/test_process.rb	(revision 45827)
+++ test/ruby/test_process.rb	(revision 45828)
@@ -613,6 +613,17 @@ class TestProcess < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_process.rb#L613
     }
   end
 
+  def test_execopts_redirect_to_out_and_err
+    with_tmpchdir {|d|
+      ret = system(RUBY, "-e", 'STDERR.print "e"; STDOUT.print "o"', [:out, :err] => "foo")
+      assert_equal(true, ret)
+      assert_equal("eo", File.read("foo"))
+      ret = system(RUBY, "-e", 'STDERR.print "E"; STDOUT.print "O"', [:err, :out] => "bar")
+      assert_equal(true, ret)
+      assert_equal("EO", File.read("bar"))
+    }
+  end
+
   def test_execopts_redirect_dup2_child
     with_tmpchdir {|d|
       Process.wait spawn(RUBY, "-e", "STDERR.print 'err'; STDOUT.print 'out'",

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

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