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

ruby-changes:48122

From: akr <ko1@a...>
Date: Sat, 21 Oct 2017 16:01:04 +0900 (JST)
Subject: [ruby-changes:48122] akr:r60236 (trunk): lib/open3.rb: accept IO-like object for :stdin_data argument.

akr	2017-10-21 16:00:58 +0900 (Sat, 21 Oct 2017)

  New Revision: 60236

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

  Log:
    lib/open3.rb: accept IO-like object for :stdin_data argument.
    
    Open3.capture3, Open3.capture2, Open3.capture2e accepts
    IO-like object for :stdin_data argument.
    [ruby-core:80936] [Feature #13527] proposed by janko.

  Modified files:
    trunk/lib/open3.rb
    trunk/test/test_open3.rb
Index: test/test_open3.rb
===================================================================
--- test/test_open3.rb	(revision 60235)
+++ test/test_open3.rb	(revision 60236)
@@ -155,6 +155,17 @@ class TestOpen3 < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_open3.rb#L155
     assert(s.success?)
   end
 
+  def test_capture3_stdin_data_io
+    IO.pipe {|r, w|
+      w.write "i"
+      w.close
+      o, e, s = Open3.capture3(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>r)
+      assert_equal("io", o)
+      assert_equal("ie", e)
+      assert(s.success?)
+    }
+  end
+
   def test_capture3_flip
     o, e, s = Open3.capture3(RUBY, '-e', 'STDOUT.sync=true; 1000.times { print "o"*1000; STDERR.print "e"*1000 }')
     assert_equal("o"*1000000, o)
@@ -168,12 +179,32 @@ class TestOpen3 < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_open3.rb#L179
     assert(s.success?)
   end
 
+  def test_capture2_stdin_data_io
+    IO.pipe {|r, w|
+      w.write "i"
+      w.close
+      o, s = Open3.capture2(RUBY, '-e', 'i=STDIN.read; print i+"o"', :stdin_data=>r)
+      assert_equal("io", o)
+      assert(s.success?)
+    }
+  end
+
   def test_capture2e
     oe, s = Open3.capture2e(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>"i")
     assert_equal("ioie", oe)
     assert(s.success?)
   end
 
+  def test_capture2e_stdin_data_io
+    IO.pipe {|r, w|
+      w.write "i"
+      w.close
+      oe, s = Open3.capture2e(RUBY, '-e', 'i=STDIN.read; print i+"o"; STDOUT.flush; STDERR.print i+"e"', :stdin_data=>r)
+      assert_equal("ioie", oe)
+      assert(s.success?)
+    }
+  end
+
   def test_capture3_stdin_data
     o, e, s = Open3.capture3(RUBY, '-e', '', :stdin_data=>"z"*(1024*1024))
     assert_equal("", o)
Index: lib/open3.rb
===================================================================
--- lib/open3.rb	(revision 60235)
+++ lib/open3.rb	(revision 60236)
@@ -264,7 +264,11 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L264
       out_reader = Thread.new { o.read }
       err_reader = Thread.new { e.read }
       begin
-        i.write stdin_data
+        if stdin_data.respond_to? :readpartial
+          IO.copy_stream(stdin_data, i)
+        else
+          i.write stdin_data
+        end
       rescue Errno::EPIPE
       end
       i.close
@@ -311,7 +315,11 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L315
       out_reader = Thread.new { o.read }
       if stdin_data
         begin
-          i.write stdin_data
+          if stdin_data.respond_to? :readpartial
+            IO.copy_stream(stdin_data, i)
+          else
+            i.write stdin_data
+          end
         rescue Errno::EPIPE
         end
       end
@@ -346,7 +354,11 @@ module Open3 https://github.com/ruby/ruby/blob/trunk/lib/open3.rb#L354
       outerr_reader = Thread.new { oe.read }
       if stdin_data
         begin
-          i.write stdin_data
+          if stdin_data.respond_to? :readpartial
+            IO.copy_stream(stdin_data, i)
+          else
+            i.write stdin_data
+          end
         rescue Errno::EPIPE
         end
       end

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

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