ruby-changes:26870
From: marcandre <ko1@a...>
Date: Thu, 24 Jan 2013 15:23:54 +0900 (JST)
Subject: [ruby-changes:26870] marcandRe: r38922 (trunk): * enumerator.c: Fix state handling for Lazy#drop
marcandre 2013-01-24 15:23:42 +0900 (Thu, 24 Jan 2013) New Revision: 38922 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38922 Log: * enumerator.c: Fix state handling for Lazy#drop [bug #7696] [bug #7691] * test/ruby/test_lazy_enumerator.rb: test for above Modified files: trunk/enumerator.c trunk/test/ruby/test_lazy_enumerator.rb Index: enumerator.c =================================================================== --- enumerator.c (revision 38921) +++ enumerator.c (revision 38922) @@ -1725,13 +1725,16 @@ lazy_drop_size(VALUE lazy) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1725 static VALUE lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv) { - NODE *memo = RNODE(args); - - if (memo->u3.cnt == 0) { + long remain; + VALUE memo = rb_ivar_get(argv[0], id_memo); + if (NIL_P(memo)) { + memo = args; + } + if ((remain = NUM2LONG(memo)) == 0) { rb_funcall2(argv[0], id_yield, argc - 1, argv + 1); } else { - memo->u3.cnt--; + rb_ivar_set(argv[0], id_memo, LONG2NUM(--remain)); } return Qnil; } @@ -1739,15 +1742,13 @@ lazy_drop_func(VALUE val, VALUE args, in https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1742 static VALUE lazy_drop(VALUE obj, VALUE n) { - NODE *memo; long len = NUM2LONG(n); if (len < 0) { rb_raise(rb_eArgError, "attempt to drop negative size"); } - memo = NEW_MEMO(0, 0, len); return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, - lazy_drop_func, (VALUE) memo), + lazy_drop_func, n), rb_ary_new3(1, n), lazy_drop_size); } Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 38921) +++ test/ruby/test_lazy_enumerator.rb (revision 38922) @@ -257,6 +257,13 @@ class TestLazyEnumerator < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_lazy_enumerator.rb#L257 assert_equal([*(6..10)]*5, drop5.flat_map{drop5}.force, bug7696) end + def test_drop_nested + bug7696 = '[ruby-core:51470]' + a = Step.new(1..10) + drop5 = a.lazy.drop(5) + assert_equal([*(6..10)]*5, drop5.flat_map{drop5}.force, bug7696) + end + def test_take_rewound bug7696 = '[ruby-core:51470]' e=(1..42).lazy.take(2) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/