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

ruby-changes:25589

From: shugo <ko1@a...>
Date: Tue, 13 Nov 2012 18:05:53 +0900 (JST)
Subject: [ruby-changes:25589] shugo:r37646 (trunk): * eval.c (rb_mod_using): raise an ArgumentError if cyclic using is

shugo	2012-11-13 18:05:18 +0900 (Tue, 13 Nov 2012)

  New Revision: 37646

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

  Log:
    * eval.c (rb_mod_using): raise an ArgumentError if cyclic using is
      detected.  based on the patch by Charlie Somerville.
      [ruby-core:49092] Bug #7308
    
    * test/ruby/test_refinement.rb: related test.

  Modified files:
    trunk/ChangeLog
    trunk/eval.c
    trunk/test/ruby/test_refinement.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37645)
+++ ChangeLog	(revision 37646)
@@ -1,3 +1,11 @@
+Tue Nov 13 18:01:54 2012  Shugo Maeda  <shugo@r...>
+
+	* eval.c (rb_mod_using): raise an ArgumentError if cyclic using is
+	  detected.  based on the patch by Charlie Somerville.
+	  [ruby-core:49092] Bug #7308
+
+	* test/ruby/test_refinement.rb: related test.
+
 Tue Nov 13 17:40:04 2012  NARUSE, Yui  <naruse@r...>
 
 	* common.mk (vm_insnhelper.c): this target is useless and causes
Index: eval.c
===================================================================
--- eval.c	(revision 37645)
+++ eval.c	(revision 37646)
@@ -1135,6 +1135,26 @@
     rb_hash_foreach(refinements, using_refinement, (VALUE) cref);
 }
 
+
+static int
+check_cyclic_using(VALUE mod, VALUE _, VALUE search)
+{
+    VALUE using_modules;
+    ID id_using_modules;
+    CONST_ID(id_using_modules, "__using_modules__");
+
+    if (mod == search) {
+	rb_raise(rb_eArgError, "cyclic using detected");
+    }
+
+    using_modules = rb_attr_get(mod, id_using_modules);
+    if (!NIL_P(using_modules)) {
+	rb_hash_foreach(using_modules, check_cyclic_using, search);
+    }
+
+    return ST_CONTINUE;
+}
+
 /*
  *  call-seq:
  *     using(module)    -> self
@@ -1150,6 +1170,7 @@
     VALUE using_modules;
 
     Check_Type(module, T_MODULE);
+    check_cyclic_using(module, 0, self);
     CONST_ID(id_using_modules, "__using_modules__");
     using_modules = rb_attr_get(self, id_using_modules);
     if (NIL_P(using_modules)) {
Index: test/ruby/test_refinement.rb
===================================================================
--- test/ruby/test_refinement.rb	(revision 37645)
+++ test/ruby/test_refinement.rb	(revision 37646)
@@ -801,4 +801,25 @@
       p c.foo
     INPUT
   end
+
+  def test_circular_using_is_not_allowed
+    a = Module.new
+    b = Module.new
+
+    assert_raise ArgumentError do
+      a.module_eval do
+        using a
+      end
+    end
+
+    b.module_eval do
+      using a
+    end
+
+    assert_raise ArgumentError do
+      a.module_eval do
+        using b
+      end
+    end
+  end
 end

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

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