Skip to content

Commit

Permalink
open3.rb don't use keyword splat (**).
Browse files Browse the repository at this point in the history
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.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66352 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
akr committed Dec 12, 2018
1 parent 914a290 commit c8cb056
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
101 changes: 86 additions & 15 deletions lib/open3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ module Open3
# 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
Expand Down Expand Up @@ -136,7 +142,13 @@ def popen3(*cmd, **opts, &block)
# 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
Expand Down Expand Up @@ -179,7 +191,13 @@ def popen2(*cmd, **opts, &block)
# }
# }
#
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
Expand All @@ -192,10 +210,6 @@ def popen2e(*cmd, **opts, &block)
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)
Expand Down Expand Up @@ -254,7 +268,16 @@ class << self
# 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
Expand Down Expand Up @@ -306,7 +329,16 @@ def capture3(*cmd, stdin_data: '', binmode: false, **opts)
# 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
Expand Down Expand Up @@ -345,7 +377,16 @@ def capture2(*cmd, stdin_data: nil, binmode: false, **opts)
# # 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
Expand Down Expand Up @@ -410,7 +451,13 @@ def capture2e(*cmd, stdin_data: nil, binmode: false, **opts)
# 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
Expand Down Expand Up @@ -460,7 +507,13 @@ def pipeline_rw(*cmds, **opts, &block)
# 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

Expand Down Expand Up @@ -496,7 +549,13 @@ def pipeline_r(*cmds, **opts, &block)
# 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
Expand Down Expand Up @@ -549,7 +608,13 @@ def pipeline_w(*cmds, **opts, &block)
# 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
Expand Down Expand Up @@ -611,7 +676,13 @@ def pipeline_start(*cmds, **opts, &block)
# # 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)
}
Expand Down
5 changes: 5 additions & 0 deletions test/test_open3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,9 @@ def test_pipeline
}
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

0 comments on commit c8cb056

Please sign in to comment.