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

ruby-changes:32534

From: nobu <ko1@a...>
Date: Wed, 15 Jan 2014 17:17:05 +0900 (JST)
Subject: [ruby-changes:32534] nobu:r44610 (trunk): numeric.c: preserve encoding

nobu	2014-01-15 17:16:37 +0900 (Wed, 15 Jan 2014)

  New Revision: 44610

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44610

  Log:
    numeric.c: preserve encoding
    
    * numeric.c (coerce_failed): preserve encoding of error message.

  Modified files:
    trunk/numeric.c
    trunk/test/ruby/test_numeric.rb
Index: numeric.c
===================================================================
--- numeric.c	(revision 44609)
+++ numeric.c	(revision 44610)
@@ -232,17 +232,19 @@ coerce_body(VALUE *x) https://github.com/ruby/ruby/blob/trunk/numeric.c#L232
     return rb_funcall(x[1], id_coerce, 1, x[0]);
 }
 
+NORETURN(static void coerce_failed(VALUE x, VALUE y));
+static void
+coerce_failed(VALUE x, VALUE y)
+{
+    rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
+	     (rb_special_const_p(y)? rb_inspect(y) : rb_obj_class(y)),
+	     rb_obj_class(x));
+}
+
 static VALUE
 coerce_rescue(VALUE *x)
 {
-    volatile VALUE v = rb_inspect(x[1]);
-
-    rb_raise(rb_eTypeError, "%s can't be coerced into %s",
-	     rb_special_const_p(x[1])?
-	     RSTRING_PTR(v):
-	     rb_obj_classname(x[1]),
-	     rb_obj_classname(x[0]));
-
+    coerce_failed(x[0], x[1]);
     return Qnil;		/* dummy */
 }
 
@@ -3261,11 +3263,7 @@ bit_coerce(VALUE *x, VALUE *y, int err) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3263
 	if (!FIXNUM_P(*x) && !RB_TYPE_P(*x, T_BIGNUM)
 	    && !FIXNUM_P(*y) && !RB_TYPE_P(*y, T_BIGNUM)) {
 	    if (!err) return FALSE;
-	    rb_raise(rb_eTypeError,
-		     "%s can't be coerced into %s for bitwise arithmetic",
-		     rb_special_const_p(*y) ?
-			RSTRING_PTR(rb_inspect(*y)) : rb_obj_classname(*y),
-		     rb_obj_classname(*x));
+	    coerce_failed(*x, *y);
 	}
     }
     return TRUE;
Index: test/ruby/test_numeric.rb
===================================================================
--- test/ruby/test_numeric.rb	(revision 44609)
+++ test/ruby/test_numeric.rb	(revision 44610)
@@ -19,6 +19,11 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L19
     assert_raise_with_message(TypeError, /can't be coerced into /) {1&:foo}
     assert_raise_with_message(TypeError, /can't be coerced into /) {1|:foo}
     assert_raise_with_message(TypeError, /can't be coerced into /) {1^:foo}
+
+    assert_raise_with_message(TypeError, /:\u{3042}/) {1+:"\u{3042}"}
+    assert_raise_with_message(TypeError, /:\u{3042}/) {1&:"\u{3042}"}
+    assert_raise_with_message(TypeError, /:\u{3042}/) {1|:"\u{3042}"}
+    assert_raise_with_message(TypeError, /:\u{3042}/) {1^:"\u{3042}"}
   end
 
   def test_dummynumeric

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

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