From c5150d03031bf436a342b5a41c0065199f925e85 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 9 Apr 2012 15:36:47 -0700 Subject: [PATCH] On 1.9, Kernel.system returns nil if exec fails --- kernel/common/process.rb | 23 ---------------- kernel/common/process18.rb | 25 +++++++++++++++++ kernel/common/process19.rb | 27 +++++++++++++++++++ spec/tags/19/ruby/core/kernel/system_tags.txt | 1 - spec/tags/20/ruby/core/kernel/system_tags.txt | 2 -- 5 files changed, 52 insertions(+), 26 deletions(-) delete mode 100644 spec/tags/19/ruby/core/kernel/system_tags.txt delete mode 100644 spec/tags/20/ruby/core/kernel/system_tags.txt diff --git a/kernel/common/process.rb b/kernel/common/process.rb index 1f31cca902..016881806f 100644 --- a/kernel/common/process.rb +++ b/kernel/common/process.rb @@ -710,29 +710,6 @@ def fork(&block) end module_function :fork - def system(prog, *args) - pid = Process.fork - if pid - Process.waitpid(pid) - $?.exitstatus == 0 - else - begin - Kernel.exec(prog, *args) - rescue Exception => e - if $DEBUG - e.render("Unable to execute subprogram", STDERR) - end - exit! 1 - end - - if $DEBUG - STDERR.puts "Unable to execute subprogram - exec silently returned" - end - exit! 1 - end - end - module_function :system - def `(str) #` str = StringValue(str) unless str.kind_of?(String) pid, output = Process.replace(str) diff --git a/kernel/common/process18.rb b/kernel/common/process18.rb index 69363742fc..f3033424da 100644 --- a/kernel/common/process18.rb +++ b/kernel/common/process18.rb @@ -26,3 +26,28 @@ def self.exec(cmd, *args) end end end + +module Kernel + def system(prog, *args) + pid = Process.fork + if pid + Process.waitpid(pid) + $?.exitstatus == 0 + else + begin + Kernel.exec(prog, *args) + rescue Exception => e + if $DEBUG + e.render("Unable to execute subprogram", STDERR) + end + exit! 1 + end + + if $DEBUG + STDERR.puts "Unable to execute subprogram - exec silently returned" + end + exit! 1 + end + end + module_function :system +end diff --git a/kernel/common/process19.rb b/kernel/common/process19.rb index 1e32f9b479..3736b71c85 100644 --- a/kernel/common/process19.rb +++ b/kernel/common/process19.rb @@ -69,3 +69,30 @@ def self.exec(environment_or_cmd, *args) end end end + +module Kernel + def system(prog, *args) + IO.pipe do |read, write| + pid = Process.fork do + read.close + + begin + Kernel.exec(prog, *args) + ensure + write.write false + exit! 1 + end + end + + write.close + Process.waitpid(pid) + + if read.eof? + $?.exitstatus == 0 + else + nil + end + end + end + module_function :system +end diff --git a/spec/tags/19/ruby/core/kernel/system_tags.txt b/spec/tags/19/ruby/core/kernel/system_tags.txt deleted file mode 100644 index 040e28518f..0000000000 --- a/spec/tags/19/ruby/core/kernel/system_tags.txt +++ /dev/null @@ -1 +0,0 @@ -fails:Kernel#system returns nil when command execution fails diff --git a/spec/tags/20/ruby/core/kernel/system_tags.txt b/spec/tags/20/ruby/core/kernel/system_tags.txt deleted file mode 100644 index 6317730b61..0000000000 --- a/spec/tags/20/ruby/core/kernel/system_tags.txt +++ /dev/null @@ -1,2 +0,0 @@ -fails:Kernel#system returns nil when command execution fails -fails:Kernel#system is a private method