ruby-changes:22003
From: nobu <ko1@a...>
Date: Thu, 15 Dec 2011 14:33:48 +0900 (JST)
Subject: [ruby-changes:22003] nobu:r34052 (trunk): * error.c (rb_check_typeddata): refine error message with
nobu 2011-12-15 14:33:34 +0900 (Thu, 15 Dec 2011) New Revision: 34052 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34052 Log: * error.c (rb_check_typeddata): refine error message with including expected struct name. Added directories: trunk/ext/-test-/typeddata/ trunk/test/-ext-/typeddata/ Added files: trunk/ext/-test-/typeddata/extconf.rb trunk/ext/-test-/typeddata/typeddata.c trunk/test/-ext-/typeddata/test_typeddata.rb Modified files: trunk/ChangeLog trunk/error.c Index: ChangeLog =================================================================== --- ChangeLog (revision 34051) +++ ChangeLog (revision 34052) @@ -1,3 +1,8 @@ +Thu Dec 15 14:33:33 2011 Nobuyoshi Nakada <nobu@r...> + + * error.c (rb_check_typeddata): refine error message with + including expected struct name. + Thu Dec 15 13:15:51 2011 Nobuyoshi Nakada <nobu@r...> * regcomp.c (onig_region_memsize): implemented for memsize_of(). Index: ext/-test-/typeddata/typeddata.c =================================================================== --- ext/-test-/typeddata/typeddata.c (revision 0) +++ ext/-test-/typeddata/typeddata.c (revision 34052) @@ -0,0 +1,20 @@ +#include <ruby.h> + +static const rb_data_type_t test_data = { + "typed_data", +}; + +static VALUE +test_check(VALUE self, VALUE obj) +{ + rb_check_typeddata(obj, &test_data); + return obj; +} + +void +Init_typeddata(void) +{ + VALUE mBug = rb_define_module("Bug"); + VALUE klass = rb_define_class_under(mBug, "TypedData", rb_cData); + rb_define_singleton_method(klass, "check", test_check, 1); +} Property changes on: ext/-test-/typeddata/typeddata.c ___________________________________________________________________ Added: svn:eol-style + LF Index: ext/-test-/typeddata/extconf.rb =================================================================== --- ext/-test-/typeddata/extconf.rb (revision 0) +++ ext/-test-/typeddata/extconf.rb (revision 34052) @@ -0,0 +1 @@ +create_makefile("-test-/typeddata/typeddata") Property changes on: ext/-test-/typeddata/extconf.rb ___________________________________________________________________ Added: svn:eol-style + LF Index: error.c =================================================================== --- error.c (revision 34051) +++ error.c (revision 34052) @@ -378,6 +378,30 @@ {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */ }; +static const char * +builtin_type_name(VALUE x) +{ + const char *etype; + + if (NIL_P(x)) { + etype = "nil"; + } + else if (FIXNUM_P(x)) { + etype = "Fixnum"; + } + else if (SYMBOL_P(x)) { + etype = "Symbol"; + } + else if (rb_special_const_p(x)) { + x = rb_obj_as_string(x); + etype = StringValuePtr(x); + } + else { + etype = rb_obj_classname(x); + } + return etype; +} + void rb_check_type(VALUE x, int t) { @@ -396,22 +420,7 @@ if (type->type == t) { const char *etype; - if (NIL_P(x)) { - etype = "nil"; - } - else if (FIXNUM_P(x)) { - etype = "Fixnum"; - } - else if (SYMBOL_P(x)) { - etype = "Symbol"; - } - else if (rb_special_const_p(x)) { - x = rb_obj_as_string(x); - etype = StringValuePtr(x); - } - else { - etype = rb_obj_classname(x); - } + etype = builtin_type_name(xt); rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", etype, type->name); } @@ -451,7 +460,8 @@ static const char mesg[] = "wrong argument type %s (expected %s)"; if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA) { - Check_Type(obj, T_DATA); + etype = builtin_type_name(obj); + rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } if (!RTYPEDDATA_P(obj)) { etype = rb_obj_classname(obj); Index: test/-ext-/typeddata/test_typeddata.rb =================================================================== --- test/-ext-/typeddata/test_typeddata.rb (revision 0) +++ test/-ext-/typeddata/test_typeddata.rb (revision 34052) @@ -0,0 +1,21 @@ +require 'test/unit' +require "-test-/typeddata/typeddata" + +class Test_TypedData < Test::Unit::TestCase + def test_wrong_argtype + e = assert_raise(TypeError) {Bug::TypedData.check(false)} + assert_equal("wrong argument type false (expected typed_data)", e.message) + + e = assert_raise(TypeError) {Bug::TypedData.check(true)} + assert_equal("wrong argument type true (expected typed_data)", e.message) + + e = assert_raise(TypeError) {Bug::TypedData.check(:e)} + assert_equal("wrong argument type Symbol (expected typed_data)", e.message) + + e = assert_raise(TypeError) {Bug::TypedData.check(0)} + assert_equal("wrong argument type Fixnum (expected typed_data)", e.message) + + e = assert_raise(TypeError) {Bug::TypedData.check("a")} + assert_equal("wrong argument type String (expected typed_data)", e.message) + end +end Property changes on: test/-ext-/typeddata/test_typeddata.rb ___________________________________________________________________ Added: svn:eol-style + LF -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/