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

ruby-changes:32439

From: nobu <ko1@a...>
Date: Tue, 7 Jan 2014 12:43:14 +0900 (JST)
Subject: [ruby-changes:32439] nobu:r44518 (trunk): timeout.rb: fix for ExitException

nobu	2014-01-07 12:43:08 +0900 (Tue, 07 Jan 2014)

  New Revision: 44518

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44518

  Log:
    timeout.rb: fix for ExitException
    
    * lib/timeout.rb (Timeout#timeout): should not rescue ordinarily
      raised ExitException, which should not be thrown.
    * lib/timeout.rb (Timeout::ExitException.catch): set @thread only if
      it ought to be caught.

  Modified files:
    trunk/ChangeLog
    trunk/lib/timeout.rb
    trunk/test/test_timeout.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44517)
+++ ChangeLog	(revision 44518)
@@ -1,4 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Tue Jan  7 12:42:35 2014  Nobuyoshi Nakada  <nobu@r...>
+Tue Jan  7 12:43:06 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout#timeout): should not rescue ordinarily
+	  raised ExitException, which should not be thrown.
+
+	* lib/timeout.rb (Timeout::ExitException.catch): set @thread only if
+	  it ought to be caught.
 
 	* lib/timeout.rb (Timeout#timeout): when a custom exception is given,
 	  no instance is needed to be caught, so defer creating new instance
Index: lib/timeout.rb
===================================================================
--- lib/timeout.rb	(revision 44517)
+++ lib/timeout.rb	(revision 44518)
@@ -28,10 +28,11 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L28
   class ExitException < ::Exception # :nodoc:
     attr_reader :klass, :thread
 
-    def initialize(*)
-      super
-      @thread = Thread.current
-      freeze
+    def self.catch
+      exc = new
+      exc.instance_variable_set(:@thread, Thread.current)
+      exc.freeze
+      ::Kernel.catch(exc) {yield exc}
     end
 
     def exception(*)
@@ -80,8 +81,6 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L81
           end
         }
         return yield(sec)
-      rescue klass => e
-        e.backtrace
       ensure
         if y
           y.kill
@@ -89,7 +88,15 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L88
         end
       end
     end
-    bt = klass ? bl.call(klass) : catch((klass = ExitException).new, &bl)
+    if klass
+      begin
+        bl.call(klass)
+      rescue klass => e
+        bt = e.backtrace
+      end
+    else
+      bt = ExitException.catch(&bl)
+    end
     rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
     bt.reject! {|m| rej =~ m}
     level = -caller(CALLER_OFFSET).size
Index: test/test_timeout.rb
===================================================================
--- test/test_timeout.rb	(revision 44517)
+++ test/test_timeout.rb	(revision 44518)
@@ -67,4 +67,12 @@ class TestTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_timeout.rb#L67
       assert_equal(:ok, timeout(100, err) {:ok})
     end
   end
+
+  def test_exit_exception
+    assert_raise_with_message(Timeout::ExitException, "boon") do
+      Timeout.timeout(10, Timeout::ExitException) do
+        raise Timeout::ExitException, "boon"
+      end
+    end
+  end
 end

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

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