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

ruby-changes:32444

From: nobu <ko1@a...>
Date: Wed, 8 Jan 2014 13:12:47 +0900 (JST)
Subject: [ruby-changes:32444] nobu:r44523 (trunk): lib/timeout.rb: fallback to Timeout::Error

nobu	2014-01-08 13:12:39 +0900 (Wed, 08 Jan 2014)

  New Revision: 44523

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

  Log:
    lib/timeout.rb: fallback to Timeout::Error
    
    * lib/timeout.rb (Timeout::ExitException.catch): pass arguments
      for new instance.
    * lib/timeout.rb (Timeout::ExitException#exception): fallback to
      Timeout::Error if couldn't throw.  [ruby-dev:47872] [Bug #9380]
    * lib/timeout.rb (Timeout#timeout): initialize ExitException with
      message for the fallback case.

  Modified files:
    trunk/ChangeLog
    trunk/lib/timeout.rb
    trunk/test/test_timeout.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44522)
+++ ChangeLog	(revision 44523)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jan  8 13:12:41 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout::ExitException.catch): pass arguments
+	  for new instance.
+
+	* lib/timeout.rb (Timeout::ExitException#exception): fallback to
+	  Timeout::Error if couldn't throw.  [ruby-dev:47872] [Bug #9380]
+
+	* lib/timeout.rb (Timeout#timeout): initialize ExitException with
+	  message for the fallback case.
+
 Tue Jan  7 12:43:06 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/timeout.rb (Timeout#timeout): should not rescue ordinarily
Index: lib/timeout.rb
===================================================================
--- lib/timeout.rb	(revision 44522)
+++ lib/timeout.rb	(revision 44523)
@@ -28,15 +28,23 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L28
   class ExitException < ::Exception # :nodoc:
     attr_reader :thread
 
-    def self.catch
-      exc = new
+    def self.catch(*args)
+      exc = new(*args)
       exc.instance_variable_set(:@thread, Thread.current)
       exc.freeze
       ::Kernel.catch(exc) {yield exc}
     end
 
     def exception(*)
-      throw(self, caller) if self.thread == Thread.current
+      if self.thread == Thread.current
+        bt = caller
+        begin
+          throw(self, bt)
+        rescue ArgumentError => e
+          raise unless e.message.start_with?("uncaught throw")
+          raise Error, message, backtrace
+        end
+      end
       self
     end
   end
@@ -95,7 +103,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L103
         bt = e.backtrace
       end
     else
-      bt = ExitException.catch(&bl)
+      bt = ExitException.catch(message, &bl)
     end
     rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
     bt.reject! {|m| rej =~ m}
Index: test/test_timeout.rb
===================================================================
--- test/test_timeout.rb	(revision 44522)
+++ test/test_timeout.rb	(revision 44523)
@@ -75,4 +75,15 @@ class TestTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_timeout.rb#L75
       end
     end
   end
+
+  def test_enumerator_next
+    bug9380 = '[ruby-dev:47872] [Bug #9380]: timeout in Enumerator#next'
+    e = (o=Object.new).to_enum
+    def o.each
+      sleep
+    end
+    assert_raise_with_message(Timeout::Error, 'execution expired', bug9380) do
+      Timeout.timeout(0.01) {e.next}
+    end
+  end
 end

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

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