ruby-changes:32679
From: nobu <ko1@a...>
Date: Thu, 30 Jan 2014 15:16:19 +0900 (JST)
Subject: [ruby-changes:32679] nobu:r44758 (trunk): object.c: error message encoding
nobu 2014-01-30 15:16:15 +0900 (Thu, 30 Jan 2014) New Revision: 44758 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44758 Log: object.c: error message encoding * object.c (convert_type, rb_convert_type, rb_check_convert_type), (rb_to_integer): preserve class name encoding error messages. Modified files: trunk/object.c trunk/test/ruby/test_exception.rb Index: object.c =================================================================== --- object.c (revision 44757) +++ object.c (revision 44758) @@ -2583,13 +2583,16 @@ convert_type(VALUE val, const char *tnam https://github.com/ruby/ruby/blob/trunk/object.c#L2583 r = rb_check_funcall(val, m, 0, 0); if (r == Qundef) { if (raise) { - rb_raise(rb_eTypeError, i < IMPLICIT_CONVERSIONS - ? "no implicit conversion of %s into %s" - : "can't convert %s into %s", - NIL_P(val) ? "nil" : - val == Qtrue ? "true" : - val == Qfalse ? "false" : - rb_obj_classname(val), + const char *msg = i < IMPLICIT_CONVERSIONS ? + "no implicit conversion of" : "can't convert"; + const char *cname = NIL_P(val) ? "nil" : + val == Qtrue ? "true" : + val == Qfalse ? "false" : + NULL; + if (cname) + rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname); + rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg, + rb_obj_class(val), tname); } return Qnil; @@ -2597,6 +2600,16 @@ convert_type(VALUE val, const char *tnam https://github.com/ruby/ruby/blob/trunk/object.c#L2600 return r; } +NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE)); +static void +conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result) +{ + VALUE cname = rb_obj_class(val); + rb_raise(rb_eTypeError, + "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")", + cname, tname, cname, method, rb_obj_class(result)); +} + VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method) { @@ -2605,9 +2618,7 @@ rb_convert_type(VALUE val, int type, con https://github.com/ruby/ruby/blob/trunk/object.c#L2618 if (TYPE(val) == type) return val; v = convert_type(val, tname, method, TRUE); if (TYPE(v) != type) { - const char *cname = rb_obj_classname(val); - rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)", - cname, tname, cname, method, rb_obj_classname(v)); + conversion_mismatch(val, tname, method, v); } return v; } @@ -2622,9 +2633,7 @@ rb_check_convert_type(VALUE val, int typ https://github.com/ruby/ruby/blob/trunk/object.c#L2633 v = convert_type(val, tname, method, FALSE); if (NIL_P(v)) return Qnil; if (TYPE(v) != type) { - const char *cname = rb_obj_classname(val); - rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)", - cname, tname, cname, method, rb_obj_classname(v)); + conversion_mismatch(val, tname, method, v); } return v; } @@ -2639,9 +2648,7 @@ rb_to_integer(VALUE val, const char *met https://github.com/ruby/ruby/blob/trunk/object.c#L2648 if (RB_TYPE_P(val, T_BIGNUM)) return val; v = convert_type(val, "Integer", method, TRUE); if (!rb_obj_is_kind_of(v, rb_cInteger)) { - const char *cname = rb_obj_classname(val); - rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)", - cname, cname, method, rb_obj_classname(v)); + conversion_mismatch(val, "Integer", method, v); } return v; } Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 44757) +++ test/ruby/test_exception.rb (revision 44758) @@ -221,6 +221,22 @@ class TestException < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L221 assert_raise(ArgumentError) { raise 1, 1, 1, 1 } end + def test_type_error_message_encoding + c = eval("Module.new do break class C\u{4032}; self; end; end") + o = c.new + assert_raise_with_message(TypeError, /C\u{4032}/) do + ""[o] + end + c.class_eval {def to_int; self; end} + assert_raise_with_message(TypeError, /C\u{4032}/) do + ""[o] + end + c.class_eval {def to_a; self; end} + assert_raise_with_message(TypeError, /C\u{4032}/) do + [*o] + end + end + def test_errat assert_in_out_err([], "p $@", %w(nil), []) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/