Skip to content

Commit

Permalink
Merge pull request #1936 from ryoqun/raise-nil-false
Browse files Browse the repository at this point in the history
Correctly raise TypeError even if nil or false is given
  • Loading branch information
Brian Ford committed Oct 3, 2012
2 parents 9f174bd + c6c6824 commit b6fe43f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
11 changes: 9 additions & 2 deletions kernel/bootstrap/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def join_inner(timeout = undefined)
end
private :join_inner

def raise(exc=$!, msg=nil, trace=nil)
def raise(exc=undefined, msg=nil, trace=nil)
Rubinius.lock(self)

unless @alive
Expand All @@ -243,11 +243,18 @@ def raise(exc=$!, msg=nil, trace=nil)
end

begin
if exc.equal?(undefined)
no_argument = true
exc = $!
end

if exc.respond_to? :exception
exc = exc.exception msg
Kernel.raise TypeError, 'exception class/object expected' unless Exception === exc
exc.set_backtrace trace if trace
elsif exc.kind_of? String or !exc
elsif no_argument
exc = RuntimeError.exception nil
elsif exc.kind_of? String
exc = RuntimeError.exception exc
else
Kernel.raise TypeError, 'exception class/object expected'
Expand Down
2 changes: 1 addition & 1 deletion kernel/delta/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def raise(exc=undefined, msg=undefined, ctx=nil)
exc = exc.exception msg
end
raise ::TypeError, 'exception class/object expected' unless exc.kind_of?(::Exception)
elsif exc.kind_of? String or !exc
elsif exc.kind_of? String
exc = ::RuntimeError.exception exc
else
raise ::TypeError, 'exception class/object expected'
Expand Down
25 changes: 25 additions & 0 deletions spec/ruby/shared/kernel/raise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@
lambda { @object.raise }.should raise_error(RuntimeError)
end

it "raises a given Exception instance" do
error = RuntimeError.new
lambda { @object.raise(error) }.should raise_error(error)
end

it "raises a RuntimeError if string given" do
lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
end

it "raises a TypeError when passed a non-Exception object" do
lambda { @object.raise(Object.new) }.should raise_error(TypeError)
end

it "raises a TypeError when passed true" do
lambda { @object.raise(true) }.should raise_error(TypeError)
end

it "raises a TypeError when passed false" do
lambda { @object.raise(false) }.should raise_error(TypeError)
end

it "raises a TypeError when passed nil" do
lambda { @object.raise(nil) }.should raise_error(TypeError)
end

it "re-raises the rescued exception" do
lambda do
begin
Expand Down

0 comments on commit b6fe43f

Please sign in to comment.