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

ruby-changes:54131

From: akr <ko1@a...>
Date: Wed, 12 Dec 2018 15:48:50 +0900 (JST)
Subject: [ruby-changes:54131] akr:r66352 (trunk): open3.rb don't use keyword splat (**).

akr	2018-12-12 15:48:46 +0900 (Wed, 12 Dec 2018)

  New Revision: 66352

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

  Log:
    open3.rb don't use keyword splat (**).
    
    revert r43582, r49173 and r49177.
    
    open3 arguments uses spawn-like keyword arguments.
    Both symbol and integer keys are used.
    ```
    Open3.capture2(*command, :in => IO::NULL, 3 => IO::NULL)
    ``
    
    This style cannot be supported with keyword splat (**) since Ruby 2.6.
    Because Ruby 2.6 prohibits symbol/non-symbol key hash separation.

  Modified files:
    trunk/lib/open3.rb
    trunk/test/test_open3.rb
Index: lib/open3.rb
===================================================================
--- lib/open3.rb	(revision 66351)
+++ lib/open3.rb	(revision 66352)
@@ -81,7 +81,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L81
   # If merged stdout and stderr output is not a problem, you can use Open3.popen2e.
   # If you really need stdout and stderr output as separate strings, you can consider Open3.capture3.
   #
-  def popen3(*cmd, **opts, &block)
+  def popen3(*cmd, &block)
+    if Hash === cmd.last
+      opts = cmd.pop.dup
+    else
+      opts = {}
+    end
+
     in_r, in_w = IO.pipe
     opts[:in] = in_r
     in_w.sync = true
@@ -136,7 +142,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L142
   #     p o.read #=> "*"
   #   }
   #
-  def popen2(*cmd, **opts, &block)
+  def popen2(*cmd, &block)
+    if Hash === cmd.last
+      opts = cmd.pop.dup
+    else
+      opts = {}
+    end
+
     in_r, in_w = IO.pipe
     opts[:in] = in_r
     in_w.sync = true
@@ -179,7 +191,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L191
   #     }
   #   }
   #
-  def popen2e(*cmd, **opts, &block)
+  def popen2e(*cmd, &block)
+    if Hash === cmd.last
+      opts = cmd.pop.dup
+    else
+      opts = {}
+    end
+
     in_r, in_w = IO.pipe
     opts[:in] = in_r
     in_w.sync = true
@@ -192,10 +210,6 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L210
   module_function :popen2e
 
   def popen_run(cmd, opts, child_io, parent_io) # :nodoc:
-    if last = Hash.try_convert(cmd.last)
-      opts = opts.merge(last)
-      cmd.pop
-    end
     pid = spawn(*cmd, opts)
     wait_thr = Process.detach(pid)
     child_io.each(&:close)
@@ -254,7 +268,16 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L268
   #     STDOUT.binmode; print thumbnail
   #   end
   #
-  def capture3(*cmd, stdin_data: '', binmode: false, **opts)
+  def capture3(*cmd)
+    if Hash === cmd.last
+      opts = cmd.pop.dup
+    else
+      opts = {}
+    end
+
+    stdin_data = opts.delete(:stdin_data) || ''
+    binmode = opts.delete(:binmode)
+
     popen3(*cmd, opts) {|i, o, e, t|
       if binmode
         i.binmode
@@ -306,7 +329,16 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L329
   #   End
   #   image, s = Open3.capture2("gnuplot", :stdin_data=>gnuplot_commands, :binmode=>true)
   #
-  def capture2(*cmd, stdin_data: nil, binmode: false, **opts)
+  def capture2(*cmd)
+    if Hash === cmd.last
+      opts = cmd.pop.dup
+    else
+      opts = {}
+    end
+
+    stdin_data = opts.delete(:stdin_data)
+    binmode = opts.delete(:binmode)
+
     popen2(*cmd, opts) {|i, o, t|
       if binmode
         i.binmode
@@ -345,7 +377,16 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L377
   #   # capture make log
   #   make_log, s = Open3.capture2e("make")
   #
-  def capture2e(*cmd, stdin_data: nil, binmode: false, **opts)
+  def capture2e(*cmd)
+    if Hash === cmd.last
+      opts = cmd.pop.dup
+    else
+      opts = {}
+    end
+
+    stdin_data = opts.delete(:stdin_data)
+    binmode = opts.delete(:binmode)
+
     popen2e(*cmd, opts) {|i, oe, t|
       if binmode
         i.binmode
@@ -410,7 +451,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L451
   #     stdin.close     # send EOF to sort.
   #     p stdout.read   #=> "     1\tbar\n     2\tbaz\n     3\tfoo\n"
   #   }
-  def pipeline_rw(*cmds, **opts, &block)
+  def pipeline_rw(*cmds, &block)
+    if Hash === cmds.last
+      opts = cmds.pop.dup
+    else
+      opts = {}
+    end
+
     in_r, in_w = IO.pipe
     opts[:in] = in_r
     in_w.sync = true
@@ -460,7 +507,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L507
   #     p ts[1].value #=> #<Process::Status: pid 24913 exit 0>
   #   }
   #
-  def pipeline_r(*cmds, **opts, &block)
+  def pipeline_r(*cmds, &block)
+    if Hash === cmds.last
+      opts = cmds.pop.dup
+    else
+      opts = {}
+    end
+
     out_r, out_w = IO.pipe
     opts[:out] = out_w
 
@@ -496,7 +549,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L549
   #     i.puts "hello"
   #   }
   #
-  def pipeline_w(*cmds, **opts, &block)
+  def pipeline_w(*cmds, &block)
+    if Hash === cmds.last
+      opts = cmds.pop.dup
+    else
+      opts = {}
+    end
+
     in_r, in_w = IO.pipe
     opts[:in] = in_r
     in_w.sync = true
@@ -549,7 +608,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L608
   #     p err_r.read # error messages of pdftops and lpr.
   #   }
   #
-  def pipeline_start(*cmds, **opts, &block)
+  def pipeline_start(*cmds, &block)
+    if Hash === cmds.last
+      opts = cmds.pop.dup
+    else
+      opts = {}
+    end
+
     if block
       pipeline_run(cmds, opts, [], [], &block)
     else
@@ -611,7 +676,13 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L676
   #   #   106
   #   #   202
   #
-  def pipeline(*cmds, **opts)
+  def pipeline(*cmds)
+    if Hash === cmds.last
+      opts = cmds.pop.dup
+    else
+      opts = {}
+    end
+
     pipeline_run(cmds, opts, [], []) {|ts|
       ts.map(&:value)
     }
Index: test/test_open3.rb
===================================================================
--- test/test_open3.rb	(revision 66351)
+++ test/test_open3.rb	(revision 66352)
@@ -316,4 +316,9 @@ class TestOpen3 < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_open3.rb#L316
     }
   end
 
+  def test_integer_and_symbol_key
+    command = [RUBY, '-e', 'puts "test_integer_and_symbol_key"']
+    out, status = Open3.capture2(*command, :in => IO::NULL, 3 => IO::NULL)
+    assert_equal("test_integer_and_symbol_key\n", out)
+  end
 end

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

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