Permalink
Browse files

Merge pull request #1936 from ryoqun/raise-nil-false

Correctly raise TypeError even if nil or false is given
  • Loading branch information...
Brian Ford
Brian Ford committed Oct 3, 2012
2 parents 9f174bd + c6c6824 commit b6fe43ffbde51ffbbae6b6a44ef1d585faff556c
Showing with 35 additions and 3 deletions.
  1. +9 −2 kernel/bootstrap/thread.rb
  2. +1 −1 kernel/delta/kernel.rb
  3. +25 −0 spec/ruby/shared/kernel/raise.rb
View
@@ -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
@@ -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'
View
@@ -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'
@@ -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

0 comments on commit b6fe43f

Please sign in to comment.