summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--eval.c16
-rw-r--r--test/ruby/test_exception.rb54
-rw-r--r--version.h6
4 files changed, 72 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index 39d4f2d22e..39336b7ef5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Apr 17 04:20:40 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * eval.c (setup_exception): set the cause only if it is explicitly
+ given or not set yet. [Bug #12068]
+
Sat Apr 16 00:56:45 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* thread.c (rb_thread_setname): defer setting native thread name
diff --git a/eval.c b/eval.c
index f87be390f6..1002975633 100644
--- a/eval.c
+++ b/eval.c
@@ -25,6 +25,7 @@ VALUE rb_eLocalJumpError;
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_thread_t *th);
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, volatile VALUE mesg, VALUE cause)
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)) {
@@ -1652,4 +1655,5 @@ Init_eval(void)
id_signo = rb_intern_const("signo");
id_status = rb_intern_const("status");
+ id_cause = rb_intern_const("cause");
}
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index 91732dd062..b7b6ba4b1e 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -625,6 +625,41 @@ end.join
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_raise_with_cause
msg = "[Feature #8257]"
cause = ArgumentError.new("foobar")
@@ -639,6 +674,25 @@ end.join
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'
diff --git a/version.h b/version.h
index edbdb74cad..6a485c9ca7 100644
--- a/version.h
+++ b/version.h
@@ -1,10 +1,10 @@
#define RUBY_VERSION "2.3.0"
-#define RUBY_RELEASE_DATE "2016-04-16"
-#define RUBY_PATCHLEVEL 83
+#define RUBY_RELEASE_DATE "2016-04-17"
+#define RUBY_PATCHLEVEL 84
#define RUBY_RELEASE_YEAR 2016
#define RUBY_RELEASE_MONTH 4
-#define RUBY_RELEASE_DAY 16
+#define RUBY_RELEASE_DAY 17
#include "ruby/version.h"