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

ruby-changes:3974

From: ko1@a...
Date: Thu, 14 Feb 2008 01:43:43 +0900 (JST)
Subject: [ruby-changes:3974] nobu - Ruby:r15464 (ruby_1_8, trunk): * lib/timeout.rb (Timeout::timeout): made sensitive to location on the

nobu	2008-02-14 01:43:18 +0900 (Thu, 14 Feb 2008)

  New Revision: 15464

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/lib/timeout.rb
    trunk/ChangeLog
    trunk/lib/timeout.rb

  Log:
    * lib/timeout.rb (Timeout::timeout): made sensitive to location on the
      stack.  [ruby-core:15458]


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=15464&r2=15463&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15464&r2=15463&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/timeout.rb?r1=15464&r2=15463&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/lib/timeout.rb?r1=15464&r2=15463&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15463)
+++ ChangeLog	(revision 15464)
@@ -1,3 +1,8 @@
+Thu Feb 14 01:43:16 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout::timeout): made sensitive to location on the
+	  stack.  [ruby-core:15458]
+
 Thu Feb 14 00:49:53 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* common.mk (INSTRUBY_ARGS): pass mode to install.  [ruby-dev:33766]
Index: lib/timeout.rb
===================================================================
--- lib/timeout.rb	(revision 15463)
+++ lib/timeout.rb	(revision 15464)
@@ -24,9 +24,14 @@
 
 module Timeout
   # Raised by Timeout#timeout when the block times out.
-  class Error < Interrupt
+  class Error < RuntimeError
   end
+  class ExitException < ::Exception # :nodoc:
+  end
 
+  THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
+  CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
+
   # Executes the method's block.  If the block execution terminates before
   # +sec+ seconds has passed, it returns the result value of the block.
   # If not, it terminates the execution and raises +exception+ (which defaults
@@ -35,8 +40,9 @@
   # Note that this is both a method of module Timeout, so you can 'include Timeout'
   # into your classes so they have a #timeout method, as well as a module method,
   # so you can call it directly as Timeout.timeout().
-  def timeout(sec, exception = Error)   #:yield: +sec+
+  def timeout(sec, klass = nil)   #:yield: +sec+
     return yield(sec) if sec == nil or sec.zero?
+    exception = klass || Class.new(ExitException)
     begin
       x = Thread.current
       y = Thread.start {
@@ -44,6 +50,17 @@
         x.raise exception, "execution expired" if x.alive?
       }
       return yield(sec)
+    rescue exception => e
+      rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
+      (bt = e.backtrace).reject! {|m| rej =~ m}
+      level = -caller(CALLER_OFFSET).size
+      while THIS_FILE =~ bt[level]
+        bt.delete_at(level)
+        level += 1
+      end
+      raise if klass            # if exception class is specified, it
+                                # would be expected outside.
+      raise Error, e.message, e.backtrace
     ensure
       if y and y.alive?
         y.kill 
@@ -61,7 +78,7 @@
 #
 # Defined for backwards compatibility with earlier versions of timeout.rb, see
 # Timeout#timeout.
-def timeout(n, e = Timeout::Error, &block)
+def timeout(n, e = nil, &block)
   Timeout::timeout(n, e, &block)
 end
 
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 15463)
+++ ruby_1_8/ChangeLog	(revision 15464)
@@ -1,3 +1,8 @@
+Thu Feb 14 01:43:16 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout::timeout): made sensitive to location on the
+	  stack.  [ruby-core:15458]
+
 Thu Feb 14 00:49:53 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* common.mk (INSTRUBY_ARGS): pass mode to install.  [ruby-dev:33766]
Index: ruby_1_8/lib/timeout.rb
===================================================================
--- ruby_1_8/lib/timeout.rb	(revision 15463)
+++ ruby_1_8/lib/timeout.rb	(revision 15464)
@@ -32,9 +32,14 @@
   ##
   # Raised by Timeout#timeout when the block times out.
 
-  class Error<Interrupt
+  class Error < Interrupt
   end
+  class ExitException < ::Exception # :nodoc:
+  end
 
+  THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
+  CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
+
   ##
   # Executes the method's block. If the block execution terminates before +sec+
   # seconds has passed, it returns true. If not, it terminates the execution
@@ -44,9 +49,10 @@
   # Timeout' into your classes so they have a #timeout method, as well as a
   # module method, so you can call it directly as Timeout.timeout().
 
-  def timeout(sec, exception=Error)
+  def timeout(sec, klass = nil)
     return yield if sec == nil or sec.zero?
     raise ThreadError, "timeout within critical session" if Thread.critical
+    exception = klass || Class.new(ExitException)
     begin
       x = Thread.current
       y = Thread.start {
@@ -55,6 +61,17 @@
       }
       yield sec
       #    return true
+    rescue exception => e
+      rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
+      (bt = e.backtrace).reject! {|m| rej =~ m}
+      level = -caller(CALLER_OFFSET).size
+      while THIS_FILE =~ bt[level]
+        bt.delete_at(level)
+        level += 1
+      end
+      raise if klass            # if exception class is specified, it
+                                # would be expected outside.
+      raise Error, e.message, e.backtrace
     ensure
       y.kill if y and y.alive?
     end
@@ -72,7 +89,7 @@
 # Defined for backwards compatibility with earlier versions of timeout.rb, see
 # Timeout#timeout.
 
-def timeout(n, e=Timeout::Error, &block) # :nodoc:
+def timeout(n, e = nil, &block) # :nodoc:
   Timeout::timeout(n, e, &block)
 end
 

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

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