timeout.rb: raise given exception

* lib/timeout.rb (Timeout#timeout): skip rescue clause only when no
  exception class is given.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42710 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-08-27 08:18:50 +00:00
parent 8ef0192103
commit ba57274860
3 changed files with 27 additions and 5 deletions

View File

@ -1,3 +1,8 @@
Tue Aug 27 17:18:40 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/timeout.rb (Timeout#timeout): skip rescue clause only when no
exception class is given.
Tue Aug 27 17:02:58 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> Tue Aug 27 17:02:58 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (copy_stream_body): should write in binary mode. based on a * io.c (copy_stream_body): should write in binary mode. based on a

View File

@ -62,7 +62,9 @@ module Timeout
# a module method, so you can call it directly as Timeout.timeout(). # a module method, so you can call it directly as Timeout.timeout().
def timeout(sec, klass = nil) #:yield: +sec+ def timeout(sec, klass = nil) #:yield: +sec+
return yield(sec) if sec == nil or sec.zero? return yield(sec) if sec == nil or sec.zero?
bt = catch(ExitException.new) do |exception| message = "execution expired"
e = Error
bt = catch((klass||ExitException).new) do |exception|
begin begin
x = Thread.current x = Thread.current
y = Thread.start { y = Thread.start {
@ -71,11 +73,12 @@ module Timeout
rescue => e rescue => e
x.raise e x.raise e
else else
# no message, not to make new instance. x.raise exception, message
x.raise exception
end end
} }
return yield(sec) return yield(sec)
rescue (klass||ExitException) => e
e.backtrace
ensure ensure
if y if y
y.kill y.kill
@ -89,7 +92,7 @@ module Timeout
while THIS_FILE =~ bt[level] while THIS_FILE =~ bt[level]
bt.delete_at(level) bt.delete_at(level)
end end
raise((klass||Error), "execution expired", bt) raise(e, message, bt)
end end
module_function :timeout module_function :timeout

View File

@ -33,7 +33,7 @@ class TestTimeout < Test::Unit::TestCase
def test_skip_rescue def test_skip_rescue
bug8730 = '[Bug #8730]' bug8730 = '[Bug #8730]'
e = nil e = nil
assert_raise(Timeout::Error, bug8730) do assert_raise_with_message(Timeout::Error, /execution expired/, bug8730) do
timeout 0.1 do timeout 0.1 do
begin begin
sleep 3 sleep 3
@ -43,4 +43,18 @@ class TestTimeout < Test::Unit::TestCase
end end
assert_nil(e, bug8730) assert_nil(e, bug8730)
end end
def test_rescue_exit
exc = Class.new(RuntimeError)
e = nil
assert_nothing_raised(exc) do
timeout 0.1, exc do
begin
sleep 3
rescue exc => e
end
end
end
assert_raise_with_message(exc, /execution expired/) {raise e if e}
end
end end