Skip to content

Commit

Permalink
io.c: get rid of IOError when skipped while iteration
Browse files Browse the repository at this point in the history
* io.c (argf_next_argv): set init flag if succeeded to forward, after
  skipping.
* io.c (argf_block_call_i, argf_block_call): no more forwarding if
  forwarded after skipping.  [ruby-list:49185]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39997 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Mar 29, 2013
1 parent 8d7c52d commit 824cb69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion ChangeLog
@@ -1,4 +1,10 @@
Fri Mar 29 16:51:04 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
Fri Mar 29 16:51:39 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>

* io.c (argf_next_argv): set init flag if succeeded to forward, after
skipping.

* io.c (argf_block_call_i, argf_block_call): no more forwarding if
forwarded after skipping. [ruby-list:49185]

* io.c (argf_close): deal with init flag.

Expand Down
9 changes: 5 additions & 4 deletions io.c
Expand Up @@ -7614,7 +7614,6 @@ argf_next_argv(VALUE argf)
else if (ARGF.next_p == -1 && RARRAY_LEN(ARGF.argv) > 0) {
ARGF.next_p = 1;
}
ARGF.init_p = 1;
}

if (ARGF.next_p == 1) {
Expand Down Expand Up @@ -7747,6 +7746,7 @@ argf_next_argv(VALUE argf)
rb_stdout = orig_stdout;
}
}
if (ARGF.init_p == -1) ARGF.init_p = 1;
return TRUE;
}

Expand Down Expand Up @@ -10927,23 +10927,24 @@ argf_readbyte(VALUE argf)
return c;
}

#define FOREACH_ARGF() for (; next_argv(); ARGF.next_p = 1)
#define FOREACH_ARGF() while (next_argv())

static VALUE
argf_block_call_i(VALUE i, VALUE argf, int argc, VALUE *argv)
{
const VALUE current = ARGF.current_file;
rb_yield_values2(argc, argv);
if (ARGF.init_p == -1 || current != ARGF.current_file) {
rb_iter_break();
rb_iter_break_value(Qundef);
}
return Qnil;
}

static void
argf_block_call(ID mid, int argc, VALUE *argv, VALUE argf)
{
rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_i, argf);
VALUE ret = rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_i, argf);
if (ret != Qundef) ARGF.next_p = 1;
}

/*
Expand Down
5 changes: 5 additions & 0 deletions test/ruby/test_argf.rb
Expand Up @@ -689,6 +689,11 @@ def test_skip_in_each_line
SRC
assert_equal("1\n3\n5\n", f.read, '[ruby-list:49185]')
end
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f|
ARGF.each_line {|l| ARGF.skip; puts [l, ARGF.gets].map {|s| s ? s.chomp : s.inspect}.join("+")}
SRC
assert_equal("1+3\n4+5\n6+nil\n", f.read, '[ruby-list:49185]')
end
end

def test_skip_in_each_byte
Expand Down

0 comments on commit 824cb69

Please sign in to comment.