From 3a724e086af6a6a8846da1f194cfb12c1977573b Mon Sep 17 00:00:00 2001 From: yugui Date: Sun, 25 Oct 2009 14:46:28 +0000 Subject: [PATCH] merges r24513,r24514 and r24515 from trunk into ruby_1_9_1. -- * class.c (rb_define_class_id_under, rb_define_module_id_under): new functions to define a nested class/module with non-ascii name. * struct.c (make_struct): use name with encoding. * struct.c (inspect_struct): ditto. [ruby-core:24849] -- * test/ruby/test_marshal.rb (test_class_nonascii): test for non-ascii name class. -- * class.c (rb_define_module_id_under): fix the name. * class.c (rb_define_module_under): fix for prevvious changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@25479 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 16 ++++++++++++++++ class.c | 30 +++++++++++++++++++----------- include/ruby/intern.h | 2 ++ struct.c | 21 +++++++++++---------- test/ruby/test_marshal.rb | 6 ++++++ test/ruby/test_struct.rb | 7 +++++++ 6 files changed, 61 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 01fd7f0ebe..d691f1b807 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Wed Aug 12 15:52:04 2009 NARUSE, Yui + + * class.c (rb_define_module_id_under): fix the name. + + * class.c (rb_define_module_under): fix for prevvious changes. + +Wed Aug 12 15:32:16 2009 Nobuyoshi Nakada + + * class.c (rb_define_class_id_under, rb_define_module_id_under): + new functions to define a nested class/module with non-ascii + name. + + * struct.c (make_struct): use name with encoding. + + * struct.c (inspect_struct): ditto. [ruby-core:24849] + Wed Aug 12 Wed Aug 12 14:54:34 2009 Koichi Sasada * insns.def, vm.c, vm_insnhelper.c, vm_insnhelper.h: check diff --git a/class.c b/class.c index 89becccb54..08bd86a623 100644 --- a/class.c +++ b/class.c @@ -298,26 +298,30 @@ rb_define_class(const char *name, VALUE super) VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super) { - VALUE klass; - ID id; + return rb_define_class_id_under(outer, rb_intern(name), super); +} + +VALUE +rb_define_class_id_under(VALUE outer, ID id, VALUE super) +{ + VALUE klass; - id = rb_intern(name); if (rb_const_defined_at(outer, id)) { klass = rb_const_get_at(outer, id); if (TYPE(klass) != T_CLASS) { - rb_raise(rb_eTypeError, "%s is not a class", name); + rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id)); } if (rb_class_real(RCLASS_SUPER(klass)) != super) { - rb_name_error(id, "%s is already defined", name); + rb_name_error(id, "%s is already defined", rb_id2name(id)); } return klass; } if (!super) { rb_warn("no super class for `%s::%s', Object assumed", - rb_class2name(outer), name); + rb_class2name(outer), rb_id2name(id)); } klass = rb_define_class_id(id, super); - rb_set_class_path(klass, outer, name); + rb_set_class_path_string(klass, outer, rb_id2str(id)); rb_const_set(outer, id, klass); rb_class_inherited(super, klass); @@ -368,10 +372,14 @@ rb_define_module(const char *name) VALUE rb_define_module_under(VALUE outer, const char *name) { - VALUE module; - ID id; + return rb_define_module_id_under(outer, rb_intern(name)); +} + +VALUE +rb_define_module_id_under(VALUE outer, ID id) +{ + VALUE module; - id = rb_intern(name); if (rb_const_defined_at(outer, id)) { module = rb_const_get_at(outer, id); if (TYPE(module) == T_MODULE) @@ -381,7 +389,7 @@ rb_define_module_under(VALUE outer, const char *name) } module = rb_define_module_id(id); rb_const_set(outer, id, module); - rb_set_class_path(module, outer, name); + rb_set_class_path_string(module, outer, rb_id2str(id)); return module; } diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 9229bde738..6444230382 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -150,8 +150,10 @@ VALUE rb_make_metaclass(VALUE, VALUE); void rb_check_inheritable(VALUE); VALUE rb_class_inherited(VALUE, VALUE); VALUE rb_define_class_id(ID, VALUE); +VALUE rb_define_class_id_under(VALUE, ID, VALUE); VALUE rb_module_new(void); VALUE rb_define_module_id(ID); +VALUE rb_define_module_id_under(VALUE, ID); VALUE rb_mod_included_modules(VALUE); VALUE rb_mod_include_p(VALUE, VALUE); VALUE rb_mod_ancestors(VALUE); diff --git a/struct.c b/struct.c index bda3cde409..3d876bb216 100644 --- a/struct.c +++ b/struct.c @@ -191,7 +191,7 @@ make_struct(VALUE name, VALUE members, VALUE klass) rb_warn("redefining constant Struct::%s", StringValuePtr(name)); rb_mod_remove_const(klass, ID2SYM(id)); } - nstr = rb_define_class_under(klass, rb_id2name(id), klass); + nstr = rb_define_class_id_under(klass, id, klass); } rb_iv_set(nstr, "__members__", members); @@ -488,21 +488,19 @@ rb_struct_each_pair(VALUE s) static VALUE inspect_struct(VALUE s, VALUE dummy, int recur) { - const char *cname = rb_class2name(rb_obj_class(s)); - VALUE str, members; + VALUE cname = rb_class_name(rb_obj_class(s)); + VALUE members, str = rb_str_new2("#", cname); + return rb_str_cat2(str, ":...>"); } members = rb_struct_members(s); - if (cname[0] == '#') { - str = rb_str_new2("# 0) { rb_str_cat2(str, ", "); } + else if (first != '#') { + rb_str_cat2(str, " "); + } slot = RARRAY_PTR(members)[i]; id = SYM2ID(slot); if (rb_is_local_id(id) || rb_is_const_id(id)) { diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb index 4a9192d448..172bfbb3f7 100644 --- a/test/ruby/test_marshal.rb +++ b/test/ruby/test_marshal.rb @@ -198,4 +198,10 @@ class TestMarshal < Test::Unit::TestCase assert_equal(sym, Marshal.load(Marshal.dump(sym)), '[ruby-core:24788]') end end + + ClassUTF8 = eval("class R\u{e9}sum\u{e9}; self; end") + def test_class_nonascii + a = ClassUTF8.new + assert_instance_of(ClassUTF8, Marshal.load(Marshal.dump(a)), '[ruby-core:24790]') + end end diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index 6c0e1f4fb2..1b68429d0c 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -212,4 +212,11 @@ class TestStruct < Test::Unit::TestCase Struct.new(0) } end + + def test_nonascii + struct_test = Struct.new("R\u{e9}sum\u{e9}", :"r\u{e9}sum\u{e9}") + assert_equal(Struct.const_get("R\u{e9}sum\u{e9}"), struct_test, '[ruby-core:24849]') + a = struct_test.new(42) + assert_equal("#", a.inspect, '[ruby-core:24849]') + end end