From 4191a6b90d3eeb63a31609dba29a1904efee3738 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 28 Sep 2015 02:40:46 +0000 Subject: preserve encodings in error messages git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 7 +++--- bignum.c | 4 ++-- complex.c | 4 ++-- enum.c | 4 ++-- enumerator.c | 8 +++---- error.c | 39 +++++++++++++++++++++++++--------- numeric.c | 3 ++- sprintf.c | 20 +++++++++++++++++ test/-ext-/typeddata/test_typeddata.rb | 3 +++ test/ruby/test_array.rb | 2 ++ test/ruby/test_bignum.rb | 2 ++ test/ruby/test_complex.rb | 3 +++ test/ruby/test_enum.rb | 2 ++ test/ruby/test_enumerator.rb | 12 +++++++++++ test/ruby/test_exception.rb | 4 ++++ test/ruby/test_time.rb | 5 +++++ time.c | 20 +++++++++-------- 17 files changed, 109 insertions(+), 33 deletions(-) diff --git a/array.c b/array.c index 3a6e231a6e..44eaa20cfe 100644 --- a/array.c +++ b/array.c @@ -2170,10 +2170,11 @@ rb_ary_to_h(VALUE ary) long i; VALUE hash = rb_hash_new(); for (i=0; i T_MASK && xt <= 0x3f) { rb_fatal("unknown type 0x%x (0x%x given, probably comes from extension library for ruby 1.8)", t, xt); @@ -594,19 +609,23 @@ void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type) { const char *etype; - static const char mesg[] = "wrong argument type %s (expected %s)"; if (!RB_TYPE_P(obj, T_DATA)) { + wrong_type: etype = builtin_class_name(obj); - rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); + if (!etype) + rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)", + rb_obj_class(obj), data_type->wrap_struct_name); + wrong_datatype: + rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", + etype, data_type->wrap_struct_name); } if (!RTYPEDDATA_P(obj)) { - etype = rb_obj_classname(obj); - rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); + goto wrong_type; } else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name; - rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); + goto wrong_datatype; } return DATA_PTR(obj); } diff --git a/numeric.c b/numeric.c index 10ed43c870..32c9fcb8e3 100644 --- a/numeric.c +++ b/numeric.c @@ -295,7 +295,8 @@ do_coerce(VALUE *x, VALUE *y, int err) if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) { if (err) { rb_raise(rb_eTypeError, "coerce must return [x, y]"); - } else if (!NIL_P(ary)) { + } + else if (!NIL_P(ary)) { rb_warn("Bad return value for #coerce, called by numerical comparison operators."); rb_warn("#coerce must return [x, y]. The next release will raise an error for this."); } diff --git a/sprintf.c b/sprintf.c index 4549791d20..afe27c2d63 100644 --- a/sprintf.c +++ b/sprintf.c @@ -1338,6 +1338,26 @@ ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int s rb_raise(rb_eRuntimeError, "rb_vsprintf reentered"); } if (sign == '+') { + if (RB_TYPE_P(value, T_CLASS)) { +# define LITERAL(str) (*sz = rb_strlen_lit(str), str) + + if (value == rb_cNilClass) { + return LITERAL("nil"); + } + else if (value == rb_cFixnum) { + return LITERAL("Fixnum"); + } + else if (value == rb_cSymbol) { + return LITERAL("Symbol"); + } + else if (value == rb_cTrueClass) { + return LITERAL("true"); + } + else if (value == rb_cFalseClass) { + return LITERAL("false"); + } +# undef LITERAL + } value = rb_inspect(value); } else { diff --git a/test/-ext-/typeddata/test_typeddata.rb b/test/-ext-/typeddata/test_typeddata.rb index c24ad08004..31fe32b20a 100644 --- a/test/-ext-/typeddata/test_typeddata.rb +++ b/test/-ext-/typeddata/test_typeddata.rb @@ -12,5 +12,8 @@ class Test_TypedData < Test::Unit::TestCase assert_raise_with_message(TypeError, "wrong argument type Fixnum (expected typed_data)") {Bug::TypedData.check(0)} assert_raise_with_message(TypeError, "wrong argument type String (expected typed_data)") {Bug::TypedData.check("a")} + + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {Bug::TypedData.check(obj)} end end diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index ea551dbeba..74da8c1dab 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1543,6 +1543,8 @@ class TestArray < Test::Unit::TestCase [[:first_one, :ok], :not_ok].to_h } assert_equal "wrong element type Symbol at 1 (expected array)", e.message + array = [eval("class C\u{1f5ff}; self; end").new] + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {array.to_h} e = assert_raise(ArgumentError) { [[:first_one, :ok], [1, 2], [:not_ok]].to_h } diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb index 012a5624d0..d057e39dbd 100644 --- a/test/ruby/test_bignum.rb +++ b/test/ruby/test_bignum.rb @@ -568,6 +568,8 @@ class TestBignum < Test::Unit::TestCase def test_coerce assert_equal([T64P, T31P], T31P.coerce(T64P)) assert_raise(TypeError) { T31P.coerce(nil) } + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) { T31P.coerce(obj) } end def test_abs diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 8da0e51644..de3bb05d6c 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -427,6 +427,9 @@ class Complex_Test < Test::Unit::TestCase assert_equal([Complex(Rational(2)),Complex(1)], Complex(1).coerce(Rational(2))) assert_equal([Complex(2),Complex(1)], Complex(1).coerce(Complex(2))) + + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) { Complex(1).coerce(obj) } end class ObjectX diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 26c8815cfe..10692e687d 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -457,6 +457,8 @@ class TestEnumerable < Test::Unit::TestCase assert_equal([[1, 3], [2, 4], [3, nil], [1, nil], [2, nil]], @obj.zip(ary)) def ary.to_ary; [5, 6]; end assert_equal([[1, 5], [2, 6], [3, nil], [1, nil], [2, nil]], @obj.zip(ary)) + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {(1..1).zip(obj)} end def test_take diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index b5ced3b12c..91c91a4bbd 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -433,6 +433,18 @@ class TestEnumerator < Test::Unit::TestCase assert_raise(RuntimeError) { g.__send__ :initialize, proc { |y| y << 4 << 5 } } + + g = Enumerator::Generator.new(proc {|y| y << 4 << 5; :foo }) + a = [] + assert_equal(:foo, g.each {|x| a << x }) + assert_equal([4, 5], a) + + assert_raise(LocalJumpError) {Enumerator::Generator.new} + assert_raise(TypeError) {Enumerator::Generator.new(1)} + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) { + Enumerator::Generator.new(obj) + } end def test_generator_args diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index b4567d9187..af69d86d6b 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -302,6 +302,10 @@ class TestException < Test::Unit::TestCase assert_raise_with_message(TypeError, /C\u{4032}/) do [*o] end + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) do + Class.new {include obj} + end end def test_errat diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index f4fb5bf3b3..97c6288f4d 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -417,6 +417,11 @@ class TestTime < Test::Unit::TestCase } } assert_raise(ArgumentError) { m.sleep(-1) } + assert_raise(TypeError) { m.sleep("") } + assert_raise(TypeError) { sleep("") } + obj = eval("class C\u{1f5ff}; self; end").new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {m.sleep(obj)} + assert_raise_with_message(TypeError, /C\u{1f5ff}/) {sleep(obj)} end def test_to_f diff --git a/time.c b/time.c index c1c73699a0..11c76a5bac 100644 --- a/time.c +++ b/time.c @@ -202,8 +202,8 @@ divmodv(VALUE n, VALUE d, VALUE *q, VALUE *r) tmp = rb_funcall(n, id_divmod, 1, d); ary = rb_check_array_type(tmp); if (NIL_P(ary)) { - rb_raise(rb_eTypeError, "unexpected divmod result: into %s", - rb_obj_classname(tmp)); + rb_raise(rb_eTypeError, "unexpected divmod result: into %"PRIsVALUE, + rb_obj_class(tmp)); } *q = rb_ary_entry(ary, 0); *r = rb_ary_entry(ary, 1); @@ -560,8 +560,8 @@ wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr) tmp = rb_funcall(w2v(wn), id_divmod, 1, w2v(wd)); ary = rb_check_array_type(tmp); if (NIL_P(ary)) { - rb_raise(rb_eTypeError, "unexpected divmod result: into %s", - rb_obj_classname(tmp)); + rb_raise(rb_eTypeError, "unexpected divmod result: into %"PRIsVALUE, + rb_obj_class(tmp)); } *wq = v2w(rb_ary_entry(ary, 0)); *wr = v2w(rb_ary_entry(ary, 1)); @@ -641,8 +641,10 @@ num_exact(VALUE v) default: typeerror: - rb_raise(rb_eTypeError, "can't convert %s into an exact number", - NIL_P(v) ? "nil" : rb_obj_classname(v)); + if (NIL_P(v)) + rb_raise(rb_eTypeError, "can't convert nil into an exact number"); + rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into an exact number", + rb_obj_class(v)); } return v; } @@ -2322,7 +2324,7 @@ static struct timespec time_timespec(VALUE num, int interval) { struct timespec t; - const char *tstr = interval ? "time interval" : "time"; + const char *const tstr = interval ? "time interval" : "time"; VALUE i, f, ary; #ifndef NEGATIVE_TIME_T @@ -2382,8 +2384,8 @@ time_timespec(VALUE num, int interval) t.tv_nsec = NUM2LONG(f); } else { - rb_raise(rb_eTypeError, "can't convert %s into %s", - rb_obj_classname(num), tstr); + rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into %s", + rb_obj_class(num), tstr); } break; } -- cgit v1.2.3