summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--io.c16
-rw-r--r--test/ruby/test_io.rb19
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 <nobu@ruby-lang.org>
+
+ * 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 <shugo@ruby-lang.org>
* 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 }