Skip to content

Commit

Permalink
[Bug #20802] Copy encodings at reopen as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Nov 7, 2024
1 parent ffcfaf4 commit 1701dde
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -8326,6 +8326,7 @@ io_reopen(VALUE io, VALUE nfile)

/* copy rb_io_t structure */
fptr->mode = orig->mode | (fptr->mode & FMODE_EXTERNAL);
fptr->encs = orig->encs;
fptr->pid = orig->pid;
fptr->lineno = orig->lineno;
if (RTEST(orig->pathv)) fptr->pathv = orig->pathv;
Expand Down
21 changes: 21 additions & 0 deletions test/ruby/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,17 @@ def test_reopen_opt
}
end

def test_reopen_binmode
f1 = File.open(__FILE__)
f2 = File.open(__FILE__)
f1.binmode
f1.reopen(f2)
assert_not_operator(f1, :binmode?)
ensure
f2.close
f1.close
end

def make_tempfile_for_encoding
t = make_tempfile
open(t.path, "rb+:utf-8") {|f| f.puts "\u7d05\u7389bar\n"}
Expand Down Expand Up @@ -2685,6 +2696,16 @@ def test_reopen_encoding
}
end

def test_reopen_encoding_from_io
f1 = File.open(__FILE__, "rb:UTF-16LE")
f2 = File.open(__FILE__, "r:UTF-8")
f1.reopen(f2)
assert_equal(Encoding::UTF_8, f1.external_encoding)
ensure
f2.close
f1.close
end

def test_reopen_opt_encoding
feature7103 = '[ruby-core:47806]'
make_tempfile_for_encoding {|t|
Expand Down

0 comments on commit 1701dde

Please sign in to comment.