[前][次][番号順一覧][スレッド一覧]

ruby-changes:28122

From: naruse <ko1@a...>
Date: Sun, 7 Apr 2013 21:18:37 +0900 (JST)
Subject: [ruby-changes:28122] naruse:r40174 (trunk): * object.c (rb_mod_cvar_set): call to_str for string only once.

naruse	2013-04-07 21:18:26 +0900 (Sun, 07 Apr 2013)

  New Revision: 40174

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40174

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

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_module.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40173)
+++ ChangeLog	(revision 40174)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Apr  7 21:16:19 2013  NARUSE, Yui  <naruse@r...>
+
+	* 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@m...>
 
 	* test/ruby/test_require.rb (TestRequire#test_require_nonascii_path):
Index: object.c
===================================================================
--- object.c	(revision 40173)
+++ object.c	(revision 40174)
@@ -2305,12 +2305,17 @@ rb_mod_cvar_set(VALUE obj, VALUE iv, VAL https://github.com/ruby/ruby/blob/trunk/object.c#L2305
 			  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;
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 40173)
+++ test/ruby/test_module.rb	(revision 40174)
@@ -665,6 +665,7 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L665
     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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L675
     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) }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]