[前][次][番号順一覧][スレッド一覧]

ruby-changes:41746

From: nobu <ko1@a...>
Date: Sun, 14 Feb 2016 16:19:05 +0900 (JST)
Subject: [ruby-changes:41746] nobu:r53819 (trunk): not overwrite cause

nobu	2016-02-14 16:19:23 +0900 (Sun, 14 Feb 2016)

  New Revision: 53819

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53819

  Log:
    not overwrite cause
    
    * eval.c (setup_exception): set the cause only if it is explicitly
      given or not set yet.  [Bug #12068]

  Modified files:
    trunk/ChangeLog
    trunk/eval.c
    trunk/test/ruby/test_exception.rb
Index: test/ruby/test_exception.rb
===================================================================
--- test/ruby/test_exception.rb	(revision 53818)
+++ test/ruby/test_exception.rb	(revision 53819)
@@ -625,6 +625,44 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L625
     assert_not_same(e, e.cause, "#{msg}: should not be recursive")
   end
 
+  def test_cause_raised_in_rescue
+    e = assert_raise_with_message(RuntimeError, 'b') {
+      begin
+        raise 'a'
+      rescue => a
+        begin
+          raise 'b'
+        rescue => b
+          begin
+            raise 'c'
+          rescue
+            raise b
+          end
+        end
+      end
+    }
+    assert_equal('a', e.cause.message, 'cause should not be overwritten by reraise')
+  end
+
+  def test_cause_at_raised
+    e = assert_raise_with_message(RuntimeError, 'b') {
+      begin
+        raise 'a'
+      rescue => a
+        b = RuntimeError.new('b')
+        begin
+          raise 'c'
+        rescue
+          raise b
+        end
+      end
+    }
+    assert_equal('c', e.cause.message, 'cause should be the exception at raised')
+  end
+
+  def test_cause_at_raised
+  end
+
   def test_raise_with_cause
     msg = "[Feature #8257]"
     cause = ArgumentError.new("foobar")
@@ -639,6 +677,25 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L677
     end
   end
 
+  def test_raise_with_cause_in_rescue
+    e = assert_raise_with_message(RuntimeError, 'b') {
+      begin
+        raise 'a'
+      rescue => a
+        begin
+          raise 'b'
+        rescue => b
+          begin
+            raise 'c'
+          rescue
+            raise b, cause: ArgumentError.new('d')
+          end
+        end
+      end
+    }
+    assert_equal('d', e.cause.message, 'cause option should be honored always')
+  end
+
   def test_unknown_option
     bug = '[ruby-core:63203] [Feature #8257] should pass unknown options'
 
Index: eval.c
===================================================================
--- eval.c	(revision 53818)
+++ eval.c	(revision 53819)
@@ -25,6 +25,7 @@ VALUE rb_eLocalJumpError; https://github.com/ruby/ruby/blob/trunk/eval.c#L25
 VALUE rb_eSysStackError;
 
 ID ruby_static_id_signo, ruby_static_id_status;
+static ID id_cause;
 #define id_signo ruby_static_id_signo
 #define id_status ruby_static_id_status
 
@@ -442,9 +443,6 @@ static VALUE get_thread_errinfo(rb_threa https://github.com/ruby/ruby/blob/trunk/eval.c#L443
 static VALUE
 exc_setup_cause(VALUE exc, VALUE cause)
 {
-    ID id_cause;
-    CONST_ID(id_cause, "cause");
-
 #if SUPPORT_JOKE
     if (NIL_P(cause)) {
 	ID id_true_cause;
@@ -488,10 +486,15 @@ setup_exception(rb_thread_t *th, int tag https://github.com/ruby/ruby/blob/trunk/eval.c#L486
 	mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
 	nocause = 0;
     }
-    if (cause == Qundef) {
-	cause = nocause ? Qnil : get_thread_errinfo(th);
+    if (cause != Qundef) {
+	exc_setup_cause(mesg, cause);
+    }
+    else if (nocause) {
+	exc_setup_cause(mesg, Qnil);
+    }
+    else if (!rb_ivar_defined(mesg, id_cause)) {
+	exc_setup_cause(mesg, get_thread_errinfo(th));
     }
-    exc_setup_cause(mesg, cause);
 
     file = rb_source_loc(&line);
     if (file && !NIL_P(mesg)) {
@@ -1668,4 +1671,5 @@ Init_eval(void) https://github.com/ruby/ruby/blob/trunk/eval.c#L1671
 
     id_signo = rb_intern_const("signo");
     id_status = rb_intern_const("status");
+    id_cause = rb_intern_const("cause");
 }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53818)
+++ ChangeLog	(revision 53819)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Feb 14 16:18:57 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* eval.c (setup_exception): set the cause only if it is explicitly
+	  given or not set yet.  [Bug #12068]
+
 Sat Feb 13 21:44:58 2016  Tanaka Akira  <akr@f...>
 
 	* hash.c (rb_hash_invert): [DOC] more examples.

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]