diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | io.c | 28 | ||||
-rw-r--r-- | test/ruby/test_io_m17n.rb | 13 |
3 files changed, 32 insertions, 13 deletions
@@ -1,3 +1,7 @@ +Fri Aug 22 02:08:58 2008 Tanaka Akira <akr@fsij.org> + + * io.c (rb_io_initialize): accept hash argument. + Thu Aug 21 23:51:51 2008 Shugo Maeda <shugo@ruby-lang.org> * strftime.c (rb_strftime): supported %F and %<precision>N. @@ -5472,31 +5472,31 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io) VALUE fnum, mode, orig; rb_io_t *fp, *ofp = NULL; int fd, flags, modenum = O_RDONLY; + convconfig_t convconfig; + VALUE opt; rb_secure(4); + + opt = pop_last_hash(&argc, &argv); rb_scan_args(argc, argv, "11", &fnum, &mode); - if (argc == 2) { - if (FIXNUM_P(mode)) { - modenum = FIX2LONG(mode); - } - else { - SafeStringValue(mode); - modenum = rb_io_mode_modenum(StringValueCStr(mode)); - } - } + rb_io_extract_modeenc(mode, opt, &modenum, &flags, &convconfig); orig = rb_io_check_io(fnum); if (NIL_P(orig)) { fd = NUM2INT(fnum); UPDATE_MAXFD(fd); - if (argc != 2) { + if (NIL_P(mode)) { #if defined(HAVE_FCNTL) && defined(F_GETFL) modenum = fcntl(fd, F_GETFL); if (modenum == -1) rb_sys_fail(0); + flags = rb_io_modenum_flags(modenum); #endif } MakeOpenFile(io, fp); fp->fd = fd; - fp->mode = rb_io_modenum_flags(modenum); + fp->mode = flags; + fp->enc = convconfig.enc; + fp->enc2 = convconfig.enc2; + clear_codeconv(fp); io_check_tty(fp); } else if (RFILE(io)->fptr) { @@ -5508,8 +5508,7 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io) VALUE s = rb_inspect(orig); rb_raise(rb_eIOError, "too many shared IO for %s", StringValueCStr(s)); } - if (argc == 2) { - flags = rb_io_modenum_flags(modenum); + if (!NIL_P(mode)) { if ((ofp->mode ^ flags) & (FMODE_READWRITE|FMODE_BINMODE)) { if (FIXNUM_P(mode)) { rb_raise(rb_eArgError, "incompatible mode 0x%x", modenum); @@ -5519,6 +5518,9 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io) } } } + if (convconfig.enc || convconfig.enc2) { + rb_raise(rb_eArgError, "encoding specified for shared IO"); + } ofp->refcnt++; RFILE(io)->fptr = ofp; } diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 46b7954a9..d39e03075 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -206,6 +206,19 @@ EOT } end + def test_io_new_enc + with_tmpdir { + generate_file("tmp", "\xa1") + fd = IO.sysopen("tmp") + f = IO.new(fd, "r:sjis") + begin + assert_equal(Encoding::Shift_JIS, f.read.encoding) + ensure + f.close + end + } + end + def test_stdin assert_equal(Encoding.default_external, STDIN.external_encoding) assert_equal(nil, STDIN.internal_encoding) |