[前][次][番号順一覧][スレッド一覧]

ruby-changes:72446

From: Nobuyoshi <ko1@a...>
Date: Thu, 7 Jul 2022 11:33:29 +0900 (JST)
Subject: [ruby-changes:72446] cd94842922 (master): [Bug #18892] Reset `ARGF.lineno` after reading shebang

https://git.ruby-lang.org/ruby.git/commit/?id=cd94842922

From cd948429223223f5e299626a4695ab30e43de2ea Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 2 Jul 2022 20:58:59 +0900
Subject: [Bug #18892] Reset `ARGF.lineno` after reading shebang

---
 io.c                   |  6 ++++++
 ruby.c                 |  3 +++
 test/ruby/test_argf.rb | 11 +++++++++++
 3 files changed, 20 insertions(+)

diff --git a/io.c b/io.c
index efe1ad944a..fe0652f233 100644
--- a/io.c
+++ b/io.c
@@ -9866,6 +9866,12 @@ argf_lineno_setter(VALUE val, ID id, VALUE *var) https://github.com/ruby/ruby/blob/trunk/io.c#L9866
     ARGF.last_lineno = ARGF.lineno = n;
 }
 
+void
+rb_reset_argf_lineno(long n)
+{
+    ARGF.last_lineno = ARGF.lineno = n;
+}
+
 static VALUE argf_gets(int, VALUE *, VALUE);
 
 /*
diff --git a/ruby.c b/ruby.c
index 0b6843d539..51921ffbe5 100644
--- a/ruby.c
+++ b/ruby.c
@@ -2151,6 +2151,8 @@ warn_cr_in_shebang(const char *str, long len) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2151
 #define warn_cr_in_shebang(str, len) (void)0
 #endif
 
+void rb_reset_argf_lineno(long n);
+
 struct load_file_arg {
     VALUE parser;
     VALUE fname;
@@ -2247,6 +2249,7 @@ load_file_internal(VALUE argp_v) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2249
         if (NIL_P(c)) {
 	    argp->f = f = Qnil;
 	}
+        rb_reset_argf_lineno(0);
         ruby_opt_init(opt);
     }
     if (opt->src.enc.index >= 0) {
diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb
index dbffd24370..12f7d6485a 100644
--- a/test/ruby/test_argf.rb
+++ b/test/ruby/test_argf.rb
@@ -143,6 +143,17 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L143
     };
   end
 
+  def test_lineno_after_shebang
+    expected = %w"1 1 1 2 2 2 3 3 1 4 4 2"
+    assert_in_out_err(["--enable=gems", "-", @t1.path, @t2.path], "#{<<~"{#"}\n#{<<~'};'}", expected)
+    #!/usr/bin/env ruby
+    {#
+      ARGF.each do |line|
+        puts [$., ARGF.lineno, ARGF.file.lineno]
+      end
+    };
+  end
+
   def test_new_lineno_each
     f = ARGF.class.new(@t1.path, @t2.path, @t3.path)
     result = []
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]