Skip to content

Commit 7c61cbd

Browse files
nobutmm1
authored andcommitted
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 ruby#9354] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44517 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog
1 parent 03fc81c commit 7c61cbd

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/timeout.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def timeout(sec, klass = nil) #:yield: +sec+
6767
return yield(sec) if sec == nil or sec.zero?
6868
message = "execution expired"
6969
e = Error
70-
bt = catch((klass||ExitException).new) do |exception|
70+
bl = proc do |exception|
7171
begin
7272
x = Thread.current
7373
y = Thread.start {
@@ -80,7 +80,7 @@ def timeout(sec, klass = nil) #:yield: +sec+
8080
end
8181
}
8282
return yield(sec)
83-
rescue (klass||ExitException) => e
83+
rescue klass => e
8484
e.backtrace
8585
ensure
8686
if y
@@ -89,6 +89,7 @@ def timeout(sec, klass = nil) #:yield: +sec+
8989
end
9090
end
9191
end
92+
bt = klass ? bl.call(klass) : catch((klass = ExitException).new, &bl)
9293
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
9394
bt.reject! {|m| rej =~ m}
9495
level = -caller(CALLER_OFFSET).size

test/test_timeout.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,14 @@ def test_rescue_exit
5757
end
5858
assert_raise_with_message(exc, /execution expired/) {raise e if e}
5959
end
60+
61+
def test_custom_exception
62+
bug9354 = '[ruby-core:59511] [Bug #9354]'
63+
err = Class.new(StandardError) do
64+
def initialize(msg) super end
65+
end
66+
assert_nothing_raised(ArgumentError, bug9354) do
67+
assert_equal(:ok, timeout(100, err) {:ok})
68+
end
69+
end
6070
end

0 commit comments

Comments
 (0)