* error.c (rb_check_typeddata): refine error message with

including expected struct name.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34052 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-12-15 05:33:34 +00:00
parent 57537ccb0e
commit 9dccfb5513
5 changed files with 74 additions and 17 deletions

View File

@ -1,3 +1,8 @@
Thu Dec 15 14:33:33 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* error.c (rb_check_typeddata): refine error message with
including expected struct name.
Thu Dec 15 13:15:51 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> Thu Dec 15 13:15:51 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* regcomp.c (onig_region_memsize): implemented for memsize_of(). * regcomp.c (onig_region_memsize): implemented for memsize_of().

42
error.c
View File

@ -378,22 +378,9 @@ static const struct types {
{T_UNDEF, "undef"}, /* internal use: #undef; should not happen */ {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
}; };
void static const char *
rb_check_type(VALUE x, int t) builtin_type_name(VALUE x)
{ {
const struct types *type = builtin_types;
const struct types *const typeend = builtin_types +
sizeof(builtin_types) / sizeof(builtin_types[0]);
int xt;
if (x == Qundef) {
rb_bug("undef leaked to the Ruby space");
}
xt = TYPE(x);
if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
while (type < typeend) {
if (type->type == t) {
const char *etype; const char *etype;
if (NIL_P(x)) { if (NIL_P(x)) {
@ -412,6 +399,28 @@ rb_check_type(VALUE x, int t)
else { else {
etype = rb_obj_classname(x); etype = rb_obj_classname(x);
} }
return etype;
}
void
rb_check_type(VALUE x, int t)
{
const struct types *type = builtin_types;
const struct types *const typeend = builtin_types +
sizeof(builtin_types) / sizeof(builtin_types[0]);
int xt;
if (x == Qundef) {
rb_bug("undef leaked to the Ruby space");
}
xt = TYPE(x);
if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
while (type < typeend) {
if (type->type == t) {
const char *etype;
etype = builtin_type_name(xt);
rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)", rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
etype, type->name); etype, type->name);
} }
@ -451,7 +460,8 @@ rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
static const char mesg[] = "wrong argument type %s (expected %s)"; static const char mesg[] = "wrong argument type %s (expected %s)";
if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA) { 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)) { if (!RTYPEDDATA_P(obj)) {
etype = rb_obj_classname(obj); etype = rb_obj_classname(obj);

View File

@ -0,0 +1 @@
create_makefile("-test-/typeddata/typeddata")

View File

@ -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);
}

View File

@ -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