ARGF.dup.rewind do not rewind lineno

e$B1sF#$H?=$7$^$9!#e(B

ARGF.dup e$B$re(B rewind e$B$9$k$He(B lineno e$B$,$*$+$7$/$J$j$^$9!#e(B

$ cat foobarbaz.txt
foo
bar
baz

$ ./ruby -ve ’
a = ARGF.dup
p [a.gets, a.lineno]
p [a.gets, a.lineno]
a.rewind
p [a.gets, a.lineno]
p [a.gets, a.lineno]
’ foobarbaz.txt
ruby 1.9.0 (2008-04-17 revision 16058) [i686-linux]
[“foo\n”, 1]
[“bar\n”, 2]
[“foo\n”, 1]
[“bar\n”, 2]

rb_io_t e$B$Ke(B argf e$B$r<($9%U%#!<%k%I$rDI2C$7$FBP1~$7$F$_$^$7$?!#e(B

Index: include/ruby/io.h

— include/ruby/io.h (revision 16062)
+++ include/ruby/io.h (working copy)
@@ -47,6 +47,7 @@
VALUE tied_io_for_writing;
rb_encoding *enc;
rb_encoding *enc2;

  • VALUE argf;
    } rb_io_t;

#define HAVE_RB_IO_T 1
@@ -92,6 +93,7 @@
fp->tied_io_for_writing = 0;
fp->enc = 0;
fp->enc2 = 0;\

  • fp->argf = 0;
    } while (0)

FILE *rb_io_stdio_file(rb_io_t *fptr);
Index: io.c

— io.c (revision 16062)
+++ io.c (working copy)
@@ -994,10 +994,12 @@
rb_io_rewind(VALUE io)
{
rb_io_t *fptr;

  • VALUE argf;

    GetOpenFile(io, fptr);

  • argf = fptr->argf;
    if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail(fptr->path);

  • if (io == ARGF.current_file) {
  • if (argf && io == ARGF.current_file) {
    ARGF.gets_lineno -= fptr->lineno;
    }
    fptr->lineno = 0;
    @@ -5209,6 +5211,8 @@
    if (stdout_binmode) rb_io_binmode(rb_stdout);
    }
    current_file = prep_io(fr, FMODE_READABLE, rb_cFile, fn);

  • GetOpenFile(current_file, fptr);

  • fptr->argf = argf;
    }
    if (argf_binmode) rb_io_binmode(current_file);
    if (argf_enc) {
    Index: gc.c
    ===================================================================
    — gc.c (revision 16062)
    +++ gc.c (working copy)
    @@ -1113,8 +1113,10 @@
    break;

    case T_FILE:

  •    if (obj->as.file.fptr)
    
  •    if (obj->as.file.fptr) {
           gc_mark(obj->as.file.fptr->tied_io_for_writing, lev);
    
  •  gc_mark(obj->as.file.fptr->argf, lev);
    
  • }
    break;

    case T_REGEXP:

e$B1sF#$G$9!#D{@5$G$9!#e(B

08/04/18 e$B$Ke(B Yusuke ENDOH[email protected] e$B$5$s$O=q$-$^$7$?e(B:

p [a.gets, a.lineno]
a.rewind
p [a.gets, a.lineno]
p [a.gets, a.lineno]
’ foobarbaz.txt
ruby 1.9.0 (2008-04-17 revision 16058) [i686-linux]
[“foo\n”, 1]
[“bar\n”, 2]
[“foo\n”, 1]
[“bar\n”, 2]

e$B$9$_$^$;$s!"$3$l$O=$@58e$N%m%0$G$7$?!#e(B
e$B8=>u$G$O$3$&$J$j$^$9!#e(B

$ ./ruby -ve ’
a = ARGF.dup
p [a.gets, a.lineno]
p [a.gets, a.lineno]
a.rewind
p [a.gets, a.lineno]
p [a.gets, a.lineno]
’ foobarbaz.txt
ruby 1.9.0 (2008-04-17 revision 16058) [i686-linux]
[“foo\n”, 1]
[“bar\n”, 2]
[“foo\n”, 3]
[“bar\n”, 4]

e$B$J$+$@$G$9!#e(B

At Fri, 18 Apr 2008 00:20:54 +0900,
Yusuke ENDOH wrote in [ruby-dev:34445]:

ARGF.dup e$B$re(B rewind e$B$9$k$He(B lineno e$B$,$*$+$7$/$J$j$^$9!#e(B

rb_io_t e$B$Ke(B argf e$B$r<($9%U%#!<%k%I$rDI2C$7$FBP1~$7$F$_$^$7$?!#e(B

e$B$3$3$^$G$9$k$s$G$"$l$Pe(Brb_io_getline_fast()e$B$de(Brb_io_getline_1()e$B$Ge(B
e$B$b$=$C$A$r;H$&$h$&$K$7$?$[$&$,$$$$$h$&$J!#$7$+$7!"$=$3$^$G$9$kI,e(B
e$BMW<+BN$"$k$+$I$&$+!#$"$k$$$O$d$C$Q$je(BARGF.dupe$B$O6X;_$9$k$+!D!#e(B