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
This commit is contained in:
nobu 2014-01-07 03:42:37 +00:00
Родитель 765ec40120
Коммит 9f5537c5b3
3 изменённых файлов: 19 добавлений и 2 удалений

Просмотреть файл

@ -1,3 +1,9 @@
Tue Jan 7 12:42:35 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <drbrain@segment7.net> Tue Jan 7 10:16:02 2014 Eric Hodel <drbrain@segment7.net>
* lib/rubygems: Update to RubyGems master 21e409d / RubyGems 2.2.1. * lib/rubygems: Update to RubyGems master 21e409d / RubyGems 2.2.1.

Просмотреть файл

@ -67,7 +67,7 @@ module Timeout
return yield(sec) if sec == nil or sec.zero? return yield(sec) if sec == nil or sec.zero?
message = "execution expired" message = "execution expired"
e = Error e = Error
bt = catch((klass||ExitException).new) do |exception| bl = proc do |exception|
begin begin
x = Thread.current x = Thread.current
y = Thread.start { y = Thread.start {
@ -80,7 +80,7 @@ module Timeout
end end
} }
return yield(sec) return yield(sec)
rescue (klass||ExitException) => e rescue klass => e
e.backtrace e.backtrace
ensure ensure
if y if y
@ -89,6 +89,7 @@ module Timeout
end end
end end
end end
bt = klass ? bl.call(klass) : catch((klass = ExitException).new, &bl)
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
bt.reject! {|m| rej =~ m} bt.reject! {|m| rej =~ m}
level = -caller(CALLER_OFFSET).size level = -caller(CALLER_OFFSET).size

Просмотреть файл

@ -57,4 +57,14 @@ class TestTimeout < Test::Unit::TestCase
end end
assert_raise_with_message(exc, /execution expired/) {raise e if e} assert_raise_with_message(exc, /execution expired/) {raise e if e}
end 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 end