Permalink
Browse files

Merge remote-tracking branch 'ConradIrwin/bug/at_exit_exceptions'

  • Loading branch information...
2 parents 1eebc60 + 5160fc5 commit 9d8df0992e5eb934966674e77f8e1cd9ef87811c @brixen brixen committed Oct 3, 2012
Showing with 52 additions and 52 deletions.
  1. +46 −52 kernel/loader.rb
  2. +6 −0 spec/ruby/core/kernel/at_exit_spec.rb
View
@@ -799,62 +799,56 @@ def write_last_error(e)
# Orchestrate everything.
def main
- begin
- begin
- preamble
- system_load_path
- signals
- load_compiler
- preload
- detect_alias
- options
- load_paths
- debugger
- rubygems
- gemfile
- requires
- evals
- script
- irb
-
- rescue SystemExit => e
- # Let the outer rescue grab it
- raise e
-
- rescue SyntaxError => e
- @exit_code = 1
-
- show_syntax_error(e)
+ preamble
+ system_load_path
+ signals
+ load_compiler
+ preload
+ detect_alias
+ options
+ load_paths
+ debugger
+ rubygems
+ gemfile
+ requires
+ evals
+ script
+ irb
+
+ rescue SystemExit => e
+ @exit_code = e.status
+
+ epilogue
+ rescue SyntaxError => e
+ @exit_code = 1
- STDERR.puts "\nBacktrace:"
- STDERR.puts e.awesome_backtrace.show
- rescue Interrupt => e
- @exit_code = 1
+ show_syntax_error(e)
- write_last_error(e)
- e.render "An exception occurred #{@stage}"
- rescue SignalException => e
- Signal.trap(e.signo, "SIG_DFL")
- Process.kill e.signo, Process.pid
- rescue Object => e
- @exit_code = 1
+ STDERR.puts "\nBacktrace:"
+ STDERR.puts e.awesome_backtrace.show
+ epilogue
+ rescue Interrupt => e
+ @exit_code = 1
- write_last_error(e)
- e.render "An exception occurred #{@stage}"
- end
+ write_last_error(e)
+ e.render "An exception occurred #{@stage}"
+ epilogue
+ rescue SignalException => e
+ Signal.trap(e.signo, "SIG_DFL")
+ Process.kill e.signo, Process.pid
+ epilogue
+ rescue Object => e
+ @exit_code = 1
- # We do this, run epilogue both on catching SystemExit and
- # if there was no exception so that the at_exit handlers
- # can see $! as the SystemExit object the system is going to
- # exit with.
- rescue SystemExit => e
- @exit_code = e.status
- epilogue
- else
- epilogue
- ensure
- done
- end
+ write_last_error(e)
+ e.render "An exception occurred #{@stage}"
+ epilogue
+ else
+ # We do this, run epilogue both in the rescue blocks and also here,
+ # so that at_exit{} hooks can read $!.
+ epilogue
+ ensure
+ done
end
# Creates an instance of the Loader and runs it. We catch any uncaught
@@ -20,6 +20,12 @@
ruby_exe(code).should == "643"
end
+ it "gives access to the last raised exception" do
+ code = 'at_exit{ puts $! == $exception }; begin; raise "foo"; rescue => e; $exception = e; raise; end'
+ # The true is embedded in the stack trace of the uncaught exception
+ ruby_exe("STDERR=STDOUT; #{code}", :escape => true).should =~ /true/
+ end
+
end
describe "Kernel#at_exit" do

0 comments on commit 9d8df09

Please sign in to comment.