From 9f5537c5b39b230360af793977d9ddb5b539f4de Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 7 Jan 2014 03:42:37 +0000 Subject: 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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44517 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ lib/timeout.rb | 5 +++-- test/test_timeout.rb | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc1ab7f3a2..4be11d9831 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Jan 7 12:42:35 2014 Nobuyoshi Nakada + + * 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 * lib/rubygems: Update to RubyGems master 21e409d / RubyGems 2.2.1. diff --git a/lib/timeout.rb b/lib/timeout.rb index ad951d2ffa..f2ed725229 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -67,7 +67,7 @@ module Timeout 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 end } return yield(sec) - rescue (klass||ExitException) => e + rescue klass => e e.backtrace ensure if y @@ -89,6 +89,7 @@ module Timeout 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 diff --git a/test/test_timeout.rb b/test/test_timeout.rb index 71edc65c51..08885cd54f 100644 --- a/test/test_timeout.rb +++ b/test/test_timeout.rb @@ -57,4 +57,14 @@ class TestTimeout < Test::Unit::TestCase 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 -- cgit v1.2.3