Skip to content
Browse files

Address 1.9 compatibility issues identified by John Pagonis and Vassi…

…lis Vatikiotis

- No more Thread.esclusive, which doesn't exist in 1.9
- Prevent hammertime_raise from calling itself.
  • Loading branch information...
1 parent 167a664 commit 0d479f2550effb72e98ef5c47e836b481da2e7a3 @avdi committed Jan 30, 2012
Showing with 39 additions and 5 deletions.
  1. +11 −0 examples/loaderror.rb
  2. +28 −5 lib/hammertime.rb
View
11 examples/loaderror.rb
@@ -0,0 +1,11 @@
+# IN A WORLD... where ruby-debug and IRB is not in the load
+# path... one man will raise an exception.
+require 'rubygems'
+require '../lib/hammertime'
+
+# Clear out load path. Now LoadError will be raised when
+# Hammertime tries to load IRB or the debugger.
+$:.clear
+
+include Hammertime
+raise "Holy smokes!"
View
33 lib/hammertime.rb
@@ -1,3 +1,4 @@
+require 'thread'
require 'highline'
module Hammertime
@@ -28,12 +29,15 @@ def self.debug_supported?
def hammertime_raise(*args)
backtrace = caller(2)
- Thread.exclusive do
- error, backtrace =
+ fallback = lambda do
+ hammertime_original_raise(*args)
+ end
+ exclusive_and_non_reentrant(fallback) do
+ error, backtrace =
case args.size
when 0 then [($!.nil? ? RuntimeError.new : $!), backtrace]
- when 1 then
- if args[0].is_a?(String)
+ when 1 then
+ if args[0].is_a?(String)
[RuntimeError.exception(args[0]), backtrace]
else
[args[0].exception, backtrace]
@@ -52,7 +56,7 @@ def hammertime_raise(*args)
else
::Hammertime.stopped = true
end
-
+
c = ::Hammertime.hammertime_console
c.say "\n"
c.say "=== Stop! Hammertime. ==="
@@ -118,6 +122,14 @@ def raise(*args)
private
+ # No lazy initialization where threads are concerned. We still use
+ # ||= on the off chance that this file gets loaded twice in 1.8.
+ @mutex ||= Mutex.new
+
+ def self.mutex
+ @mutex
+ end
+
def self.hammertime_console
@console ||= HighLine.new($stdin, $stderr)
end
@@ -128,6 +140,17 @@ def hammertime_ignore_error?(error, backtrace)
return true if ::Hammertime.ignored_lines.include?(backtrace.first)
return false
end
+
+ def exclusive_and_non_reentrant(fallback, &block)
+ lock_acquired = ::Hammertime.mutex.try_lock
+ if lock_acquired
+ yield
+ ::Hammertime.mutex.unlock
+ else
+ fallback.call
+ end
+ end
+
end
unless ::Object < Hammertime

0 comments on commit 0d479f2

Please sign in to comment.
Something went wrong with that request. Please try again.