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

ruby-changes:38888

From: nobu <ko1@a...>
Date: Fri, 19 Jun 2015 13:55:19 +0900 (JST)
Subject: [ruby-changes:38888] nobu:r50969 (trunk): proc.c: inadvertent ID

nobu	2015-06-19 13:55:01 +0900 (Fri, 19 Jun 2015)

  New Revision: 50969

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

  Log:
    proc.c: inadvertent ID
    
    * proc.c (rb_mod_define_method): get rid of inadvertent ID
      creations at error.

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/test/-ext-/symbol/test_inadvertent_creation.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50968)
+++ ChangeLog	(revision 50969)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jun 19 13:54:43 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* proc.c (rb_mod_define_method): get rid of inadvertent ID
+	  creations at error.
+
 Fri Jun 19 07:58:11 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/extmk.rb: if no with-ext option is given, default to
Index: proc.c
===================================================================
--- proc.c	(revision 50968)
+++ proc.c	(revision 50969)
@@ -1657,6 +1657,7 @@ rb_mod_define_method(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/proc.c#L1657
 {
     ID id;
     VALUE body;
+    VALUE name;
     const rb_cref_t *cref = rb_vm_cref_in_context(mod, mod);
     const rb_scope_visibility_t default_scope_visi = {METHOD_VISI_PUBLIC, FALSE};
     const rb_scope_visibility_t *scope_visi = &default_scope_visi;
@@ -1666,13 +1667,13 @@ rb_mod_define_method(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/proc.c#L1667
 	scope_visi = CREF_SCOPE_VISI(cref);
     }
 
+    rb_check_arity(argc, 1, 2);
+    name = argv[0];
+    id = rb_check_id(&name);
     if (argc == 1) {
-	id = rb_to_id(argv[0]);
 	body = rb_block_lambda();
     }
     else {
-	rb_check_arity(argc, 1, 2);
-	id = rb_to_id(argv[0]);
 	body = argv[1];
 	is_method = rb_obj_is_method(body) != Qfalse;
 	if (!is_method && !rb_obj_is_proc(body)) {
@@ -1681,6 +1682,7 @@ rb_mod_define_method(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/proc.c#L1682
 		     rb_obj_classname(body));
 	}
     }
+    if (!id) id = rb_to_id(name);
 
     if (is_method) {
 	struct METHOD *method = (struct METHOD *)DATA_PTR(body);
Index: test/-ext-/symbol/test_inadvertent_creation.rb
===================================================================
--- test/-ext-/symbol/test_inadvertent_creation.rb	(revision 50968)
+++ test/-ext-/symbol/test_inadvertent_creation.rb	(revision 50969)
@@ -63,6 +63,28 @@ module Test_Symbol https://github.com/ruby/ruby/blob/trunk/test/-ext-/symbol/test_inadvertent_creation.rb#L63
       assert_not_interned_error(cl, :const_defined?, name.to_sym)
     end
 
+    def test_module_define_method_type_error
+      cl = Class.new
+      name = noninterned_name
+
+      assert_raise(TypeError) {cl.class_eval {define_method(name, "")}}
+      assert_not_interned(name)
+
+      assert_raise(TypeError) {cl.class_eval {define_method(name.to_sym, "")}}
+      assert_not_pinneddown(name)
+    end
+
+    def test_module_define_method_argument_error
+      cl = Class.new
+      name = noninterned_name
+
+      assert_raise(ArgumentError) {cl.class_eval {define_method(name)}}
+      assert_not_interned(name)
+
+      assert_raise(ArgumentError) {cl.class_eval {define_method(name.to_sym)}}
+      assert_not_pinneddown(name)
+    end
+
     def test_respond_to_missing
       feature5072 = Feature5072
       c = Class.new do

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

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