* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40174 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2013-04-07 12:18:26 +00:00
parent 875bba3fc5
commit d9ff5c2230
3 changed files with 17 additions and 5 deletions

View File

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

View File

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

View File

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