From 4c50e9f3597db1c5ba462227b1e9e4a084f28ccf Mon Sep 17 00:00:00 2001 From: Adam Wiggins Date: Sun, 27 Apr 2008 15:03:31 -0700 Subject: [PATCH] IO.popen read/write pipes diff --git a/kernel/core/io.rb b/kernel/core/io.rb index e11e58d..9ae4cc8 100644 --- a/kernel/core/io.rb +++ b/kernel/core/io.rb @@ -272,8 +272,6 @@ class IO alias_method :isatty, :tty? def self.popen(str, mode = "r") - raise "TODO make this support more than r" if mode != "r" - if str == "+-+" and !block_given? raise ArgumentError, "this mode requires a block currently" end @@ -283,21 +281,41 @@ class IO pid = Process.fork if pid - ch_write.close + # Parent: read/write to the child + if mode == 'w' + pa_read.close + pa_read = nil + elsif mode == 'r' + ch_write.close + ch_write = nil + end + # See bottom for definition - rp = BidirectionalPipe.new(pid, pa_read, nil) + rp = BidirectionalPipe.new(pid, pa_read, ch_write) if block_given? begin yield rp ensure - pa_read.close + pa_read.close if pa_read + ch_write.close if ch_write + Process.kill('KILL', pid) end else return rp end else - pa_read.close - STDOUT.reopen ch_write + # Child: replace process with the requested shell command + if mode == 'w' + ch_write.close + ch_write = nil + elsif mode == 'r' + pa_read.close + pa_read = nil + end + + STDIN.reopen(pa_read) if pa_read + STDOUT.reopen(ch_write) if ch_write + if str == "+-+" yield nil else diff --git a/spec/tags/ruby/1.8/core/io/popen_tags.txt b/spec/tags/ruby/1.8/core/io/popen_tags.txt index 325615f..e0a022e 100644 --- a/spec/tags/ruby/1.8/core/io/popen_tags.txt +++ b/spec/tags/ruby/1.8/core/io/popen_tags.txt @@ -1,4 +1,2 @@ -fails:writes to a read/write pipe fails:IO#popen with block does not raise error when io closed inside the block incomplete:IO.popen needs to be reviewed for spec completeness -fails:IO#popen writes to a read/write pipe