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

ruby-changes:32438

From: nobu <ko1@a...>
Date: Tue, 7 Jan 2014 12:42:42 +0900 (JST)
Subject: [ruby-changes:32438] nobu:r44517 (trunk): timeout.rb: defer creating custom exception

nobu	2014-01-07 12:42:37 +0900 (Tue, 07 Jan 2014)

  New Revision: 44517

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

  Log:
    timeout.rb: defer creating custom exception
    
    * lib/timeout.rb (Timeout#timeout): when a custom exception is given,
      no instance is needed to be caught, so defer creating new instance
      until it is raised.  [ruby-core:59511] [Bug #9354]

  Modified files:
    trunk/ChangeLog
    trunk/lib/timeout.rb
    trunk/test/test_timeout.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44516)
+++ ChangeLog	(revision 44517)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jan  7 12:42:35 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout#timeout): when a custom exception is given,
+	  no instance is needed to be caught, so defer creating new instance
+	  until it is raised.  [ruby-core:59511] [Bug #9354]
+
 Tue Jan  7 10:16:02 2014  Eric Hodel  <drbrain@s...>
 
 	* lib/rubygems:  Update to RubyGems master 21e409d / RubyGems 2.2.1.
Index: lib/timeout.rb
===================================================================
--- lib/timeout.rb	(revision 44516)
+++ lib/timeout.rb	(revision 44517)
@@ -67,7 +67,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L67
     return yield(sec) if sec == nil or sec.zero?
     message = "execution expired"
     e = Error
-    bt = catch((klass||ExitException).new) do |exception|
+    bl = proc do |exception|
       begin
         x = Thread.current
         y = Thread.start {
@@ -80,7 +80,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L80
           end
         }
         return yield(sec)
-      rescue (klass||ExitException) => e
+      rescue klass => e
         e.backtrace
       ensure
         if y
@@ -89,6 +89,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L89
         end
       end
     end
+    bt = klass ? bl.call(klass) : catch((klass = ExitException).new, &bl)
     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 44516)
+++ test/test_timeout.rb	(revision 44517)
@@ -57,4 +57,14 @@ class TestTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_timeout.rb#L57
     end
     assert_raise_with_message(exc, /execution expired/) {raise e if e}
   end
+
+  def test_custom_exception
+    bug9354 = '[ruby-core:59511] [Bug #9354]'
+    err = Class.new(StandardError) do
+      def initialize(msg) super end
+    end
+    assert_nothing_raised(ArgumentError, bug9354) do
+      assert_equal(:ok, timeout(100, err) {:ok})
+    end
+  end
 end

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

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