diff --git a/io.c b/io.c index e007512..7f55fa2 100644 --- a/io.c +++ b/io.c @@ -6365,7 +6365,17 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file) } if (!NIL_P(nmode)) { - int fmode = rb_io_modestr_fmode(StringValueCStr(nmode)); + VALUE intmode; + int fmode; + + if (!NIL_P(intmode = rb_check_to_integer(nmode, "to_int"))) { + oflags = NUM2INT(intmode); + fmode = rb_io_oflags_fmode(oflags); + } + else { + fmode = rb_io_modestr_fmode(StringValueCStr(nmode)); + } + if (IS_PREP_STDIO(fptr) && ((fptr->mode & FMODE_READWRITE) & (fmode & FMODE_READWRITE)) != (fptr->mode & FMODE_READWRITE)) { @@ -6375,7 +6385,9 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file) rb_io_fmode_modestr(fmode)); } fptr->mode = fmode; - rb_io_mode_enc(fptr, StringValueCStr(nmode)); + if (NIL_P(intmode)) { + rb_io_mode_enc(fptr, StringValueCStr(nmode)); + } fptr->encs.ecflags = 0; fptr->encs.ecopts = Qnil; } diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index e266f11..75ac744 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -1713,6 +1713,24 @@ End } end + def test_reopen_mode + make_tempfile {|t| + open(__FILE__) do |f| + assert_nothing_raised { + f.reopen(t.path, "r") + assert_equal("foo\n", f.gets) + } + end + + open(__FILE__) do |f| + assert_nothing_raised { + f.reopen(t.path, File::RDONLY) + assert_equal("foo\n", f.gets) + } + end + } + end + def test_foreach a = [] IO.foreach("|" + EnvUtil.rubybin + " -e 'puts :foo; puts :bar; puts :baz'") {|x| a << x }