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

ruby-changes:25437

From: tenderlove <ko1@a...>
Date: Tue, 6 Nov 2012 20:42:34 +0900 (JST)
Subject: [ruby-changes:25437] tenderlove:r37494 (trunk): * object.c (rb_mod_const_get): Fix constant missing exception class

tenderlove	2012-11-06 20:42:24 +0900 (Tue, 06 Nov 2012)

  New Revision: 37494

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

  Log:
    * object.c (rb_mod_const_get): Fix constant missing exception class
      and message to maintain backwards compatibility. Constant search
      should start at Object when constant starts with '::'
    
    * test/ruby/test_module.rb: test for fixes

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37493)
+++ ChangeLog	(revision 37494)
@@ -1,3 +1,11 @@
+Tue Nov  6 20:40:28 2012  Aaron Patterson <aaron@t...>
+
+	* object.c (rb_mod_const_get): Fix constant missing exception class
+	  and message to maintain backwards compatibility. Constant search
+	  should start at Object when constant starts with '::'
+
+	* test/ruby/test_module.rb: test for fixes
+
 Tue Nov  6 16:50:00 2012  Masaki Matsushita  <glass.saga@g...>
 
 	* lib/tempfile.rb (Tempfile#inspect): fix confusing #inspect.
Index: object.c
===================================================================
--- object.c	(revision 37493)
+++ object.c	(revision 37494)
@@ -1935,12 +1935,28 @@
     }
 
     pbeg = p = path;
+
+    if (!*p) {
+	rb_raise(rb_eNameError, "wrong constant name %s", path);
+    }
+
+    if (p[0] == ':' && p[1] == ':') {
+	mod = rb_cObject;
+	p += 2;
+	pbeg = p;
+    }
+
     while (*p) {
 	while (*p && *p != ':') p++;
+
+	if (pbeg == p) {
+	    rb_raise(rb_eNameError, "wrong constant name %s", path);
+	}
+
 	id = rb_intern3(pbeg, p-pbeg, enc);
 	if (p[0] == ':') {
 	    if (p[1] != ':') {
-              rb_raise(rb_eArgError, "undefined class/module %.*s", (int)(p-path), path);
+		rb_raise(rb_eNameError, "wrong constant name %s", path);
             }
 	    p += 2;
 	    pbeg = p;
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 37493)
+++ test/ruby/test_module.rb	(revision 37494)
@@ -240,6 +240,24 @@
     assert(!Math.const_defined?("IP"))
   end
 
+  def test_bad_constants
+    [
+      "#<Class:0x7b8b718b>",
+      ":Object",
+      "",
+      ":",
+    ].each do |name|
+      e = assert_raises(NameError) {
+        Object.const_get name
+      }
+      assert_equal("wrong constant name %s" % name, e.message)
+    end
+  end
+
+  def test_leading_colons
+    assert_equal Object, AClass.const_get('::Object')
+  end
+
   def test_const_get
     assert_equal(Math::PI, Math.const_get("PI"))
     assert_equal(Math::PI, Math.const_get(:PI))

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

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