preserve encodings in error messages

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-09-28 02:40:46 +00:00
parent 7d49923e9e
commit 4191a6b90d
17 changed files with 109 additions and 33 deletions

View File

@ -2170,10 +2170,11 @@ rb_ary_to_h(VALUE ary)
long i; long i;
VALUE hash = rb_hash_new(); VALUE hash = rb_hash_new();
for (i=0; i<RARRAY_LEN(ary); i++) { for (i=0; i<RARRAY_LEN(ary); i++) {
VALUE key_value_pair = rb_check_array_type(rb_ary_elt(ary, i)); const VALUE elt = rb_ary_elt(ary, i);
const VALUE key_value_pair = rb_check_array_type(elt);
if (NIL_P(key_value_pair)) { if (NIL_P(key_value_pair)) {
rb_raise(rb_eTypeError, "wrong element type %s at %ld (expected array)", rb_raise(rb_eTypeError, "wrong element type %"PRIsVALUE" at %ld (expected array)",
rb_builtin_class_name(rb_ary_elt(ary, i)), i); rb_obj_class(elt), i);
} }
if (RARRAY_LEN(key_value_pair) != 2) { if (RARRAY_LEN(key_value_pair) != 2) {
rb_raise(rb_eArgError, "wrong array length at %ld (expected 2, was %ld)", rb_raise(rb_eArgError, "wrong array length at %ld (expected 2, was %ld)",

View File

@ -6834,8 +6834,8 @@ rb_big_coerce(VALUE x, VALUE y)
y = rb_int2big(FIX2LONG(y)); y = rb_int2big(FIX2LONG(y));
} }
else if (!RB_BIGNUM_TYPE_P(y)) { else if (!RB_BIGNUM_TYPE_P(y)) {
rb_raise(rb_eTypeError, "can't coerce %s to Bignum", rb_raise(rb_eTypeError, "can't coerce %"PRIsVALUE" to Bignum",
rb_obj_classname(y)); rb_obj_class(y));
} }
return rb_assoc_new(y, x); return rb_assoc_new(y, x);
} }

View File

@ -1020,8 +1020,8 @@ nucomp_coerce(VALUE self, VALUE other)
if (RB_TYPE_P(other, T_COMPLEX)) if (RB_TYPE_P(other, T_COMPLEX))
return rb_assoc_new(other, self); return rb_assoc_new(other, self);
rb_raise(rb_eTypeError, "%s can't be coerced into %s", rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
rb_obj_classname(other), rb_obj_classname(self)); rb_obj_class(other), rb_obj_class(self));
return Qnil; return Qnil;
} }

4
enum.c
View File

@ -2483,8 +2483,8 @@ enum_zip(int argc, VALUE *argv, VALUE obj)
CONST_ID(conv, "to_enum"); CONST_ID(conv, "to_enum");
for (i=0; i<argc; i++) { for (i=0; i<argc; i++) {
if (!rb_respond_to(argv[i], id_each)) { if (!rb_respond_to(argv[i], id_each)) {
rb_raise(rb_eTypeError, "wrong argument type %s (must respond to :each)", rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
rb_obj_classname(argv[i])); rb_obj_class(argv[i]));
} }
argv[i] = rb_funcall(argv[i], conv, 1, ID2SYM(id_each)); argv[i] = rb_funcall(argv[i], conv, 1, ID2SYM(id_each));
} }

View File

@ -1245,8 +1245,8 @@ generator_initialize(int argc, VALUE *argv, VALUE obj)
if (!rb_obj_is_proc(proc)) if (!rb_obj_is_proc(proc))
rb_raise(rb_eTypeError, rb_raise(rb_eTypeError,
"wrong argument type %s (expected Proc)", "wrong argument type %"PRIsVALUE" (expected Proc)",
rb_obj_classname(proc)); rb_obj_class(proc));
if (rb_block_given_p()) { if (rb_block_given_p()) {
rb_warn("given block not used"); rb_warn("given block not used");
@ -1771,8 +1771,8 @@ lazy_zip(int argc, VALUE *argv, VALUE obj)
if (NIL_P(v)) { if (NIL_P(v)) {
for (; i < argc; i++) { for (; i < argc; i++) {
if (!rb_respond_to(argv[i], id_each)) { if (!rb_respond_to(argv[i], id_each)) {
rb_raise(rb_eTypeError, "wrong argument type %s (must respond to :each)", rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
rb_obj_classname(argv[i])); rb_obj_class(argv[i]));
} }
} }
ary = rb_ary_new4(argc, argv); ary = rb_ary_new4(argc, argv);

39
error.c
View File

@ -520,9 +520,8 @@ rb_builtin_type_name(int t)
return 0; return 0;
} }
#define builtin_class_name rb_builtin_class_name static const char *
const char * builtin_class_name(VALUE x)
rb_builtin_class_name(VALUE x)
{ {
const char *etype; const char *etype;
@ -542,6 +541,17 @@ rb_builtin_class_name(VALUE x)
etype = "false"; etype = "false";
} }
else { else {
etype = NULL;
}
return etype;
}
const char *
rb_builtin_class_name(VALUE x)
{
const char *etype = builtin_class_name(x);
if (!etype) {
etype = rb_obj_classname(x); etype = rb_obj_classname(x);
} }
return etype; return etype;
@ -560,8 +570,13 @@ rb_check_type(VALUE x, int t)
if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) { if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
const char *tname = rb_builtin_type_name(t); const char *tname = rb_builtin_type_name(t);
if (tname) { if (tname) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", const char *cname = builtin_class_name(x);
builtin_class_name(x), tname); if (cname)
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
cname, tname);
else
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
rb_obj_class(x), tname);
} }
if (xt > T_MASK && xt <= 0x3f) { if (xt > T_MASK && xt <= 0x3f) {
rb_fatal("unknown type 0x%x (0x%x given, probably comes from extension library for ruby 1.8)", t, xt); 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) rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
{ {
const char *etype; const char *etype;
static const char mesg[] = "wrong argument type %s (expected %s)";
if (!RB_TYPE_P(obj, T_DATA)) { if (!RB_TYPE_P(obj, T_DATA)) {
wrong_type:
etype = builtin_class_name(obj); 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)) { if (!RTYPEDDATA_P(obj)) {
etype = rb_obj_classname(obj); goto wrong_type;
rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name);
} }
else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name; 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); return DATA_PTR(obj);
} }

View File

@ -295,7 +295,8 @@ do_coerce(VALUE *x, VALUE *y, int err)
if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) { if (!RB_TYPE_P(ary, T_ARRAY) || RARRAY_LEN(ary) != 2) {
if (err) { if (err) {
rb_raise(rb_eTypeError, "coerce must return [x, y]"); 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("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."); rb_warn("#coerce must return [x, y]. The next release will raise an error for this.");
} }

View File

@ -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"); rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
} }
if (sign == '+') { 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); value = rb_inspect(value);
} }
else { else {

View File

@ -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 Fixnum (expected typed_data)") {Bug::TypedData.check(0)}
assert_raise_with_message(TypeError, "wrong argument type String (expected typed_data)") {Bug::TypedData.check("a")} 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
end end

View File

@ -1543,6 +1543,8 @@ class TestArray < Test::Unit::TestCase
[[:first_one, :ok], :not_ok].to_h [[:first_one, :ok], :not_ok].to_h
} }
assert_equal "wrong element type Symbol at 1 (expected array)", e.message 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) { e = assert_raise(ArgumentError) {
[[:first_one, :ok], [1, 2], [:not_ok]].to_h [[:first_one, :ok], [1, 2], [:not_ok]].to_h
} }

View File

@ -568,6 +568,8 @@ class TestBignum < Test::Unit::TestCase
def test_coerce def test_coerce
assert_equal([T64P, T31P], T31P.coerce(T64P)) assert_equal([T64P, T31P], T31P.coerce(T64P))
assert_raise(TypeError) { T31P.coerce(nil) } 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 end
def test_abs def test_abs

View File

@ -427,6 +427,9 @@ class Complex_Test < Test::Unit::TestCase
assert_equal([Complex(Rational(2)),Complex(1)], assert_equal([Complex(Rational(2)),Complex(1)],
Complex(1).coerce(Rational(2))) Complex(1).coerce(Rational(2)))
assert_equal([Complex(2),Complex(1)], Complex(1).coerce(Complex(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 end
class ObjectX class ObjectX

View File

@ -457,6 +457,8 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal([[1, 3], [2, 4], [3, nil], [1, nil], [2, nil]], @obj.zip(ary)) assert_equal([[1, 3], [2, 4], [3, nil], [1, nil], [2, nil]], @obj.zip(ary))
def ary.to_ary; [5, 6]; end def ary.to_ary; [5, 6]; end
assert_equal([[1, 5], [2, 6], [3, nil], [1, nil], [2, nil]], @obj.zip(ary)) 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 end
def test_take def test_take

View File

@ -433,6 +433,18 @@ class TestEnumerator < Test::Unit::TestCase
assert_raise(RuntimeError) { assert_raise(RuntimeError) {
g.__send__ :initialize, proc { |y| y << 4 << 5 } 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 end
def test_generator_args def test_generator_args

View File

@ -302,6 +302,10 @@ class TestException < Test::Unit::TestCase
assert_raise_with_message(TypeError, /C\u{4032}/) do assert_raise_with_message(TypeError, /C\u{4032}/) do
[*o] [*o]
end 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 end
def test_errat def test_errat

View File

@ -417,6 +417,11 @@ class TestTime < Test::Unit::TestCase
} }
} }
assert_raise(ArgumentError) { m.sleep(-1) } 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 end
def test_to_f def test_to_f

20
time.c
View File

@ -202,8 +202,8 @@ divmodv(VALUE n, VALUE d, VALUE *q, VALUE *r)
tmp = rb_funcall(n, id_divmod, 1, d); tmp = rb_funcall(n, id_divmod, 1, d);
ary = rb_check_array_type(tmp); ary = rb_check_array_type(tmp);
if (NIL_P(ary)) { if (NIL_P(ary)) {
rb_raise(rb_eTypeError, "unexpected divmod result: into %s", rb_raise(rb_eTypeError, "unexpected divmod result: into %"PRIsVALUE,
rb_obj_classname(tmp)); rb_obj_class(tmp));
} }
*q = rb_ary_entry(ary, 0); *q = rb_ary_entry(ary, 0);
*r = rb_ary_entry(ary, 1); *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)); tmp = rb_funcall(w2v(wn), id_divmod, 1, w2v(wd));
ary = rb_check_array_type(tmp); ary = rb_check_array_type(tmp);
if (NIL_P(ary)) { if (NIL_P(ary)) {
rb_raise(rb_eTypeError, "unexpected divmod result: into %s", rb_raise(rb_eTypeError, "unexpected divmod result: into %"PRIsVALUE,
rb_obj_classname(tmp)); rb_obj_class(tmp));
} }
*wq = v2w(rb_ary_entry(ary, 0)); *wq = v2w(rb_ary_entry(ary, 0));
*wr = v2w(rb_ary_entry(ary, 1)); *wr = v2w(rb_ary_entry(ary, 1));
@ -641,8 +641,10 @@ num_exact(VALUE v)
default: default:
typeerror: typeerror:
rb_raise(rb_eTypeError, "can't convert %s into an exact number", if (NIL_P(v))
NIL_P(v) ? "nil" : rb_obj_classname(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; return v;
} }
@ -2322,7 +2324,7 @@ static struct timespec
time_timespec(VALUE num, int interval) time_timespec(VALUE num, int interval)
{ {
struct timespec t; struct timespec t;
const char *tstr = interval ? "time interval" : "time"; const char *const tstr = interval ? "time interval" : "time";
VALUE i, f, ary; VALUE i, f, ary;
#ifndef NEGATIVE_TIME_T #ifndef NEGATIVE_TIME_T
@ -2382,8 +2384,8 @@ time_timespec(VALUE num, int interval)
t.tv_nsec = NUM2LONG(f); t.tv_nsec = NUM2LONG(f);
} }
else { else {
rb_raise(rb_eTypeError, "can't convert %s into %s", rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into %s",
rb_obj_classname(num), tstr); rb_obj_class(num), tstr);
} }
break; break;
} }