Skip to content

Commit 952154d

Browse files
committed
Make Timeout::Error#exception with multiple arguments not ignore arguments
This makes: raise(Timeout::Error.new("hello"), "world") raise a TimeoutError instance with "world" as the message instead of "hello", for consistency with other Ruby exception classes. This required some internal changes to keep the tests passing. Fixes [Bug #17812]
1 parent 554660d commit 952154d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Diff for: lib/timeout.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ class Error < RuntimeError
3232
def self.catch(*args)
3333
exc = new(*args)
3434
exc.instance_variable_set(:@thread, Thread.current)
35-
::Kernel.catch(exc) {yield exc}
35+
catch_value = Object.new
36+
exc.instance_variable_set(:@catch_value, catch_value)
37+
::Kernel.catch(catch_value) {yield exc}
3638
end
3739

3840
def exception(*)
3941
# TODO: use Fiber.current to see if self can be thrown
4042
if self.thread == Thread.current
4143
bt = caller
4244
begin
43-
throw(self, bt)
45+
throw(@catch_value, bt)
4446
rescue UncaughtThrowError
4547
end
4648
end
47-
self
49+
super
4850
end
4951
end
5052

@@ -115,6 +117,7 @@ def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
115117
begin
116118
bl.call(klass)
117119
rescue klass => e
120+
message = e.message
118121
bt = e.backtrace
119122
end
120123
else

Diff for: test/test_timeout.rb

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ def test_exit_exception
8080
end
8181
end
8282

83+
def test_raise_with_message
84+
bug17812 = '[ruby-core:103502] [Bug #17812]: Timeout::Error doesn\'t let two-argument raise() set a new message'
85+
exc = Timeout::Error.new('foo')
86+
assert_raise_with_message(Timeout::Error, 'bar', bug17812) do
87+
raise exc, 'bar'
88+
end
89+
end
90+
8391
def test_enumerator_next
8492
bug9380 = '[ruby-dev:47872] [Bug #9380]: timeout in Enumerator#next'
8593
e = (o=Object.new).to_enum

0 commit comments

Comments
 (0)