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

ruby-changes:37319

From: nobu <ko1@a...>
Date: Sun, 25 Jan 2015 12:04:17 +0900 (JST)
Subject: [ruby-changes:37319] nobu:r49400 (trunk): array.c: reduce to_ary call

nobu	2015-01-25 12:04:10 +0900 (Sun, 25 Jan 2015)

  New Revision: 49400

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

  Log:
    array.c: reduce to_ary call
    
    * array.c (flatten): no need to call to_ary method on elements
      beyond the given level.  [ruby-core:67637] [Bug #10748]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/array.c
    trunk/test/ruby/test_array.rb
Index: array.c
===================================================================
--- array.c	(revision 49399)
+++ array.c	(revision 49400)
@@ -4387,11 +4387,15 @@ flatten(VALUE ary, int level, int *modif https://github.com/ruby/ruby/blob/trunk/array.c#L4387
     while (1) {
 	while (i < RARRAY_LEN(ary)) {
 	    elt = RARRAY_AREF(ary, i++);
+	    if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) {
+		rb_ary_push(result, elt);
+		continue;
+	    }
 	    tmp = rb_check_array_type(elt);
 	    if (RBASIC(result)->klass) {
 		rb_raise(rb_eRuntimeError, "flatten reentered");
 	    }
-	    if (NIL_P(tmp) || (level >= 0 && RARRAY_LEN(stack) / 2 >= level)) {
+	    if (NIL_P(tmp)) {
 		rb_ary_push(result, elt);
 	    }
 	    else {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49399)
+++ ChangeLog	(revision 49400)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jan 25 12:04:12 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* array.c (flatten): no need to call to_ary method on elements
+	  beyond the given level.  [ruby-core:67637] [Bug #10748]
+
 Sun Jan 25 00:42:24 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/fiddle/win32/libffi.mk.tmpl: assemble without directory prefix.
Index: NEWS
===================================================================
--- NEWS	(revision 49399)
+++ NEWS	(revision 49400)
@@ -22,6 +22,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L22
     no longer changes the receiver array instantly every time the
     block is called.  [Feature #10714]
 
+  * Array#flatten and Array#flatten! no longer try to call #to_ary
+    method on elements beyond the given level.  [Bug #10748]
+
 * IO
   * IO#close doesn't raise when the IO object is closed.  [Feature #10718]
 
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 49399)
+++ test/ruby/test_array.rb	(revision 49400)
@@ -790,6 +790,19 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L790
     assert_not_same(a8, a9)
   end
 
+  def test_flatten_splat
+    bug10748 = '[ruby-core:67637] [Bug #10748]'
+    o = Object.new
+    o.singleton_class.class_eval do
+      define_method(:to_ary) do
+        raise bug10748
+      end
+    end
+    a = @cls[@cls[o]]
+    assert_raise_with_message(RuntimeError, bug10748) {a.flatten}
+    assert_nothing_raised(RuntimeError, bug10748) {a.flatten(1)}
+  end
+
   def test_flatten!
     a1 = @cls[ 1, 2, 3]
     a2 = @cls[ 5, 6 ]
@@ -814,6 +827,19 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L827
     assert_nil(@cls[].flatten!(0), '[ruby-core:23382]')
   end
 
+  def test_flatten_splat!
+    bug10748 = '[ruby-core:67637] [Bug #10748]'
+    o = Object.new
+    o.singleton_class.class_eval do
+      define_method(:to_ary) do
+        raise bug10748
+      end
+    end
+    a = @cls[@cls[o]]
+    assert_raise_with_message(RuntimeError, bug10748) {a.flatten!}
+    assert_nothing_raised(RuntimeError, bug10748) {a.flatten!(1)}
+  end
+
   def test_flatten_with_callcc
     need_continuation
     o = Object.new

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

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