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

ruby-changes:30631

From: nobu <ko1@a...>
Date: Tue, 27 Aug 2013 17:19:01 +0900 (JST)
Subject: [ruby-changes:30631] nobu:r42710 (trunk): timeout.rb: raise given exception

nobu	2013-08-27 17:18:50 +0900 (Tue, 27 Aug 2013)

  New Revision: 42710

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

  Log:
    timeout.rb: raise given exception
    
    * lib/timeout.rb (Timeout#timeout): skip rescue clause only when no
      exception class is given.

  Modified files:
    trunk/ChangeLog
    trunk/lib/timeout.rb
    trunk/test/test_timeout.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42709)
+++ ChangeLog	(revision 42710)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Aug 27 17:18:40 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout#timeout): skip rescue clause only when no
+	  exception class is given.
+
 Tue Aug 27 17:02:58 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* io.c (copy_stream_body): should write in binary mode.  based on a
Index: lib/timeout.rb
===================================================================
--- lib/timeout.rb	(revision 42709)
+++ lib/timeout.rb	(revision 42710)
@@ -62,7 +62,9 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L62
   # a module method, so you can call it directly as Timeout.timeout().
   def timeout(sec, klass = nil)   #:yield: +sec+
     return yield(sec) if sec == nil or sec.zero?
-    bt = catch(ExitException.new) do |exception|
+    message = "execution expired"
+    e = Error
+    bt = catch((klass||ExitException).new) do |exception|
       begin
         x = Thread.current
         y = Thread.start {
@@ -71,11 +73,12 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L73
           rescue => e
             x.raise e
           else
-            # no message, not to make new instance.
-            x.raise exception
+            x.raise exception, message
           end
         }
         return yield(sec)
+      rescue (klass||ExitException) => e
+        e.backtrace
       ensure
         if y
           y.kill
@@ -89,7 +92,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L92
     while THIS_FILE =~ bt[level]
       bt.delete_at(level)
     end
-    raise((klass||Error), "execution expired", bt)
+    raise(e, message, bt)
   end
 
   module_function :timeout
Index: test/test_timeout.rb
===================================================================
--- test/test_timeout.rb	(revision 42709)
+++ test/test_timeout.rb	(revision 42710)
@@ -33,7 +33,7 @@ class TestTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_timeout.rb#L33
   def test_skip_rescue
     bug8730 = '[Bug #8730]'
     e = nil
-    assert_raise(Timeout::Error, bug8730) do
+    assert_raise_with_message(Timeout::Error, /execution expired/, bug8730) do
       timeout 0.1 do
         begin
           sleep 3
@@ -43,4 +43,18 @@ class TestTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_timeout.rb#L43
     end
     assert_nil(e, bug8730)
   end
+
+  def test_rescue_exit
+    exc = Class.new(RuntimeError)
+    e = nil
+    assert_nothing_raised(exc) do
+      timeout 0.1, exc do
+        begin
+          sleep 3
+        rescue exc => e
+        end
+      end
+    end
+    assert_raise_with_message(exc, /execution expired/) {raise e if e}
+  end
 end

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

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