* io.c (argf_block_call_line): update line number in non-global
  ARGF instance.  [ruby-core:78728] [Bug #13051]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57124 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-12-20 08:25:02 +00:00
Родитель dc2d359b70
Коммит 9b6e1a9f38
2 изменённых файлов: 35 добавлений и 1 удалений

24
io.c
Просмотреть файл

@ -10925,6 +10925,12 @@ rb_stdio_set_default_encoding(void)
rb_io_set_encoding(1, &val, rb_stderr);
}
static inline int
global_argf_p(VALUE arg)
{
return arg == argf;
}
/*
* call-seq:
* ARGF.external_encoding -> encoding
@ -11541,6 +11547,22 @@ argf_block_call(ID mid, int argc, VALUE *argv, VALUE argf)
if (ret != Qundef) ARGF.next_p = 1;
}
static VALUE
argf_block_call_line_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, argf))
{
if (!global_argf_p(argf)) {
ARGF.last_lineno = ++ARGF.lineno;
}
return argf_block_call_i(i, argf, argc, argv, blockarg);
}
static void
argf_block_call_line(ID mid, int argc, VALUE *argv, VALUE argf)
{
VALUE ret = rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_line_i, argf);
if (ret != Qundef) ARGF.next_p = 1;
}
/*
* call-seq:
* ARGF.each(sep=$/) {|line| block } -> ARGF
@ -11578,7 +11600,7 @@ argf_each_line(int argc, VALUE *argv, VALUE argf)
{
RETURN_ENUMERATOR(argf, argc, argv);
FOREACH_ARGF() {
argf_block_call(rb_intern("each_line"), argc, argv, argf);
argf_block_call_line(rb_intern("each_line"), argc, argv, argf);
}
return argf;
}

Просмотреть файл

@ -137,6 +137,18 @@ class TestArgf < Test::Unit::TestCase
INPUT
end
def test_new_lineno
f = ARGF.class.new(@t1.path, @t2.path, @t3.path)
result = []
f.each {|line| result << [f.lineno, line]; break if result.size == 3}
assert_equal(3, f.lineno)
assert_equal((1..3).map {|i| [i, "#{i}\n"]}, result)
f = ARGF.class.new(@t1.path, @t2.path, @t3.path)
f.each_char.to_a
assert_equal(0, f.lineno)
end
def test_inplace
assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], [])
ARGF.inplace_mode = '.bak'