diff --git a/ChangeLog b/ChangeLog index 6a2a5f3c60..52e823bb25 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Apr 12 00:51:21 2010 Kazuhiro NISHIYAMA + + * io.c (rb_io_getline_fast, rb_io_getline_1): fix ARGF.lineno + behavior. [ruby-core:25205] + + * test/ruby/test_argf.rb (TestArgf#test_lineno3): add a test for + above. + Sun Apr 11 23:25:17 2010 Yutaka Kanemoto * configure.in: can not load libraries if --with-opt-dir is used diff --git a/io.c b/io.c index 31bdf7e0a3..2656b2bae8 100644 --- a/io.c +++ b/io.c @@ -2286,7 +2286,7 @@ swallow(rb_io_t *fptr, int term) } static VALUE -rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc) +rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, VALUE io) { VALUE str = Qnil; int len = 0; @@ -2328,7 +2328,13 @@ rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc) str = io_enc_str(str, fptr); ENC_CODERANGE_SET(str, cr); fptr->lineno++; - ARGF.last_lineno = fptr->lineno; + if (io == ARGF.current_file) { + ARGF.lineno++; + ARGF.last_lineno = ARGF.lineno; + } + else { + ARGF.last_lineno = fptr->lineno; + } return str; } @@ -2397,7 +2403,7 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io) } else if (rs == rb_default_rs && limit < 0 && !NEED_READCONV(fptr) && rb_enc_asciicompat(enc = io_read_encoding(fptr))) { - return rb_io_getline_fast(fptr, enc); + return rb_io_getline_fast(fptr, enc, io); } else { int c, newline = -1; @@ -2466,7 +2472,13 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io) if (!NIL_P(str)) { if (!nolimit) { fptr->lineno++; - ARGF.last_lineno = fptr->lineno; + if (io == ARGF.current_file) { + ARGF.lineno++; + ARGF.last_lineno = ARGF.lineno; + } + else { + ARGF.last_lineno = fptr->lineno; + } } } diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index a1f260147e..ffd5096789 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -127,6 +127,14 @@ class TestArgf < Test::Unit::TestCase SRC end + def test_lineno3 + assert_in_out_err(["-", @t1.path, @t2.path], <<-INPUT, %w"1 1 1 2 2 2 3 3 1 4 4 2", [], "[ruby-core:25205]") + ARGF.each do |line| + puts [$., ARGF.lineno, ARGF.file.lineno] + end + INPUT + end + def test_inplace assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) ARGF.inplace_mode = '.bak'