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

ruby-changes:64845

From: Kenta <ko1@a...>
Date: Wed, 13 Jan 2021 01:27:24 +0900 (JST)
Subject: [ruby-changes:64845] 6670de82c2 (master): [ruby/bigdecimal] Fix exception message raised in Kernel.BigDecimal

https://git.ruby-lang.org/ruby.git/commit/?id=6670de82c2

From 6670de82c2d7a1449412bf6629aa01cc8ecb35e2 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Tue, 12 Jan 2021 22:56:54 +0900
Subject: [ruby/bigdecimal] Fix exception message raised in Kernel.BigDecimal

https://github.com/ruby/bigdecimal/commit/d163f170a4
https://github.com/ruby/bigdecimal/commit/ff8eeeb064

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 4e124bd..8414769 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -201,7 +201,7 @@ cannot_be_coerced_into_BigDecimal(VALUE exc_class, VALUE v) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L201
 static inline VALUE BigDecimal_div2(VALUE, VALUE, VALUE);
 static VALUE rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
 static VALUE rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
-static VALUE rb_cstr_convert_to_BigDecimal(const char *cstr, size_t digs, int raise_exception);
+static VALUE rb_cstr_convert_to_BigDecimal(const char *c_str, size_t digs, int raise_exception);
 
 static Real*
 GetVpValueWithPrec(VALUE v, long prec, int must)
@@ -2860,10 +2860,15 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2860
       case Qnil:
       case Qtrue:
       case Qfalse:
-        if (!raise_exception)
-            return Qnil;
-        rb_raise(rb_eTypeError,
-                 "can't convert %"PRIsVALUE" into BigDecimal", val);
+        if (raise_exception) {
+            const char *cname = NIL_P(val)    ? "nil"   :
+                                val == Qtrue  ? "true"  :
+                                val == Qfalse ? "false" :
+                                NULL;
+            rb_raise(rb_eTypeError,
+                       "can't convert %s into BigDecimal", cname);
+        }
+        return Qnil;
 
       default:
         break;
@@ -2902,15 +2907,19 @@ rb_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2907
     else if (RB_TYPE_P(val, T_STRING)) {
         return rb_str_convert_to_BigDecimal(val, digs, raise_exception);
     }
+
     /* TODO: chheck to_d */
     /* TODO: chheck to_int */
-    if (!raise_exception) {
-        VALUE str = rb_check_convert_type(val, T_STRING, "String", "to_str");
-        if (NIL_P(str))
-            return Qnil;
-        val = str;
+
+    VALUE str = rb_check_convert_type(val, T_STRING, "String", "to_str");
+    if (!RB_TYPE_P(str, T_STRING)) {
+        if (raise_exception) {
+            rb_raise(rb_eTypeError,
+                     "can't convert %"PRIsVALUE" into BigDecimal", rb_obj_class(val));
+        }
+        return Qnil;
     }
-    return rb_str_convert_to_BigDecimal(val, digs, raise_exception);
+    return rb_str_convert_to_BigDecimal(str, digs, raise_exception);
 }
 
 /* call-seq:
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index fbe0d7e..b15b046 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -228,9 +228,18 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L228
     # assert_nothing_raised(RangeError) {
     #   assert_equal(nil, BigDecimal(1i, exception: false))
     # }
-    assert_raise(TypeError) {
+    assert_raise_with_message(TypeError, "can't convert nil into BigDecimal") {
       BigDecimal(nil, exception: true)
     }
+    assert_raise_with_message(TypeError, "can't convert true into BigDecimal") {
+      BigDecimal(true, exception: true)
+    }
+    assert_raise_with_message(TypeError, "can't convert false into BigDecimal") {
+      BigDecimal(false, exception: true)
+    }
+    assert_raise_with_message(TypeError, "can't convert Object into BigDecimal") {
+      BigDecimal(Object.new, exception: true)
+    }
     assert_nothing_raised(TypeError) {
       assert_equal(nil, BigDecimal(nil, exception: false))
     }
@@ -240,6 +249,9 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L249
     assert_nothing_raised(TypeError) {
       assert_equal(nil, BigDecimal(Object.new, exception: false))
     }
+    assert_nothing_raised(TypeError) {
+      assert_equal(nil, BigDecimal(Object.new, exception: false))
+    }
     # TODO: support to_d
     # assert_nothing_raised(TypeError) {
     #   o = Object.new
-- 
cgit v0.10.2


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

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