diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | object.c | 15 | ||||
-rw-r--r-- | test/ruby/test_module.rb | 2 |
3 files changed, 17 insertions, 5 deletions
@@ -1,3 +1,8 @@ +Sun Apr 7 21:16:19 2013 NARUSE, Yui <naruse@ruby-lang.org> + + * object.c (rb_mod_cvar_set): call to_str for string only once. + to_str was called from rb_is_class_name and rb_to_id before. + Sun Apr 7 13:56:16 2013 Kazuhiro NISHIYAMA <zn@mbf.nifty.com> * test/ruby/test_require.rb (TestRequire#test_require_nonascii_path): @@ -2305,12 +2305,17 @@ rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val) QUOTE_ID(id)); } } - else if (!rb_is_class_name(iv)) { - rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name", - QUOTE(iv)); - } else { - id = rb_to_id(iv); + VALUE cname = rb_check_string_type(iv); + if (NIL_P(cname)) { + rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol or string", + iv); + } + if (!rb_is_class_name(cname)) { + rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name", + QUOTE(cname)); + } + id = rb_to_id(cname); } rb_cvar_set(obj, id, val); return val; diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index b11a86a4da..c899948128 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -665,6 +665,7 @@ class TestModule < Test::Unit::TestCase c.class_eval('@@foo = :foo') assert_equal(:foo, c.class_variable_get(:@@foo)) assert_raise(NameError) { c.class_variable_get(:@@bar) } # c.f. instance_variable_get + assert_raise(NameError) { c.class_variable_get('@@') } assert_raise(NameError) { c.class_variable_get(:foo) } assert_raise(NameError) { c.class_variable_get("bar") } assert_raise(TypeError) { c.class_variable_get(1) } @@ -674,6 +675,7 @@ class TestModule < Test::Unit::TestCase c = Class.new c.class_variable_set(:@@foo, :foo) assert_equal(:foo, c.class_eval('@@foo')) + assert_raise(NameError) { c.class_variable_set('@@', 1) } assert_raise(NameError) { c.class_variable_set(:foo, 1) } assert_raise(NameError) { c.class_variable_set("bar", 1) } assert_raise(TypeError) { c.class_variable_set(1, 1) } |