summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-28 02:40:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-28 02:40:46 +0000
commit4191a6b90d3eeb63a31609dba29a1904efee3738 (patch)
tree6689e8d252ca17f1f90626d7ade4b144dbfccdd7
parent7d49923e9e9a9d1a01f3183fd798258e9d289da0 (diff)
preserve encodings in error messages
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--array.c7
-rw-r--r--bignum.c4
-rw-r--r--complex.c4
-rw-r--r--enum.c4
-rw-r--r--enumerator.c8
-rw-r--r--error.c39
-rw-r--r--numeric.c3
-rw-r--r--sprintf.c20
-rw-r--r--test/-ext-/typeddata/test_typeddata.rb3
-rw-r--r--test/ruby/test_array.rb2
-rw-r--r--test/ruby/test_bignum.rb2
-rw-r--r--test/ruby/test_complex.rb3
-rw-r--r--test/ruby/test_enum.rb2
-rw-r--r--test/ruby/test_enumerator.rb12
-rw-r--r--test/ruby/test_exception.rb4
-rw-r--r--test/ruby/test_time.rb5
-rw-r--r--time.c20
17 files changed, 109 insertions, 33 deletions
diff --git a/array.c b/array.c
index 3a6e231..44eaa20 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<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)) {
- rb_raise(rb_eTypeError, "wrong element type %s at %ld (expected array)",
- rb_builtin_class_name(rb_ary_elt(ary, i)), i);
+ rb_raise(rb_eTypeError, "wrong element type %"PRIsVALUE" at %ld (expected array)",
+ rb_obj_class(elt), i);
}
if (RARRAY_LEN(key_value_pair) != 2) {
rb_raise(rb_eArgError, "wrong array length at %ld (expected 2, was %ld)",
diff --git a/bignum.c b/bignum.c
index a6d68d8..c780e7d 100644
--- a/bignum.c
+++ b/bignum.c
@@ -6834,8 +6834,8 @@ rb_big_coerce(VALUE x, VALUE y)
y = rb_int2big(FIX2LONG(y));
}
else if (!RB_BIGNUM_TYPE_P(y)) {
- rb_raise(rb_eTypeError, "can't coerce %s to Bignum",
- rb_obj_classname(y));
+ rb_raise(rb_eTypeError, "can't coerce %"PRIsVALUE" to Bignum",
+ rb_obj_class(y));
}
return rb_assoc_new(y, x);
}
diff --git a/complex.c b/complex.c
index e3c2013..ec52bea 100644
--- a/complex.c
+++ b/complex.c
@@ -1020,8 +1020,8 @@ nucomp_coerce(VALUE self, VALUE other)
if (RB_TYPE_P(other, T_COMPLEX))
return rb_assoc_new(other, self);
- rb_raise(rb_eTypeError, "%s can't be coerced into %s",
- rb_obj_classname(other), rb_obj_classname(self));
+ rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
+ rb_obj_class(other), rb_obj_class(self));
return Qnil;
}
diff --git a/enum.c b/enum.c
index 70267a0..ea101ba 100644
--- a/enum.c
+++ b/enum.c
@@ -2483,8 +2483,8 @@ enum_zip(int argc, VALUE *argv, VALUE obj)
CONST_ID(conv, "to_enum");
for (i=0; i<argc; i++) {
if (!rb_respond_to(argv[i], id_each)) {
- rb_raise(rb_eTypeError, "wrong argument type %s (must respond to :each)",
- rb_obj_classname(argv[i]));
+ rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
+ rb_obj_class(argv[i]));
}
argv[i] = rb_funcall(argv[i], conv, 1, ID2SYM(id_each));
}
diff --git a/enumerator.c b/enumerator.c
index 7f91f32..092e950 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -1245,8 +1245,8 @@ generator_initialize(int argc, VALUE *argv, VALUE obj)
if (!rb_obj_is_proc(proc))
rb_raise(rb_eTypeError,
- "wrong argument type %s (expected Proc)",
- rb_obj_classname(proc));
+ "wrong argument type %"PRIsVALUE" (expected Proc)",
+ rb_obj_class(proc));
if (rb_block_given_p()) {
rb_warn("given block not used");
@@ -1771,8 +1771,8 @@ lazy_zip(int argc, VALUE *argv, VALUE obj)
if (NIL_P(v)) {
for (; i < argc; i++) {
if (!rb_respond_to(argv[i], id_each)) {
- rb_raise(rb_eTypeError, "wrong argument type %s (must respond to :each)",
- rb_obj_classname(argv[i]));
+ rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must respond to :each)",
+ rb_obj_class(argv[i]));
}
}
ary = rb_ary_new4(argc, argv);
diff --git a/error.c b/error.c
index e41908c..ce641a0 100644
--- a/error.c
+++ b/error.c
@@ -520,9 +520,8 @@ rb_builtin_type_name(int t)
return 0;
}
-#define builtin_class_name rb_builtin_class_name
-const char *
-rb_builtin_class_name(VALUE x)
+static const char *
+builtin_class_name(VALUE x)
{
const char *etype;
@@ -542,6 +541,17 @@ rb_builtin_class_name(VALUE x)
etype = "false";
}
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);
}
return etype;
@@ -560,8 +570,13 @@ rb_check_type(VALUE x, int t)
if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
const char *tname = rb_builtin_type_name(t);
if (tname) {
- rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
- builtin_class_name(x), tname);
+ const char *cname = builtin_class_name(x);
+ 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) {
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 10ed43c..32c9fcb 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 4549791..afe27c2 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 c24ad08..31fe32b 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 ea551db..74da8c1 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 012a562..d057e39 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 8da0e51..de3bb05 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 26c8815..10692e6 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 b5ced3b..91c91a4 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 b4567d9..af69d86 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 f4fb5bf..97c6288 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 c1c7369..11c76a5 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;
}