aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-20 08:25:03 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-20 08:25:03 +0000
commit1cc9c93ff9a699689cdef4b1254a70ca8559caaa (patch)
tree4a71a2838d258197aee63a23371721f11423700b
parent9b6e1a9f38ee299b7b2a2282ec7071d54a1494a4 (diff)
downloadruby-1cc9c93ff9a699689cdef4b1254a70ca8559caaa.tar.gz
io.c: update argf lineno
* io.c (argf_rewind): rewind line number in non-global ARGF instance. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57125 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--io.c10
-rw-r--r--test/ruby/test_argf.rb17
2 files changed, 19 insertions, 8 deletions
diff --git a/io.c b/io.c
index 3da57a0b40..65254ff102 100644
--- a/io.c
+++ b/io.c
@@ -11098,11 +11098,19 @@ argf_set_pos(VALUE argf, VALUE offset)
static VALUE
argf_rewind(VALUE argf)
{
+ VALUE ret;
+ int old_lineno;
+
if (!next_argv()) {
rb_raise(rb_eArgError, "no stream to rewind");
}
ARGF_FORWARD(0, 0);
- return rb_io_rewind(ARGF.current_file);
+ old_lineno = RFILE(ARGF.current_file)->fptr->lineno;
+ ret = rb_io_rewind(ARGF.current_file);
+ if (!global_argf_p(argf)) {
+ ARGF.last_lineno = ARGF.lineno -= old_lineno;
+ }
+ return ret;
}
/*
diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb
index 5c3805ca19..fdd31c630a 100644
--- a/test/ruby/test_argf.rb
+++ b/test/ruby/test_argf.rb
@@ -78,15 +78,15 @@ class TestArgf < Test::Unit::TestCase
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, "2", 2]
a.rewind
b.rewind
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, "1", 3]
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, "2", 4]
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, "3", 5]
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, "4", 6]
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 7]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, "1", 1]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["2", 2, "2", 2]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["3", 3, "3", 3]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["4", 4, "4", 4]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 5]
a.rewind
b.rewind
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 8]
- p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, "6", 9]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 5]
+ p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, "6", 6]
SRC
end
@@ -144,6 +144,9 @@ class TestArgf < Test::Unit::TestCase
assert_equal(3, f.lineno)
assert_equal((1..3).map {|i| [i, "#{i}\n"]}, result)
+ f.rewind
+ assert_equal(2, f.lineno)
+
f = ARGF.class.new(@t1.path, @t2.path, @t3.path)
f.each_char.to_a
assert_equal(0, f.lineno)