diff --git a/io.c b/io.c index 945c5f4fd15210..c1cf21adc3a4e1 100644 --- a/io.c +++ b/io.c @@ -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; diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index a623fd1d156387..5d4ede1e4ffd9c 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -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"} @@ -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|