From 4a3c049cd1a02f3a650cec165178b33d8bfb0bfd Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 28 Sep 2012 01:40:56 +0000 Subject: [PATCH] io.c: IO#reopen int mode * io.c (rb_io_reopen): accept File::Constants as well as mode string. based on the patch by Glass_saga (Masaki Matsushita) in [ruby-core:47694]. [Feature #7067] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ io.c | 16 ++++++++++++++-- test/ruby/test_io.rb | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 03aa2e634e..082d46695b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Sep 28 10:40:51 2012 Nobuyoshi Nakada + + * io.c (rb_io_reopen): accept File::Constants as well as mode string. + based on the patch by Glass_saga (Masaki Matsushita) in + [ruby-core:47694]. [Feature #7067] + Thu Sep 27 18:36:51 2012 Shugo Maeda * eval.c (rb_overlay_module, rb_mod_refine): accept a module as the diff --git a/io.c b/io.c index e007512135..9f088a9a3f 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 = rb_check_to_int(nmode); + int fmode; + + if (!NIL_P(intmode)) { + 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 e266f115ba..4ee05911fb 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -1713,6 +1713,25 @@ End } end + def test_reopen_mode + feature7067 = '[ruby-core:47694]' + 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(feature7067) { + 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 }