From 9b6e1a9f38ee299b7b2a2282ec7071d54a1494a4 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 20 Dec 2016 08:25:02 +0000 Subject: [PATCH] io.c: update argf lineno * 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 --- io.c | 24 +++++++++++++++++++++++- test/ruby/test_argf.rb | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/io.c b/io.c index 1f00f6c4fa..3da57a0b40 100644 --- a/io.c +++ b/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; } diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index d4be87e954..5c3805ca19 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -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'